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 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

View File

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

View File

@ -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
};
});

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({
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);
}

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({
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 = $('<table/>');

View File

@ -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 <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": {
"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"]) {
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
);
}
}