StaticMD/index.php

153 lines
5.3 KiB
PHP
Raw Normal View History

2017-07-16 20:10:52 +00:00
<?php
require_once('Parsedown.php');
2017-07-16 23:13:10 +00:00
if(file_exists('config.php')) {
require_once('config.php');
} else {
require_once('config.example.php');
}
2017-07-16 20:10:52 +00:00
$func = $_GET['f'];
switch($func) {
case 'build': // Build the worship.html page
buildStaticPage();
break;
default:
showAdminPage();
break;
}
function buildStaticPage() {
$pd = new Parsedown();
$title = !empty($_POST['title'])?$_POST['title']:TITLE;
2017-07-18 16:12:41 +00:00
$out = getHTMLHeader($title);
$selFiles = json_decode($_POST['chosen_files']);
foreach($selFiles as $file) {
2017-07-16 20:10:52 +00:00
if(file_exists($file)) {
$md = file_get_contents($file);
$out .= '<div class="afile">';
$out .= $pd->text($md);
$out .= "</div>";
}
}
2017-07-18 16:12:41 +00:00
$out .= getHTMLFooter();
2017-07-16 23:13:10 +00:00
$outfile = !empty($_POST['outfile'])?$_POST['outfile']:DEFAULT_FILE;
file_put_contents($outfile, $out);
2017-07-18 16:12:41 +00:00
header('Location: '.$outfile);
2017-07-16 20:10:52 +00:00
}
function showAdminPage() {
2017-07-18 14:19:06 +00:00
$out = getHTMLHeader(TITLE)
2017-07-18 16:12:41 +00:00
.'<form class="pure-form" action="/manager/?f=build" method="POST">'."\n"
2017-07-18 14:19:06 +00:00
.' <fieldset>'."\n"
2017-07-18 16:12:41 +00:00
.' <input id="chosen_files" name="chosen_files" value="" type="hidden" />'."\n"
2017-07-18 14:19:06 +00:00
.' <div class="margin10">'."\n"
2017-07-18 16:41:27 +00:00
.' <button class="pure-button pure-button-primary" type="submit">Build File</button>'."\n"
2017-07-18 14:19:06 +00:00
.' </div>'."\n"
.' <div class="margin10">'."\n"
.' <label for="filter">Filter</label>'."\n"
.' <input onkeyup="updateFilter(this.value);" name="filter" value="" placeholder="Filter Files" />'."\n"
.' <table>'."\n"
.' <tbody>'."\n";
2017-07-16 20:10:52 +00:00
foreach(glob(MD_DIR.'/*.md') as $file) {
2017-07-18 14:19:06 +00:00
$base = basename($file, '.md');
$out.=' <tr data-file="'.$base.'">'."\n"
.' <td>'.$base.'</td>'
2017-07-18 16:12:41 +00:00
.'<td><a class="pure-button pure-button-secondary" onclick="javascript:toggleFile(this, \''.$file.'\');">Add</a></td>'."\n"
2017-07-18 14:19:06 +00:00
.' </tr>'."\n";
2017-07-16 20:10:52 +00:00
}
2017-07-18 14:19:06 +00:00
$out.=' </tbody>'."\n"
.' </table>'."\n"
.' </div>'."\n"
.' </fieldset>'."\n"
.'</form>'."\n"
.'<script>'."\n"
2017-07-18 16:12:41 +00:00
.'var selectedFiles = [];'."\n"
2017-07-18 14:19:06 +00:00
.'var tablerows = document.getElementsByTagName("tr");'."\n"
.'function updateFilter(flt) {'."\n"
.' flt = flt.toLowerCase();'."\n"
.' for(var i = 0; i < tablerows.length; i++) {'."\n"
.' if(tablerows[i].dataset.file.toLowerCase().startsWith(flt)) {'."\n"
.' tablerows[i].classList.remove("hidden");'."\n"
.' } else {'."\n"
.' tablerows[i].classList.add("hidden");'."\n"
.' }'."\n"
.' }'."\n"
.'}'."\n"
2017-07-18 16:12:41 +00:00
.'function toggleFile(btn, sng) {'."\n"
.' if(!btn.classList.contains("pure-button-warning")) {'."\n"
.' btn.classList.remove("pure-button-secondary");'."\n"
.' btn.classList.add("pure-button-warning");'."\n"
.' for(var i = 0; i < selectedFiles.length; i++) {'."\n"
.' if(selectedFiles[i] == sng) { return; }'."\n"
.' }'."\n"
.' selectedFiles.push(sng);'."\n"
.' btn.innerText = "Remove";'."\n"
.' } else {'."\n"
.' btn.classList.remove("pure-button-warning");'."\n"
.' btn.classList.add("pure-button-secondary");'."\n"
.' for(var i = 0; i < selectedFiles.length; i++) {'."\n"
.' if(selectedFiles[i] == sng) { delete selectedFiles[i]; }'."\n"
.' }'."\n"
.' btn.innerText = "Add";'."\n"
.' }'."\n"
.' updateFilesInput();'."\n"
.'}'."\n"
.'function removeSong(sng) {'."\n"
.' for(var i = 0; i < selectedFiles.length; i++) {'."\n"
.' if(selectedFiles[i] == sng) { delete selectedFiles[i]; }'."\n"
.' }'."\n"
.' updateFilesInput();'."\n"
.'}'."\n"
.'function updateFilesInput() {'."\n"
.' var out = "[";'."\n"
.' for(var i = 0; i < selectedFiles.length; i++) {'."\n"
.' out+="\""+selectedFiles[i]+"\","'."\n"
.' }'."\n"
.' out = out.substring(0,out.length-1)+"]"'."\n"
.' document.getElementById("chosen_files").value = out;'."\n"
.'}'."\n"
2017-07-18 14:19:06 +00:00
.'</script>'."\n"
.getHTMLFooter();
echo $out;
2017-07-16 20:10:52 +00:00
}
2017-07-18 14:19:06 +00:00
function getHTMLHeader($title = TITLE) {
2017-07-16 20:10:52 +00:00
$out = '<!DOCTYPE html>'."\n"
.'<html lang="en">'."\n"
.' <head>'."\n"
.' <title>'.$title.'</title>'."\n"
2017-07-16 23:13:10 +00:00
.' <link rel="stylesheet" href="https://unpkg.com/purecss@1.0.0/build/pure-min.css" integrity="sha384-nn4HPE8lTHyVtfCBi5yW9d20FjT8BJwUXyWZT9InLYax14RDjBj46LmSztkmNP9w" crossorigin="anonymous">'."\n"
2017-07-18 14:19:06 +00:00
.' <style type="text/css">'."\n"
.' body { margin: 20px; }'."\n"
.' td {'."\n"
.' padding-top: 2px;'."\n"
.' padding-bottom: 3px;'."\n"
.' }'."\n"
.' .margin10 {'."\n"
.' margin: 10px;'."\n"
.' }'."\n"
2017-07-18 16:12:41 +00:00
.' .pure-button-secondary {'."\n"
.' background-color: #0A0;'."\n"
.' color: #FFF;'."\n"
.' }'."\n"
.' .pure-button-warning {'."\n"
.' background-color: #A00;'."\n"
.' color: #FFF;'."\n"
.' }'."\n"
2017-07-18 14:19:06 +00:00
.' </style>'."\n"
2017-07-16 20:10:52 +00:00
.' </head>'."\n"
.' <body>'."\n";
2017-07-18 14:19:06 +00:00
return $out;
2017-07-16 20:10:52 +00:00
}
2017-07-18 14:19:06 +00:00
function getHTMLFooter() {
2017-07-16 20:10:52 +00:00
$out = ' </body>'."\n"
.'</html>'."\n";
2017-07-18 14:19:06 +00:00
return $out;
2017-07-16 20:10:52 +00:00
}
?>