From b6cf568e315cc0f07eb8b58f091f5db96fb97788 Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Sun, 8 Jun 2014 15:13:36 +0200 Subject: [PATCH] Add markdown support for custom headers and footers. --- CHANGELOG.md | 1 + src/_h5ai/client/css/inc/content.less | 4 ++ src/_h5ai/client/js/inc/core/resource.js | 32 ++++++++++- src/_h5ai/client/js/inc/ext/custom.js | 28 +++++++++- src/_h5ai/client/js/inc/ext/preview-txt.js | 30 +--------- src/_h5ai/conf/options.json | 9 +-- src/_h5ai/server/php/inc/class-app.php | 64 +++++++++++++++++++++- 7 files changed, 132 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 309ef8f3..9b9feaac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * adds scroll position reset on location change (issue [#279](https://github.com/lrsjng/h5ai/issues/279)) * adds option to hide unreadable files * adds option where to place folders (top, inplace, bottom) +* adds markdown support for custom header and footer files * fixes QR code URI origin (issue [#287](https://github.com/lrsjng/h5ai/issues/287)) * improves preview GUI * adds Google UA support diff --git a/src/_h5ai/client/css/inc/content.less b/src/_h5ai/client/css/inc/content.less index ddda9b97..75abee3a 100644 --- a/src/_h5ai/client/css/inc/content.less +++ b/src/_h5ai/client/css/inc/content.less @@ -17,6 +17,10 @@ color: #555; } } + + pre, code { + font-family: @font-family-mono; + } } #content-header { diff --git a/src/_h5ai/client/js/inc/core/resource.js b/src/_h5ai/client/js/inc/core/resource.js index 8e864534..384aeae7 100644 --- a/src/_h5ai/client/js/inc/core/resource.js +++ b/src/_h5ai/client/js/inc/core/resource.js @@ -4,6 +4,7 @@ modulejs.define('core/resource', ['_', 'config', 'core/settings'], function (_, var imagesHref = settings.appHref + 'client/images/', fallbackHref = settings.appHref + 'client/images/fallback/', themesHref = settings.appHref + 'client/themes/', + scriptsHref = settings.appHref + 'client/js/', fallbacks = ['file', 'folder', 'folder-page', 'folder-parent', 'ar', 'aud', 'bin', 'img', 'txt', 'vid'], image = function (id) { @@ -29,10 +30,39 @@ modulejs.define('core/resource', ['_', 'config', 'core/settings'], function (_, } return fallbackHref + 'file.svg'; + }, + + loadScript = function (url, globalId, callback) { + + if (window[globalId]) { + callback(window[globalId]); + } else { + $.ajax({ + url: url, + dataType: 'script', + complete: function () { + + callback(window[globalId]); + } + }); + } + }, + + loadSyntaxhighlighter = function (callback) { + + loadScript(scriptsHref + 'syntaxhighlighter.js', 'SyntaxHighlighter', callback); + }, + + loadMarkdown = function (callback) { + + loadScript(scriptsHref + 'markdown.js', 'markdown', callback); }; + return { image: image, - icon: icon + icon: icon, + loadSyntaxhighlighter: loadSyntaxhighlighter, + loadMarkdown: loadMarkdown }; }); diff --git a/src/_h5ai/client/js/inc/ext/custom.js b/src/_h5ai/client/js/inc/ext/custom.js index 613165e5..7d116740 100644 --- a/src/_h5ai/client/js/inc/ext/custom.js +++ b/src/_h5ai/client/js/inc/ext/custom.js @@ -1,5 +1,5 @@ -modulejs.define('ext/custom', ['_', '$', 'core/settings', 'core/server', 'core/event'], function (_, $, allsettings, server, event) { +modulejs.define('ext/custom', ['_', '$', 'core/settings', 'core/server', 'core/event', 'core/resource'], function (_, $, allsettings, server, event, resource) { var settings = _.extend({ enabled: false @@ -10,16 +10,38 @@ modulejs.define('ext/custom', ['_', '$', 'core/settings', 'core/server', 'core/e server.request({action: 'get', custom: true, customHref: item.absHref}, function (response) { var h, f; + if (response) { + if (response.custom.header) { - $('#content-header').html(response.custom.header).stop().slideDown(200); + if (response.custom.header_type === 'md') { + resource.loadMarkdown(function (md) { + + if (md) { + $('#content-header').html(md.toHTML(response.custom.header)).stop().slideDown(200); + } + }); + } else { + $('#content-header').html(response.custom.header).stop().slideDown(200); + } h = true; } + if (response.custom.footer) { - $('#content-footer').html(response.custom.footer).stop().slideDown(200); + if (response.custom.footer_type === 'md') { + resource.loadMarkdown(function (md) { + + if (md) { + $('#content-footer').html(md.toHTML(response.custom.footer)).stop().slideDown(200); + } + }); + } else { + $('#content-footer').html(response.custom.footer).stop().slideDown(200); + } f = true; } } + if (!h) { $('#content-header').stop().slideUp(200); } diff --git a/src/_h5ai/client/js/inc/ext/preview-txt.js b/src/_h5ai/client/js/inc/ext/preview-txt.js index ee857a27..c001b7a2 100644 --- a/src/_h5ai/client/js/inc/ext/preview-txt.js +++ b/src/_h5ai/client/js/inc/ext/preview-txt.js @@ -1,5 +1,5 @@ -modulejs.define('ext/preview-txt', ['_', '$', 'core/settings', 'core/event', 'ext/preview'], function (_, $, allsettings, event, preview) { +modulejs.define('ext/preview-txt', ['_', '$', 'core/settings', 'core/event', 'core/resource', 'ext/preview'], function (_, $, allsettings, event, resource, preview) { var settings = _.extend({ enabled: false, @@ -46,30 +46,6 @@ modulejs.define('ext/preview-txt', ['_', '$', 'core/settings', 'core/event', 'ex return $(brush.getHtml(content)).find('.line'); }, - loadScript = function (url, globalId, callback) { - - if (window[globalId]) { - callback(window[globalId]); - } else { - $.ajax({ - url: url, - dataType: 'script', - complete: function () { - - callback(window[globalId]); - } - }); - } - }, - loadSyntaxhighlighter = function (callback) { - - loadScript(allsettings.appHref + 'client/js/syntaxhighlighter.js', 'SyntaxHighlighter', callback); - }, - loadMarkdown = function (callback) { - - loadScript(allsettings.appHref + 'client/js/markdown.js', 'markdown', callback); - }, - preloadText = function (absHref, callback) { $.ajax({ @@ -132,7 +108,7 @@ modulejs.define('ext/preview-txt', ['_', '$', 'core/settings', 'core/event', 'ex $text = $(templateMarkdown).text(textContent); - loadMarkdown(function (md) { + resource.loadMarkdown(function (md) { if (md) { $text.html(md.toHTML(textContent)); @@ -142,7 +118,7 @@ modulejs.define('ext/preview-txt', ['_', '$', 'core/settings', 'core/event', 'ex $text = $(templateText).text(textContent); - loadSyntaxhighlighter(function (sh) { + resource.loadSyntaxhighlighter(function (sh) { if (sh) { var $table = $(''); diff --git a/src/_h5ai/conf/options.json b/src/_h5ai/conf/options.json index d9536d02..bb07cfa2 100644 --- a/src/_h5ai/conf/options.json +++ b/src/_h5ai/conf/options.json @@ -82,11 +82,12 @@ Options /* Allow customized header and footer files. - First looks for files "_h5ai.header.html" and "_h5ai.footer.html" in the current directory. - If not found it looks in all parent directories (starting in the current directory) for - files "_h5ai.headers.html" and "_h5ai.footers.html" until it finds one. Note the different - filenames: "header" (only current) - "headers" (current and sub directories)! + First checks for files "_h5ai.header.html" and "_h5ai.footer.html" in the current directory. + If not successful it checks all parent directories (starting in the current directory) for + files "_h5ai.headers.html" and "_h5ai.footers.html". + Note the different filenames: "header" (only current) - "headers" (current and sub directories)! The file's content will be placed inside a
tag above/below the main content. + If a file's extension is ".md" instead of ".html" its content will be interpreted as markdown. */ "custom": { "enabled": true diff --git a/src/_h5ai/server/php/inc/class-app.php b/src/_h5ai/server/php/inc/class-app.php index 94d3ec24..4b2a04d5 100644 --- a/src/_h5ai/server/php/inc/class-app.php +++ b/src/_h5ai/server/php/inc/class-app.php @@ -267,7 +267,7 @@ class App { } - public function get_customizations($url) { + public function get_customizations2($url) { if (!$this->options["custom"]["enabled"]) { return array( @@ -309,4 +309,66 @@ class App { "footer" => $footer ); } + + + private function read_custom_file($path, $name, &$content, &$type) { + + foreach (array("html", "md") as $ext) { + $file = "$path/" . FILE_PREFIX . ".$name.$ext"; + if (is_readable($file)) { + $content = file_get_contents($file); + $type = $ext; + return; + } + } + } + + + public function get_customizations($url) { + + if (!$this->options["custom"]["enabled"]) { + return array( + "header" => null, + "header_type" => null, + "footer" => null, + "footer_type" => null + ); + } + + $path = $this->to_path($url); + + $header = null; + $header_type = null; + $footer = null; + $footer_type = null; + + $this->read_custom_file($path, "header", $header, $header_type); + $this->read_custom_file($path, "footer", $footer, $footer_type); + + while ($header === null || $footer === null) { + + if ($header === null) { + $this->read_custom_file($path, "headers", $header, $header_type); + } + if ($footer === null) { + $this->read_custom_file($path, "footers", $footer, $footer_type); + } + + if ($path === ROOT_PATH) { + break; + } + $parent_path = normalize_path(dirname($path)); + if ($parent_path === $path) { + break; + } + $path = $parent_path; + } + + return array( + "header" => $header, + "header_type" => $header_type, + "footer" => $footer, + "footer_type" => $footer_type + ); + } }