Add markdown support for custom headers and footers.

This commit is contained in:
Lars Jung 2014-06-08 15:13:36 +02:00
parent f0721afb88
commit b6cf568e31
7 changed files with 132 additions and 36 deletions

View file

@ -11,6 +11,7 @@
* adds scroll position reset on location change (issue [#279](https://github.com/lrsjng/h5ai/issues/279)) * adds scroll position reset on location change (issue [#279](https://github.com/lrsjng/h5ai/issues/279))
* adds option to hide unreadable files * adds option to hide unreadable files
* adds option where to place folders (top, inplace, bottom) * 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)) * fixes QR code URI origin (issue [#287](https://github.com/lrsjng/h5ai/issues/287))
* improves preview GUI * improves preview GUI
* adds Google UA support * adds Google UA support

View file

@ -17,6 +17,10 @@
color: #555; color: #555;
} }
} }
pre, code {
font-family: @font-family-mono;
}
} }
#content-header { #content-header {

View file

@ -4,6 +4,7 @@ modulejs.define('core/resource', ['_', 'config', 'core/settings'], function (_,
var imagesHref = settings.appHref + 'client/images/', var imagesHref = settings.appHref + 'client/images/',
fallbackHref = settings.appHref + 'client/images/fallback/', fallbackHref = settings.appHref + 'client/images/fallback/',
themesHref = settings.appHref + 'client/themes/', themesHref = settings.appHref + 'client/themes/',
scriptsHref = settings.appHref + 'client/js/',
fallbacks = ['file', 'folder', 'folder-page', 'folder-parent', 'ar', 'aud', 'bin', 'img', 'txt', 'vid'], fallbacks = ['file', 'folder', 'folder-page', 'folder-parent', 'ar', 'aud', 'bin', 'img', 'txt', 'vid'],
image = function (id) { image = function (id) {
@ -29,10 +30,39 @@ modulejs.define('core/resource', ['_', 'config', 'core/settings'], function (_,
} }
return fallbackHref + 'file.svg'; 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 { return {
image: image, image: image,
icon: icon icon: icon,
loadSyntaxhighlighter: loadSyntaxhighlighter,
loadMarkdown: loadMarkdown
}; };
}); });

View file

@ -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({ var settings = _.extend({
enabled: false 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) { server.request({action: 'get', custom: true, customHref: item.absHref}, function (response) {
var h, f; var h, f;
if (response) { if (response) {
if (response.custom.header) { 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; h = true;
} }
if (response.custom.footer) { 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; f = true;
} }
} }
if (!h) { if (!h) {
$('#content-header').stop().slideUp(200); $('#content-header').stop().slideUp(200);
} }

View file

@ -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({ var settings = _.extend({
enabled: false, enabled: false,
@ -46,30 +46,6 @@ modulejs.define('ext/preview-txt', ['_', '$', 'core/settings', 'core/event', 'ex
return $(brush.getHtml(content)).find('.line'); 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) { preloadText = function (absHref, callback) {
$.ajax({ $.ajax({
@ -132,7 +108,7 @@ modulejs.define('ext/preview-txt', ['_', '$', 'core/settings', 'core/event', 'ex
$text = $(templateMarkdown).text(textContent); $text = $(templateMarkdown).text(textContent);
loadMarkdown(function (md) { resource.loadMarkdown(function (md) {
if (md) { if (md) {
$text.html(md.toHTML(textContent)); $text.html(md.toHTML(textContent));
@ -142,7 +118,7 @@ modulejs.define('ext/preview-txt', ['_', '$', 'core/settings', 'core/event', 'ex
$text = $(templateText).text(textContent); $text = $(templateText).text(textContent);
loadSyntaxhighlighter(function (sh) { resource.loadSyntaxhighlighter(function (sh) {
if (sh) { if (sh) {
var $table = $('<table/>'); var $table = $('<table/>');

View file

@ -82,11 +82,12 @@ Options
/* /*
Allow customized header and footer files. Allow customized header and footer files.
First looks for files "_h5ai.header.html" and "_h5ai.footer.html" in the current directory. First checks 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 If not successful it checks all parent directories (starting in the current directory) for
files "_h5ai.headers.html" and "_h5ai.footers.html" until it finds one. Note the different files "_h5ai.headers.html" and "_h5ai.footers.html".
filenames: "header" (only current) - "headers" (current and sub directories)! Note the different filenames: "header" (only current) - "headers" (current and sub directories)!
The file's content will be placed inside a <div/> tag above/below the main content. The file's content will be placed inside a <div/> tag above/below the main content.
If a file's extension is ".md" instead of ".html" its content will be interpreted as markdown.
*/ */
"custom": { "custom": {
"enabled": true "enabled": true

View file

@ -267,7 +267,7 @@ class App {
} }
public function get_customizations($url) { public function get_customizations2($url) {
if (!$this->options["custom"]["enabled"]) { if (!$this->options["custom"]["enabled"]) {
return array( return array(
@ -309,4 +309,66 @@ class App {
"footer" => $footer "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
);
}
} }