Merge branch 'master' of github.com:typecho/typecho

This commit is contained in:
fen 2016-12-20 12:52:04 +08:00
commit 820828757a
6 changed files with 1464 additions and 168 deletions

View File

@ -1,5 +1,6 @@
<?php if(!defined('__TYPECHO_ADMIN__')) exit; ?>
<?php $content = !empty($post) ? $post : $page; if ($options->markdown): ?>
<script src="<?php $options->adminStaticUrl('js', 'hyperdown.js?v=' . $suffixVersion); ?>"></script>
<script src="<?php $options->adminStaticUrl('js', 'pagedown.js?v=' . $suffixVersion); ?>"></script>
<script src="<?php $options->adminStaticUrl('js', 'pagedown-extra.js?v=' . $suffixVersion); ?>"></script>
<script src="<?php $options->adminStaticUrl('js', 'diff.js?v=' . $suffixVersion); ?>"></script>
@ -57,31 +58,18 @@ $(document).ready(function () {
help: '<?php _e('Markdown语法帮助'); ?>'
};
var converter = new Markdown.Converter(),
var converter = new HyperDown(),
editor = new Markdown.Editor(converter, '', options),
diffMatch = new diff_match_patch(), last = '', preview = $('#wmd-preview'),
mark = '@mark' + Math.ceil(Math.random() * 100000000) + '@',
span = '<span class="diff" />',
cache = {};
Markdown.Extra.init(converter, {
'extensions' : ["tables", "fenced_code_gfm", "def_list", "attr_list", "footnotes", "strikethrough", "newlines"]
});
// 自动跟随
converter.hooks.chain('postConversion', function (html) {
// clear special html tags
html = html.replace(/<\/?(\!doctype|html|head|body|link|title|input|select|button|textarea|style|noscript)[^>]*>/ig, function (all) {
return all.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/'/g, '&#039;')
.replace(/"/g, '&quot;');
});
// clear hard breaks
html = html.replace(/\s*((?:<br>\n)+)\s*(<\/?(?:p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|form|fieldset|iframe|hr|legend|article|section|nav|aside|hgroup|header|footer|figcaption|li|dd|dt)[^\w])/gm, '$2');
converter.hook('makeHtml', function (html) {
// convert all comment
html = html.replace(/&lt;!--(.+?)--&gt;/g, '<!--$1-->');
if (html.indexOf('<!--more-->') > 0) {
var parts = html.split(/\s*<\!\-\-more\-\->\s*/),
summary = parts.shift(),

969
admin/js/hyperdown.js Normal file
View File

@ -0,0 +1,969 @@
// Generated by CoffeeScript 1.12.1
(function() {
var Parser,
slice = [].slice;
Parser = (function() {
var array_keys, array_values, htmlspecialchars, preg_quote, str_replace, trim, ucfirst;
ucfirst = function(str) {
return (str.charAt(0)).toUpperCase() + str.substring(1);
};
preg_quote = function(str) {
return str.replace(/[-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
};
str_replace = function(search, replace, str) {
var i, j, l, len, len1, val;
if (search instanceof Array) {
if (replace instanceof Array) {
for (i = j = 0, len = search.length; j < len; i = ++j) {
val = search[i];
str = str_replace(val, replace[i], str);
}
} else {
for (l = 0, len1 = search.length; l < len1; l++) {
val = search[l];
str = str_replace(val, replace, str);
}
}
} else {
search = preg_quote(search);
str = str.replace(new RegExp(search, 'g'), replace);
}
return str;
};
htmlspecialchars = function(str) {
return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
};
trim = function(str, ch) {
var c, i, j, ref, search;
if (ch == null) {
ch = null;
}
if (ch != null) {
search = '';
for (i = j = 0, ref = ch.length - 1; 0 <= ref ? j <= ref : j >= ref; i = 0 <= ref ? ++j : --j) {
c = ch[i];
c = preg_quote(c);
search += c;
}
search = '[' + search + ']*';
return str.replace(new RegExp('^' + search), '').replace(new RegExp(search + '$'), '');
} else {
return str.replace(/^\s*/, '').replace(/\s*$/, '');
}
};
array_keys = function(arr) {
var _, j, k, len, result;
result = [];
if (arr instanceof Array) {
for (k = j = 0, len = arr.length; j < len; k = ++j) {
_ = arr[k];
result.push(k);
}
} else {
for (k in arr) {
result.push(k);
}
}
return result;
};
array_values = function(arr) {
var _, j, len, result, v;
result = [];
if (arr instanceof Array) {
for (j = 0, len = arr.length; j < len; j++) {
v = arr[j];
result.push(v);
}
} else {
for (_ in arr) {
v = arr[_];
result.push(v);
}
}
return result;
};
function Parser() {
this.commonWhiteList = 'kbd|b|i|strong|em|sup|sub|br|code|del|a|hr|small';
this.specialWhiteList = {
table: 'table|tbody|thead|tfoot|tr|td|th'
};
this.hooks = {};
}
Parser.prototype.makeHtml = function(text) {
var html;
this.footnotes = [];
this.definitions = {};
this.holders = {};
this.uniqid = (Math.ceil(Math.random() * 10000000)) + (Math.ceil(Math.random() * 10000000));
this.id = 0;
text = this.initText(text);
html = this.parse(text);
html = this.makeFootnotes(html);
return this.call('makeHtml', html);
};
Parser.prototype.hook = function(type, cb) {
if (this.hooks[type] == null) {
this.hooks[type] = [];
}
return this.hooks[type].push(cb);
};
Parser.prototype.makeHolder = function(str) {
var key;
key = "|\r" + this.uniqid + this.id + "\r|";
this.id += 1;
this.holders[key] = str;
return key;
};
Parser.prototype.initText = function(text) {
return text.replace(/\t/g, ' ').replace(/\r/g, '');
};
Parser.prototype.makeFootnotes = function(html) {
var index, val;
if (this.footnotes.length > 0) {
html += '<div class="footnotes"><hr><ol>';
index = 1;
while (val = this.footnotes.shift()) {
if (typeof val === 'string') {
val += " <a href=\"#fnref-" + index + "\" class=\"footnote-backref\">&#8617;</a>";
} else {
val[val.length - 1] += " <a href=\"#fnref-" + index + "\" class=\"footnote-backref\">&#8617;</a>";
val = val.length > 1 ? this.parse(val.join("\n")) : this.parseInline(val[0]);
}
html += "<li id=\"fn-" + index + "\">" + val + "</li>";
index += 1;
}
html += '</ol></div>';
}
return html;
};
Parser.prototype.parse = function(text) {
var block, blocks, end, extract, html, j, len, lines, method, result, start, type, value;
lines = [];
blocks = this.parseBlock(text, lines);
html = '';
for (j = 0, len = blocks.length; j < len; j++) {
block = blocks[j];
type = block[0], start = block[1], end = block[2], value = block[3];
extract = lines.slice(start, end + 1);
method = 'parse' + ucfirst(type);
extract = this.call('before' + ucfirst(method), extract, value);
result = this[method](extract, value);
result = this.call('after' + ucfirst(method), result, value);
html += result;
}
return html;
};
Parser.prototype.call = function() {
var args, callback, j, len, ref, type, value;
type = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
value = args[0];
if (this.hooks[type] == null) {
return value;
}
ref = this.hooks[type];
for (j = 0, len = ref.length; j < len; j++) {
callback = ref[j];
value = callback.apply(this, args);
args[0] = value;
}
return value;
};
Parser.prototype.releaseHolder = function(text, clearHolders) {
var deep;
if (clearHolders == null) {
clearHolders = true;
}
deep = 0;
while ((text.indexOf("\r")) >= 0 && deep < 10) {
text = str_replace(array_keys(this.holders), array_values(this.holders), text);
deep += 1;
}
if (clearHolders) {
this.holders = {};
}
return text;
};
Parser.prototype.parseInline = function(text, whiteList, clearHolders, enableAutoLink) {
if (whiteList == null) {
whiteList = '';
}
if (clearHolders == null) {
clearHolders = true;
}
if (enableAutoLink == null) {
enableAutoLink = true;
}
text = this.call('beforeParseInline', text);
text = text.replace(/(^|[^\\])(`+)(.+?)\2/mg, (function(_this) {
return function() {
var matches;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return matches[1] + _this.makeHolder('<code>' + (htmlspecialchars(matches[3])) + '</code>');
};
})(this));
text = text.replace(/\\(.)/g, (function(_this) {
return function() {
var escaped, matches;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
escaped = htmlspecialchars(matches[1]);
escaped = escaped.replace(/\$/g, '&dollar;');
return _this.makeHolder(escaped);
};
})(this));
text = text.replace(/<(https?:\/\/.+)>/ig, (function(_this) {
return function() {
var link, matches, url;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
url = _this.cleanUrl(matches[1]);
link = _this.call('parseLink', matches[1]);
return _this.makeHolder("<a href=\"" + url + "\">" + link + "</a>");
};
})(this));
text = text.replace(/<(\/?)([a-z0-9-]+)(\s+[^>]*)?>/ig, (function(_this) {
return function() {
var matches;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
if ((('|' + _this.commonWhiteList + '|' + whiteList + '|').indexOf('|' + matches[2].toLowerCase() + '|')) >= 0) {
return _this.makeHolder(matches[0]);
} else {
return htmlspecialchars(matches[0]);
}
};
})(this));
text = str_replace(['<', '>'], ['&lt;', '&gt;'], text);
text = text.replace(/\[\^((?:[^\]]|\\\]|\\\[)+?)\]/g, (function(_this) {
return function() {
var id, matches;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
id = _this.footnotes.indexOf(matches[1]);
if (id < 0) {
id = _this.footnotes.length + 1;
_this.footnotes.push(_this.parseInline(matches[1], '', false));
}
return _this.makeHolder("<sup id=\"fnref-" + id + "\"><a href=\"#fn-" + id + "\" class=\"footnote-ref\">" + id + "</a></sup>");
};
})(this));
text = text.replace(/!\[((?:[^\]]|\\\]|\\\[)*?)\]\(((?:[^\)]|\\\)|\\\()+?)\)/g, (function(_this) {
return function() {
var escaped, matches, url;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
escaped = _this.escapeBracket(matches[1]);
url = _this.escapeBracket(matches[2]);
url = _this.cleanUrl(url);
return _this.makeHolder("<img src=\"" + url + "\" alt=\"" + escaped + "\" title=\"" + escaped + "\">");
};
})(this));
text = text.replace(/!\[((?:[^\]]|\\\]|\\\[)*?)\]\[((?:[^\]]|\\\]|\\\[)+?)\]/g, (function(_this) {
return function() {
var escaped, matches, result;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
escaped = _this.escapeBracket(matches[1]);
result = _this.definitions[matches[2]] != null ? "<img src=\"" + _this.definitions[matches[2]] + "\" alt=\"" + escaped + "\" title=\"" + escaped + "\">" : escaped;
return _this.makeHolder(result);
};
})(this));
text = text.replace(/\[((?:[^\]]|\\\]|\\\[)+?)\]\(((?:[^\)]|\\\)|\\\()+?)\)/g, (function(_this) {
return function() {
var escaped, matches, url;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
escaped = _this.parseInline(_this.escapeBracket(matches[1]), '', false, false);
url = _this.escapeBracket(matches[2]);
url = _this.cleanUrl(url);
return _this.makeHolder("<a href=\"" + url + "\">" + escaped + "</a>");
};
})(this));
text = text.replace(/\[((?:[^\]]|\\\]|\\\[)+?)\]\[((?:[^\]]|\\\]|\\\[)+?)\]/g, (function(_this) {
return function() {
var escaped, matches, result;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
escaped = _this.parseInline(_this.escapeBracket(matches[1]), '', false, false);
result = _this.definitions[matches[2]] != null ? "<a href=\"" + _this.definitions[matches[2]] + "\">" + escaped + "</a>" : escaped;
return _this.makeHolder(result);
};
})(this));
text = this.parseInlineCallback(text);
text = text.replace(/<([_a-z0-9-\.\+]+@[^@]+\.[a-z]{2,})>/ig, '<a href="mailto:$1">$1</a>');
if (enableAutoLink) {
text = text.replace(/(^|[^"])((https?):[x80-xff_a-z0-9-\.\/%#!@\?\+=~\|\,&\(\)]+)($|[^"])/ig, (function(_this) {
return function() {
var link, matches;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
link = _this.call('parseLink', matches[2]);
return matches[1] + "<a href=\"" + matches[2] + "\">" + link + "</a>" + matches[4];
};
})(this));
}
text = this.call('afterParseInlineBeforeRelease', text);
text = this.releaseHolder(text, clearHolders);
text = this.call('afterParseInline', text);
return text;
};
Parser.prototype.parseInlineCallback = function(text) {
text = text.replace(/(\*{3})((?:.|\r)+?)\1/mg, (function(_this) {
return function() {
var matches;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return '<strong><em>' + (_this.parseInlineCallback(matches[2])) + '</em></strong>';
};
})(this));
text = text.replace(/(\*{2})((?:.|\r)+?)\1/mg, (function(_this) {
return function() {
var matches;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return '<strong>' + (_this.parseInlineCallback(matches[2])) + '</strong>';
};
})(this));
text = text.replace(/(\*)((?:.|\r)+?)\1/mg, (function(_this) {
return function() {
var matches;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return '<em>' + (_this.parseInlineCallback(matches[2])) + '</em>';
};
})(this));
text = text.replace(/(\s+|^)(_{3})((?:.|\r)+?)\2(\s+|$)/mg, (function(_this) {
return function() {
var matches;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return matches[1] + '<strong><em>' + (_this.parseInlineCallback(matches[3])) + '</em></strong>' + matches[4];
};
})(this));
text = text.replace(/(\s+|^)(_{2})((?:.|\r)+?)\2(\s+|$)/mg, (function(_this) {
return function() {
var matches;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return matches[1] + '<strong>' + (_this.parseInlineCallback(matches[3])) + '</strong>' + matches[4];
};
})(this));
text = text.replace(/(\s+|^)(_)((?:.|\r)+?)\2(\s+|$)/mg, (function(_this) {
return function() {
var matches;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return matches[1] + '<em>' + (_this.parseInlineCallback(matches[3])) + '</em>' + matches[4];
};
})(this));
text = text.replace(/(~{2})((?:.|\r)+?)\1/mg, (function(_this) {
return function() {
var matches;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return '<del>' + (_this.parseInlineCallback(matches[2])) + '</del>';
};
})(this));
return text;
};
Parser.prototype.parseBlock = function(text, lines) {
var align, aligns, block, emptyCount, head, isAfterList, j, key, l, len, len1, len2, line, m, matches, num, ref, row, rows, space, special, tag;
ref = text.split("\n");
for (j = 0, len = ref.length; j < len; j++) {
line = ref[j];
lines.push(line);
}
this.blocks = [];
this.current = 'normal';
this.pos = -1;
special = (array_keys(this.specialWhiteList)).join('|');
emptyCount = 0;
for (key = l = 0, len1 = lines.length; l < len1; key = ++l) {
line = lines[key];
block = this.getBlock();
if (block != null) {
block = block.slice(0);
}
if (!!(matches = line.match(/^(\s*)(~|`){3,}([^`~]*)$/i))) {
if (this.isBlock('code')) {
isAfterList = block[3][2];
if (isAfterList) {
this.combineBlock().setBlock(key);
} else {
(this.setBlock(key)).endBlock();
}
} else {
isAfterList = false;
if (this.isBlock('list')) {
space = block[3];
isAfterList = (space > 0 && matches[1].length >= space) || matches[1].length > space;
}
this.startBlock('code', key, [matches[1], matches[3], isAfterList]);
}
continue;
} else if (this.isBlock('code')) {
this.setBlock(key);
continue;
}
if (!!(matches = line.match(new RegExp("^\\s*<(" + special + ")(\\s+[^>]*)?>", 'i')))) {
tag = matches[1].toLowerCase();
if (!(this.isBlock('html', tag)) && !(this.isBlock('pre'))) {
this.startBlock('html', key, tag);
}
continue;
} else if (!!(matches = line.match(new RegExp("</(" + special + ")>\\s*$", 'i')))) {
tag = matches[1].toLowerCase();
if (this.isBlock('html', tag)) {
this.setBlock(key).endBlock();
}
continue;
} else if (this.isBlock('html')) {
this.setBlock(key);
continue;
}
switch (true) {
case !!(line.match(/^ {4}/)):
emptyCount = 0;
if ((this.isBlock('pre')) || this.isBlock('list')) {
this.setBlock(key);
} else {
this.startBlock('pre', key);
}
break;
case !!(matches = line.match(/^(\s*)((?:[0-9a-z]+\.)|\-|\+|\*)\s+/)):
space = matches[1].length;
emptyCount = 0;
if (this.isBlock('list')) {
this.setBlock(key, space);
} else {
this.startBlock('list', key, space);
}
break;
case !!(matches = line.match(/^\[\^((?:[^\]]|\]|\[)+?)\]:/)):
space = matches[0].length - 1;
this.startBlock('footnote', key, [space, matches[1]]);
break;
case !!(matches = line.match(/^\s*\[((?:[^\]]|\]|\[)+?)\]:\s*(.+)$/)):
this.definitions[matches[1]] = this.cleanUrl(matches[2]);
this.startBlock('definition', key).endBlock();
break;
case !!(line.match(/^\s*>/)):
if (this.isBlock('quote')) {
this.setBlock(key);
} else {
this.startBlock('quote', key);
}
break;
case !!(matches = line.match(/^((?:(?:(?:[ :]*\-[ :]*)+(?:\||\+))|(?:(?:\||\+)(?:[ :]*\-[ :]*)+)|(?:(?:[ :]*\-[ :]*)+(?:\||\+)(?:[ :]*\-[ :]*)+))+)$/)):
if (this.isBlock('table')) {
block[3][0].push(block[3][2]);
block[3][2] += 1;
this.setBlock(key, block[3]);
} else {
head = 0;
if ((block == null) || block[0] !== 'normal' || lines[block[2]].match(/^\s*$/)) {
this.startBlock('table', key);
} else {
head = 1;
this.backBlock(1, 'table');
}
if (matches[1][0] === '|') {
matches[1] = matches[1].substring(1);
if (matches[1][matches[1].length - 1] === '|') {
matches[1] = matches[1].substring(0, matches[1].length - 1);
}
}
rows = matches[1].split(/\+|\|/);
aligns = [];
for (m = 0, len2 = rows.length; m < len2; m++) {
row = rows[m];
align = 'none';
if (!!(matches = row.match(/^\s*(:?)\-+(:?)\s*$/))) {
if (!!matches[1] && !!matches[2]) {
align = 'center';
} else if (!!matches[1]) {
align = 'left';
} else if (!!matches[2]) {
align = 'right';
}
}
aligns.push(align);
}
this.setBlock(key, [[head], aligns, head + 1]);
}
break;
case !!(matches = line.match(/^(#+)(.*)$/)):
num = Math.min(matches[1].length, 6);
this.startBlock('sh', key, num).endBlock();
break;
case !!(matches = line.match(/^\s*((=|-){2,})\s*$/)) && ((block != null) && block[0] === 'normal' && !lines[block[2]].match(/^\s*$/)):
if (this.isBlock('normal')) {
this.backBlock(1, 'mh', matches[1][0] === '=' ? 1 : 2).setBlock(key).endBlock();
} else {
this.startBlock('normal', key);
}
break;
case !!(line.match(/^[-\*]{3,}\s*$/)):
this.startBlock('hr', key).endBlock();
break;
default:
if (this.isBlock('list')) {
if (line.match(/^(\s*)/)) {
if (emptyCount > 0) {
this.startBlock('normal', key);
} else {
this.setBlock(key);
}
emptyCount += 1;
} else if (emptyCount === 0) {
this.setBlock(key);
} else {
this.startBlock('normal', key);
}
} else if (this.isBlock('footnote')) {
matches = line.match(/^(\s*)/);
if (matches[1].length >= block[3][0]) {
this.setBlock(key);
} else {
this.startBlock('normal', key);
}
} else if (this.isBlock('table')) {
if (0 <= line.indexOf('|')) {
block[3][2] += 1;
this.setBlock(key, block[3]);
} else {
this.startBlock('normal', key);
}
} else if (this.isBlock('pre')) {
if (line.match(/^\s*$/)) {
if (emptyCount > 0) {
this.startBlock('normal', key);
} else {
this.setBlock(key);
}
emptyCount += 1;
} else {
this.startBlock('normal', key);
}
} else if (this.isBlock('quote')) {
if (line.match(/^(\s*)/)) {
if (emptyCount > 0) {
this.startBlock('normal', key);
} else {
this.setBlock(key);
}
emptyCount += 1;
} else if (emptyCount === 0) {
this.setBlock(key);
} else {
this.startBlock('normal', key);
}
} else {
if ((block == null) || block[0] !== 'normal') {
this.startBlock('normal', key);
} else {
this.setBlock(key);
}
}
}
}
return this.optimizeBlocks(this.blocks, lines);
};
Parser.prototype.optimizeBlocks = function(_blocks, _lines) {
var block, blocks, from, isEmpty, key, lines, moved, nextBlock, prevBlock, to, type, types;
blocks = _blocks.slice(0);
lines = _lines.slice(0);
blocks = this.call('beforeOptimizeBlocks', blocks, lines);
key = 0;
while (blocks[key] != null) {
moved = false;
block = blocks[key];
prevBlock = blocks[key - 1] != null ? blocks[key - 1] : null;
nextBlock = blocks[key + 1] != null ? blocks[key + 1] : null;
type = block[0], from = block[1], to = block[2];
if ('pre' === type) {
isEmpty = lines.reduce(function(result, line) {
return (line.match(/^\s*$/)) && result;
}, true);
if (isEmpty) {
block[0] = type = 'normal';
}
}
if ('normal' === type) {
types = ['list', 'quote'];
if (from === to && (lines[from].match(/^\s*$/)) && (prevBlock != null) && (nextBlock != null)) {
if (prevBlock[0] === nextBlock[0] && (types.indexOf(prevBlock[0])) >= 0) {
blocks[key - 1] = [prevBlock[0], prevBlock[1], nextBlock[2], null];
blocks.splice(key, 2);
moved = true;
}
}
}
if (!moved) {
key += 1;
}
}
return this.call('afterOptimizeBlocks', blocks, lines);
};
Parser.prototype.parseCode = function(lines, parts) {
var blank, count, lang, rel, str;
blank = parts[0], lang = parts[1];
lang = trim(lang);
count = blank.length;
if (!lang.match(/^[_a-z0-9-\+\#\:\.]+$/i)) {
lang = null;
} else {
parts = lang.split(':');
if (parts.length > 1) {
lang = parts[0], rel = parts[1];
lang = trim(lang);
rel = trim(rel);
}
}
lines = lines.slice(1, -1).map(function(line) {
return line.replace(new RegExp("/^[ ]{" + count + "}/"), '');
});
str = lines.join("\n");
if (str.match(/^\s*$/)) {
return '';
} else {
return '<pre><code' + (!!lang ? " class=\"" + lang + "\"" : '') + (!!rel ? " rel=\"" + rel + "\"" : '') + '>' + (htmlspecialchars(str)) + '</code></pre>';
}
};
Parser.prototype.parsePre = function(lines) {
var str;
lines = lines.map(function(line) {
return htmlspecialchars(line.substring(4));
});
str = lines.join("\n");
if (str.match(/^\s*$/)) {
return '';
} else {
return '<pre><code>' + str + '</code></pre>';
}
};
Parser.prototype.parseSh = function(lines, num) {
var line;
line = this.parseInline(trim(lines[0], '# '));
if (line.match(/^\s*$/)) {
return '';
} else {
return "<h" + num + ">" + line + "</h" + num + ">";
}
};
Parser.prototype.parseMh = function(lines, num) {
return this.parseSh(lines, num);
};
Parser.prototype.parseQuote = function(lines) {
var str;
lines = lines.map(function(line) {
return line.replace(/^\s*> ?/, '');
});
str = lines.join("\n");
if (str.match(/^\s*$/)) {
return '';
} else {
return '<blockquote>' + (this.parse(str)) + '</blockquote>';
}
};
Parser.prototype.parseList = function(lines) {
var found, html, j, key, l, lastType, leftLines, len, len1, len2, line, m, matches, minSpace, row, rows, secondMinSpace, space, text, type;
html = '';
minSpace = 99999;
rows = [];
for (key = j = 0, len = lines.length; j < len; key = ++j) {
line = lines[key];
if (matches = line.match(/^(\s*)((?:[0-9a-z]+\.?)|\-|\+|\*)(\s+)(.*)$/)) {
space = matches[1].length;
type = 0 <= '+-*'.indexOf(matches[2]) ? 'ul' : 'ol';
minSpace = Math.min(space, minSpace);
rows.push([space, type, line, matches[4]]);
} else {
rows.push(line);
}
}
found = false;
secondMinSpace = 99999;
for (l = 0, len1 = rows.length; l < len1; l++) {
row = rows[l];
if (row instanceof Array && row[0] !== minSpace) {
secondMinSpace = Math.min(secondMinSpace, row[0]);
found = true;
}
}
secondMinSpace = found ? secondMinSpace : minSpace;
lastType = '';
leftLines = [];
for (m = 0, len2 = rows.length; m < len2; m++) {
row = rows[m];
if (row instanceof Array) {
space = row[0], type = row[1], line = row[2], text = row[3];
if (space !== minSpace) {
leftLines.push(line.replace(new RegExp("^\\s{" + secondMinSpace + "}"), ''));
} else {
if (leftLines.length > 0) {
html += '<li>' + (this.parse(leftLines.join("\n"))) + '</li>';
}
if (lastType !== type) {
if (!!lastType) {
html += "</" + lastType + ">";
}
html += "<" + type + ">";
}
leftLines = [text];
lastType = type;
}
} else {
leftLines.push(row.replace(new RegExp("^\\s{" + secondMinSpace + "}"), ''));
}
}
if (leftLines.length > 0) {
html += '<li>' + (this.parse(leftLines.join("\n"))) + ("</li></" + lastType + ">");
}
return html;
};
Parser.prototype.parseTable = function(lines, value) {
var aligns, body, column, columns, head, html, ignores, j, key, l, last, len, len1, line, num, output, row, rows, tag, text;
ignores = value[0], aligns = value[1];
head = ignores.length > 0 && (ignores.reduce(function(prev, curr) {
return curr + prev;
})) > 0;
html = '<table>';
body = head ? null : true;
output = false;
for (key = j = 0, len = lines.length; j < len; key = ++j) {
line = lines[key];
if (0 <= ignores.indexOf(key)) {
if (head && output) {
head = false;
body = true;
}
continue;
}
line = trim(line);
output = true;
if (line[0] === '|') {
line = line.substring(1);
if (line[line.length - 1] === '|') {
line = line.substring(0, line.length - 1);
}
}
rows = line.split('|').map(function(row) {
if (row.match(/^\s+$/)) {
return '';
} else {
return trim(row);
}
});
columns = {};
last = -1;
for (l = 0, len1 = rows.length; l < len1; l++) {
row = rows[l];
if (row.length > 0) {
last += 1;
columns[last] = [(columns[last] != null ? columns[last][0] + 1 : 1), row];
} else if (columns[last] != null) {
columns[last][0] += 1;
} else {
columns[0] = [1, row];
}
}
if (head) {
html += '<thead>';
} else if (body) {
html += '<tbody>';
}
html += '<tr>';
for (key in columns) {
column = columns[key];
num = column[0], text = column[1];
tag = head ? 'th' : 'td';
html += "<" + tag;
if (num > 1) {
html += " colspan=\"" + num + "\"";
}
if ((aligns[key] != null) && aligns[key] !== 'none') {
html += " align=\"" + aligns[key] + "\"";
}
html += '>' + (this.parseInline(text)) + ("</" + tag + ">");
}
html += '</tr>';
if (head) {
html += '</thead>';
} else if (body) {
body = false;
}
}
if (body !== null) {
html += '</tbody>';
}
return html += '</table>';
};
Parser.prototype.parseHr = function() {
return '<hr>';
};
Parser.prototype.parseNormal = function(lines) {
var str;
lines = lines.map((function(_this) {
return function(line) {
return _this.parseInline(line);
};
})(this));
str = trim(lines.join("\n"));
str = str.replace(/(\n\s*){2,}/g, '</p><p>');
str = str.replace(/\n/g, '<br>');
if (str.match(/^\s*$/)) {
return '';
} else {
return "<p>" + str + "</p>";
}
};
Parser.prototype.parseFootnote = function(lines, value) {
var index, note, space;
space = value[0], note = value[1];
index = this.footnotes.indexOf(note);
if (index >= 0) {
lines = lines.slice(0);
lines[0] = lines[0].replace(/^\[\^((?:[^\]]|\]|\[)+?)\]:/, '');
this.footnotes[index] = lines;
}
return '';
};
Parser.prototype.parseDefinition = function() {
return '';
};
Parser.prototype.parseHtml = function(lines, type) {
lines = lines.map((function(_this) {
return function(line) {
return _this.parseInline(line, _this.specialWhiteList[type] != null ? _this.specialWhiteList[type] : '');
};
})(this));
return lines.join("\n");
};
Parser.prototype.cleanUrl = function(url) {
var matches;
if (!!(matches = url.match(/^\s*((http|https|ftp|mailto):[x80-xff_a-z0-9-\.\/%#@\?\+=~\|\,&\(\)]+)/i))) {
matches[1];
}
if (!!(matches = url.match(/^\s*([x80-xff_a-z0-9-\.\/%#@\?\+=~\|\,&]+)/i))) {
return matches[1];
} else {
return '#';
}
};
Parser.prototype.escapeBracket = function(str) {
return str_replace(['\\[', '\\]', '\\(', '\\)'], ['[', ']', '(', ')'], str);
};
Parser.prototype.startBlock = function(type, start, value) {
if (value == null) {
value = null;
}
this.pos += 1;
this.current = type;
this.blocks.push([type, start, start, value]);
return this;
};
Parser.prototype.endBlock = function() {
this.current = 'normal';
return this;
};
Parser.prototype.isBlock = function(type, value) {
if (value == null) {
value = null;
}
return this.current === type && (null === value ? true : this.blocks[this.pos][3] === value);
};
Parser.prototype.getBlock = function() {
if (this.blocks[this.pos] != null) {
return this.blocks[this.pos];
} else {
return null;
}
};
Parser.prototype.setBlock = function(to, value) {
if (to == null) {
to = null;
}
if (value == null) {
value = null;
}
if (to !== null) {
this.blocks[this.pos][2] = to;
}
if (value !== null) {
this.blocks[this.pos][3] = value;
}
return this;
};
Parser.prototype.backBlock = function(step, type, value) {
var item, last;
if (value == null) {
value = null;
}
if (this.pos < 0) {
return this.startBlock(type, 0, value);
}
last = this.blocks[this.pos][2];
this.blocks[this.pos][2] = last - step;
item = [type, last - step + 1, last, value];
if (this.blocks[this.pos][1] <= this.blocks[this.pos][2]) {
this.pos += 1;
this.blocks.push(item);
} else {
this.blocks[this.pos] = item;
}
this.current = type;
return this;
};
Parser.prototype.combineBlock = function() {
var current, prev;
if (this.pos < 1) {
return this;
}
prev = this.blocks[this.pos - 1].slice(0);
current = this.blocks[this.pos].slice(0);
prev[2] = current[2];
this.blocks[this.pos - 1] = prev;
this.current = prev[0];
this.blocks = this.blocks.slice(0, -1);
this.pos -= 1;
return this;
};
return Parser;
})();
if (typeof module !== "undefined" && module !== null) {
module.exports = Parser;
} else if (typeof window !== "undefined" && window !== null) {
window.HyperDown = Parser;
}
}).call(this);

View File

@ -126,6 +126,8 @@ function _p($adapter) {
switch ($adapter) {
case 'Mysql':
return Typecho_Db_Adapter_Mysql::isAvailable();
case 'Mysqli':
return Typecho_Db_Adapter_Mysqli::isAvailable();
case 'Pdo_Mysql':
return Typecho_Db_Adapter_Pdo_Mysql::isAvailable();
case 'SQLite':
@ -444,15 +446,16 @@ Typecho_Cookie::set('__typecho_lang', $lang);
<?php endif;?>
<?php elseif (isset($_GET['config'])): ?>
<?php
$adapters = array('Mysql', 'Pdo_Mysql', 'SQLite', 'Pdo_SQLite', 'Pgsql', 'Pdo_Pgsql');
$adapters = array('Mysql', 'Mysqli', 'Pdo_Mysql', 'SQLite', 'Pdo_SQLite', 'Pgsql', 'Pdo_Pgsql');
foreach ($adapters as $firstAdapter) {
if (_p($firstAdapter)) {
break;
}
}
$adapter = _r('dbAdapter', $firstAdapter);
$type = explode('_', $adapter);
$type = array_pop($type);
list ($type) = explode('_', $adapter);
$type = $type == 'Mysqli' ? 'Mysql' : $type;
?>
<form method="post" action="?config" name="config">
<h1 class="typecho-install-title"><?php _e('确认您的配置'); ?></h1>

View File

@ -1,7 +1,7 @@
<?php
/**
* HyperDown
* Parser
*
* @copyright Copyright (c) 2012 SegmentFault Team. (http://segmentfault.com)
* @author Joyqi <joyqi@segmentfault.com>
@ -14,7 +14,7 @@ class HyperDown
*
* @var string
*/
private $_commonWhiteList = 'kbd|b|i|strong|em|sup|sub|br|code|del|a|hr|small';
public $_commonWhiteList = 'kbd|b|i|strong|em|sup|sub|br|code|del|a|hr|small';
/**
* _specialWhiteList
@ -23,7 +23,7 @@ class HyperDown
* @access private
*/
private $_specialWhiteList = array(
'table' => 'table|tbody|thead|tfoot|tr|td|th'
'table' => 'table|tbody|thead|tfoot|tr|td|th'
);
/**
@ -31,7 +31,7 @@ class HyperDown
*
* @var array
*/
private $_footnotes;
public $_footnotes;
/**
* _blocks
@ -59,7 +59,7 @@ class HyperDown
*
* @var array
*/
private $_definitions;
public $_definitions;
/**
* @var array
@ -97,7 +97,9 @@ class HyperDown
$text = $this->initText($text);
$html = $this->parse($text);
return $this->makeFootnotes($html);
$html = $this->makeFootnotes($html);
return $this->call('makeHtml', $html);
}
/**
@ -115,7 +117,7 @@ class HyperDown
*/
public function makeHolder($str)
{
$key = "|\r" . $this->_uniqid . $this->_id . "\r|";
$key = "\r" . $this->_uniqid . $this->_id . "\r";
$this->_id ++;
$this->_holders[$key] = $str;
@ -128,7 +130,7 @@ class HyperDown
*/
private function initText($text)
{
$text = str_replace(array("\t", "\r"), array(' ', ''), $text);
$text = str_replace(array("\t", "\r"), array(' ', ''), $text);
return $text;
}
@ -216,7 +218,7 @@ class HyperDown
private function releaseHolder($text, $clearHolders = true)
{
$deep = 0;
while (strpos($text, "|\r") !== false && $deep < 10) {
while (strpos($text, "\r") !== false && $deep < 10) {
$text = str_replace(array_keys($this->_holders), array_values($this->_holders), $text);
$deep ++;
}
@ -234,91 +236,161 @@ class HyperDown
* @param string $text
* @param string $whiteList
* @param bool $clearHolders
* @param bool $enableAutoLink
* @return string
*/
private function parseInline($text, $whiteList = '', $clearHolders = true)
public function parseInline($text, $whiteList = '', $clearHolders = true, $enableAutoLink = true)
{
$text = $this->call('beforeParseInline', $text);
$self = $this;
$text = $this->call('beforeParseInline', $text);
// code
$text = preg_replace_callback("/(^|[^\\\])(`+)(.+?)\\2/", function ($matches) {
return $matches[1] . $this->makeHolder('<code>' . htmlspecialchars($matches[3]) . '</code>');
}, $text);
// link
$text = preg_replace_callback("/<(https?:\/\/.+)>/i", function ($matches) {
return $this->makeHolder("<a href=\"{$matches[1]}\">{$matches[1]}</a>");
}, $text);
// encode unsafe tags
$text = preg_replace_callback("/<(\/?)([a-z0-9-]+)(\s+[^>]*)?>/i", function ($matches) use ($whiteList) {
if (stripos($this->_commonWhiteList . '|' . $whiteList, $matches[2]) !== false) {
return $this->makeHolder($matches[0]);
} else {
return htmlspecialchars($matches[0]);
}
}, $text);
$text = str_replace(array('<', '>'), array('&lt;', '&gt;'), $text);
// footnote
$text = preg_replace_callback("/\[\^((?:[^\]]|\\]|\\[)+?)\]/", function ($matches) {
$id = array_search($matches[1], $this->_footnotes);
if (false === $id) {
$id = count($this->_footnotes) + 1;
$this->_footnotes[$id] = $this->parseInline($matches[1], '', false);
}
return $this->makeHolder("<sup id=\"fnref-{$id}\"><a href=\"#fn-{$id}\" class=\"footnote-ref\">{$id}</a></sup>");
}, $text);
// image
$text = preg_replace_callback("/!\[((?:[^\]]|\\]|\\[)*?)\]\(((?:[^\)]|\\)|\\()+?)\)/", function ($matches) {
$escaped = $this->escapeBracket($matches[1]);
$url = $this->escapeBracket($matches[2]);
return $this->makeHolder("<img src=\"{$url}\" alt=\"{$escaped}\" title=\"{$escaped}\">");
}, $text);
$text = preg_replace_callback("/!\[((?:[^\]]|\\]|\\[)*?)\]\[((?:[^\]]|\\]|\\[)+?)\]/", function ($matches) {
$escaped = $this->escapeBracket($matches[1]);
$result = isset($this->_definitions[$matches[2]]) ?
"<img src=\"{$this->_definitions[$matches[2]]}\" alt=\"{$escaped}\" title=\"{$escaped}\">"
: $escaped;
return $this->makeHolder($result);
}, $text);
// link
$text = preg_replace_callback("/\[((?:[^\]]|\\]|\\[)+?)\]\(((?:[^\)]|\\)|\\()+?)\)/", function ($matches) {
$escaped = $this->parseInline($this->escapeBracket($matches[1]), '', false);
$url = $this->escapeBracket($matches[2]);
return $this->makeHolder("<a href=\"{$url}\">{$escaped}</a>");
}, $text);
$text = preg_replace_callback("/\[((?:[^\]]|\\]|\\[)+?)\]\[((?:[^\]]|\\]|\\[)+?)\]/", function ($matches) {
$escaped = $this->parseInline($this->escapeBracket($matches[1]), '', false);
$result = isset($this->_definitions[$matches[2]]) ?
"<a href=\"{$this->_definitions[$matches[2]]}\">{$escaped}</a>"
: $escaped;
return $this->makeHolder($result);
}, $text);
$text = preg_replace_callback(
"/(^|[^\\\])(`+)(.+?)\\2/",
function ($matches) use ($self) {
return $matches[1] . $self->makeHolder(
'<code>' . htmlspecialchars($matches[3]) . '</code>'
);
},
$text
);
// escape
$text = preg_replace_callback("/\\\(`|\*|_|~)/", function ($matches) {
return $this->makeHolder(htmlspecialchars($matches[1]));
}, $text);
$text = preg_replace_callback(
"/\\\(.)/u",
function ($matches) use ($self) {
$escaped = htmlspecialchars($matches[1]);
$escaped = str_replace('$', '&dollar;', $escaped);
return $self->makeHolder($escaped);
},
$text
);
// link
$text = preg_replace_callback(
"/<(https?:\/\/.+)>/i",
function ($matches) use ($self) {
$url = $self->cleanUrl($matches[1]);
$link = $self->call('parseLink', $matches[1]);
return $self->makeHolder(
"<a href=\"{$url}\">{$link}</a>"
);
},
$text
);
// encode unsafe tags
$text = preg_replace_callback(
"/<(\/?)([a-z0-9-]+)(\s+[^>]*)?>/i",
function ($matches) use ($self, $whiteList) {
if (false !== stripos(
'|' . $self->_commonWhiteList . '|' . $whiteList . '|', '|' . $matches[2] . '|'
)) {
return $self->makeHolder($matches[0]);
} else {
return htmlspecialchars($matches[0]);
}
},
$text
);
$text = str_replace(array('<', '>'), array('&lt;', '&gt;'), $text);
// footnote
$text = preg_replace_callback(
"/\[\^((?:[^\]]|\\\\\]|\\\\\[)+?)\]/",
function ($matches) use ($self) {
$id = array_search($matches[1], $self->_footnotes);
if (false === $id) {
$id = count($self->_footnotes) + 1;
$self->_footnotes[$id] = $self->parseInline($matches[1], '', false);
}
return $self->makeHolder(
"<sup id=\"fnref-{$id}\"><a href=\"#fn-{$id}\" class=\"footnote-ref\">{$id}</a></sup>"
);
},
$text
);
// image
$text = preg_replace_callback(
"/!\[((?:[^\]]|\\\\\]|\\\\\[)*?)\]\(((?:[^\)]|\\\\\)|\\\\\()+?)\)/",
function ($matches) use ($self) {
$escaped = $self->escapeBracket($matches[1]);
$url = $self->escapeBracket($matches[2]);
$url = $self->cleanUrl($url);
return $self->makeHolder(
"<img src=\"{$url}\" alt=\"{$escaped}\" title=\"{$escaped}\">"
);
},
$text
);
$text = preg_replace_callback(
"/!\[((?:[^\]]|\\\\\]|\\\\\[)*?)\]\[((?:[^\]]|\\\\\]|\\\\\[)+?)\]/",
function ($matches) use ($self) {
$escaped = $self->escapeBracket($matches[1]);
$result = isset( $self->_definitions[$matches[2]] ) ?
"<img src=\"{$self->_definitions[$matches[2]]}\" alt=\"{$escaped}\" title=\"{$escaped}\">"
: $escaped;
return $self->makeHolder($result);
},
$text
);
// link
$text = preg_replace_callback(
"/\[((?:[^\]]|\\\\\]|\\\\\[)+?)\]\(((?:[^\)]|\\\\\)|\\\\\()+?)\)/",
function ($matches) use ($self) {
$escaped = $self->parseInline(
$self->escapeBracket($matches[1]), '', false, false
);
$url = $self->escapeBracket($matches[2]);
$url = $self->cleanUrl($url);
return $self->makeHolder("<a href=\"{$url}\">{$escaped}</a>");
},
$text
);
$text = preg_replace_callback(
"/\[((?:[^\]]|\\\\\]|\\\\\[)+?)\]\[((?:[^\]]|\\\\\]|\\\\\[)+?)\]/",
function ($matches) use ($self) {
$escaped = $self->parseInline(
$self->escapeBracket($matches[1]), '', false
);
$result = isset( $self->_definitions[$matches[2]] ) ?
"<a href=\"{$self->_definitions[$matches[2]]}\">{$escaped}</a>"
: $escaped;
return $self->makeHolder($result);
},
$text
);
// strong and em and some fuck
$text = $this->parseInlineCallback($text);
$text = preg_replace("/<([_a-z0-9-\.\+]+@[^@]+\.[a-z]{2,})>/i", "<a href=\"mailto:\\1\">\\1</a>", $text);
$text = preg_replace(
"/<([_a-z0-9-\.\+]+@[^@]+\.[a-z]{2,})>/i",
"<a href=\"mailto:\\1\">\\1</a>",
$text
);
// autolink url
$text = preg_replace("/(^|[^\"])((http|https|ftp|mailto):[_a-z0-9-\.\/%#@\?\+=~\|\,&\(\)]+)($|[^\"])/i",
"\\1<a href=\"\\2\">\\2</a>\\4", $text);
if ($enableAutoLink) {
$text = preg_replace_callback(
"/(^|[^\"])((https?):[x80-xff_a-z0-9-\.\/%#@\?\+=~\|\,&\(\)]+)($|[^\"])/i",
function ($matches) use ($self) {
$link = $self->call('parseLink', $matches[2]);
return "{$matches[1]}<a href=\"{$matches[2]}\">{$link}</a>{$matches[4]}";
},
$text
);
}
$text = $this->call('afterParseInlineBeforeRelease', $text);
$text = $this->releaseHolder($text, $clearHolders);
@ -332,35 +404,79 @@ class HyperDown
* @param $text
* @return mixed
*/
private function parseInlineCallback($text)
public function parseInlineCallback($text)
{
$text = preg_replace_callback("/(\*{3})(.+?)\\1/", function ($matches) {
return '<strong><em>' . $this->parseInlineCallback($matches[2]) . '</em></strong>';
}, $text);
$self = $this;
$text = preg_replace_callback("/(\*{2})(.+?)\\1/", function ($matches) {
return '<strong>' . $this->parseInlineCallback($matches[2]) . '</strong>';
}, $text);
$text = preg_replace_callback(
"/(\*{3})(.+?)\\1/",
function ($matches) use ($self) {
return '<strong><em>' .
$self->parseInlineCallback($matches[2]) .
'</em></strong>';
},
$text
);
$text = preg_replace_callback("/(\*)(.+?)\\1/", function ($matches) {
return '<em>' . $this->parseInlineCallback($matches[2]) . '</em>';
}, $text);
$text = preg_replace_callback(
"/(\*{2})(.+?)\\1/",
function ($matches) use ($self) {
return '<strong>' .
$self->parseInlineCallback($matches[2]) .
'</strong>';
},
$text
);
$text = preg_replace_callback("/(\s+|^)(_{3})(.+?)\\2(\s+|$)/", function ($matches) {
return $matches[1] . '<strong><em>' . $this->parseInlineCallback($matches[3]) . '</em></strong>' . $matches[4];
}, $text);
$text = preg_replace_callback(
"/(\*)(.+?)\\1/",
function ($matches) use ($self) {
return '<em>' .
$self->parseInlineCallback($matches[2]) .
'</em>';
},
$text
);
$text = preg_replace_callback("/(\s+|^)(_{2})(.+?)\\2(\s+|$)/", function ($matches) {
return $matches[1] . '<strong>' . $this->parseInlineCallback($matches[3]) . '</strong>' . $matches[4];
}, $text);
$text = preg_replace_callback(
"/(\s+|^)(_{3})(.+?)\\2(\s+|$)/",
function ($matches) use ($self) {
return $matches[1] . '<strong><em>' .
$self->parseInlineCallback($matches[3]) .
'</em></strong>' . $matches[4];
},
$text
);
$text = preg_replace_callback("/(\s+|^)(_)(.+?)\\2(\s+|$)/", function ($matches) {
return $matches[1] . '<em>' . $this->parseInlineCallback($matches[3]) . '</em>' . $matches[4];
}, $text);
$text = preg_replace_callback(
"/(\s+|^)(_{2})(.+?)\\2(\s+|$)/",
function ($matches) use ($self) {
return $matches[1] . '<strong>' .
$self->parseInlineCallback($matches[3]) .
'</strong>' . $matches[4];
},
$text
);
$text = preg_replace_callback("/(~{2})(.+?)\\1/", function ($matches) {
return '<del>' . $this->parseInlineCallback($matches[2]) . '</del>';
}, $text);
$text = preg_replace_callback(
"/(\s+|^)(_)(.+?)\\2(\s+|$)/",
function ($matches) use ($self) {
return $matches[1] . '<em>' .
$self->parseInlineCallback($matches[3]) .
'</em>' . $matches[4];
},
$text
);
$text = preg_replace_callback(
"/(~{2})(.+?)\\1/",
function ($matches) use ($self) {
return '<del>' .
$self->parseInlineCallback($matches[2]) .
'</del>';
},
$text
);
return $text;
}
@ -384,7 +500,7 @@ class HyperDown
// analyze by line
foreach ($lines as $key => $line) {
$block = $this->getBlock();
// code block is special
if (preg_match("/^(\s*)(~|`){3,}([^`~]*)$/i", $line, $matches)) {
if ($this->isBlock('code')) {
@ -407,7 +523,9 @@ class HyperDown
|| strlen($matches[1]) > $space;
}
$this->startBlock('code', $key, array($matches[1], $matches[3], $isAfterList));
$this->startBlock('code', $key, array(
$matches[1], $matches[3], $isAfterList
));
}
continue;
@ -416,19 +534,6 @@ class HyperDown
continue;
}
// pre block
if (preg_match("/^ {4}/", $line)) {
$emptyCount = 0;
if ($this->isBlock('pre') || $this->isBlock('list')) {
$this->setBlock($key);
continue;
} else if ($this->isBlock('normal')) {
$this->startBlock('pre', $key);
continue;
}
}
// html block is special too
if (preg_match("/^\s*<({$special})(\s+[^>]*)?>/i", $line, $matches)) {
$tag = strtolower($matches[1]);
@ -452,6 +557,17 @@ class HyperDown
}
switch (true) {
// pre block
case preg_match("/^ {4}/", $line):
$emptyCount = 0;
if ($this->isBlock('pre') || $this->isBlock('list')) {
$this->setBlock($key);
} else if ($this->isBlock('normal')) {
$this->startBlock('pre', $key);
}
break;
// list
case preg_match("/^(\s*)((?:[0-9a-z]+\.)|\-|\+|\*)\s+/", $line, $matches):
$space = strlen($matches[1]);
@ -463,17 +579,19 @@ class HyperDown
} else {
$this->startBlock('list', $key, $space);
}
break;
break;
// footnote
case preg_match("/^\[\^((?:[^\]]|\\]|\\[)+?)\]:/", $line, $matches):
$space = strlen($matches[0]) - 1;
$this->startBlock('footnote', $key, array($space, $matches[1]));
$this->startBlock('footnote', $key, array(
$space, $matches[1]
));
break;
// definition
case preg_match("/^\s*\[((?:[^\]]|\\]|\\[)+?)\]:\s*(.+)$/", $line, $matches):
$this->_definitions[$matches[1]] = $matches[2];
$this->_definitions[$matches[1]] = $this->cleanUrl($matches[2]);
$this->startBlock('definition', $key)
->endBlock();
break;
@ -489,15 +607,19 @@ class HyperDown
// table
case preg_match("/^((?:(?:(?:[ :]*\-[ :]*)+(?:\||\+))|(?:(?:\||\+)(?:[ :]*\-[ :]*)+)|(?:(?:[ :]*\-[ :]*)+(?:\||\+)(?:[ :]*\-[ :]*)+))+)$/", $line, $matches):
if ($this->isBlock('normal')) {
$head = false;
if ($this->isBlock('table')) {
$block[3][0][] = $block[3][2];
$block[3][2] ++;
$this->setBlock($key, $block[3]);
} else {
$head = 0;
if (empty($block) ||
$block[0] != 'normal' ||
preg_match("/^\s*$/", $lines[$block[2]])) {
$this->startBlock('table', $key);
} else {
$head = true;
$head = 1;
$this->backBlock(1, 'table');
}
@ -527,7 +649,7 @@ class HyperDown
$aligns[] = $align;
}
$this->setBlock($key, array($head, $aligns));
$this->setBlock($key, array(array($head), $aligns, $head + 1));
}
break;
@ -581,7 +703,8 @@ class HyperDown
}
} else if ($this->isBlock('table')) {
if (false !== strpos($line, '|')) {
$this->setBlock($key);
$block[3][2] ++;
$this->setBlock($key, $block[3]);
} else {
$this->startBlock('normal', $key);
}
@ -634,7 +757,11 @@ class HyperDown
{
$blocks = $this->call('beforeOptimizeBlocks', $blocks, $lines);
foreach ($blocks as $key => &$block) {
$key = 0;
while (isset($blocks[$key])) {
$moved = false;
$block = &$blocks[$key];
$prevBlock = isset($blocks[$key - 1]) ? $blocks[$key - 1] : NULL;
$nextBlock = isset($blocks[$key + 1]) ? $blocks[$key + 1] : NULL;
@ -658,11 +785,20 @@ class HyperDown
&& !empty($prevBlock) && !empty($nextBlock)) {
if ($prevBlock[0] == $nextBlock[0] && in_array($prevBlock[0], $types)) {
// combine 3 blocks
$blocks[$key - 1] = array($prevBlock[0], $prevBlock[1], $nextBlock[2], NULL);
$blocks[$key - 1] = array(
$prevBlock[0], $prevBlock[1], $nextBlock[2], NULL
);
array_splice($blocks, $key, 2);
// do not move
$moved = true;
}
}
}
if (!$moved) {
$key ++;
}
}
return $this->call('afterOptimizeBlocks', $blocks, $lines);
@ -681,8 +817,15 @@ class HyperDown
$lang = trim($lang);
$count = strlen($blank);
if (!preg_match("/^[_a-z0-9-\+\#]+$/i", $lang)) {
if (! preg_match("/^[_a-z0-9-\+\#\:\.]+$/i", $lang)) {
$lang = NULL;
} else {
$parts = explode(':', $lang);
if (count($parts) > 1) {
list ($lang, $rel) = $parts;
$lang = trim($lang);
$rel = trim($rel);
}
}
$lines = array_map(function ($line) use ($count) {
@ -691,8 +834,9 @@ class HyperDown
$str = implode("\n", $lines);
return preg_match("/^\s*$/", $str) ? '' :
'<pre><code' . (!empty($lang) ? " class=\"{$lang}\"" : '') . '>'
. htmlspecialchars(implode("\n", $lines)) . '</code></pre>';
'<pre><code' . (!empty($lang) ? " class=\"{$lang}\"" : '')
. (!empty($rel) ? " rel=\"{$rel}\"" : '') . '>'
. htmlspecialchars($str) . '</code></pre>';
}
/**
@ -733,8 +877,7 @@ class HyperDown
*/
private function parseMh(array $lines, $num)
{
$line = $this->parseInline(trim($lines[0], '# '));
return preg_match("/^\s*$/", $line) ? '' : "<h{$num}>{$line}</h{$num}>";
return $this->parseSh($lines, $num);
}
/**
@ -786,7 +929,7 @@ class HyperDown
$found = true;
}
}
$secondMinSpace = $found ?: $minSpace;
$secondMinSpace = $found ? $secondMinSpace : $minSpace;
$lastType = '';
$leftLines = array();
@ -832,21 +975,24 @@ class HyperDown
*/
private function parseTable(array $lines, array $value)
{
list ($head, $aligns) = $value;
$ignore = $head ? 1 : 0;
list ($ignores, $aligns) = $value;
$head = count($ignores) > 0 && array_sum($ignores) > 0;
$html = '<table>';
$body = NULL;
$body = $head ? NULL : true;
$output = false;
foreach ($lines as $key => $line) {
if ($key == $ignore) {
$head = false;
$body = true;
if (in_array($key, $ignores)) {
if ($head && $output) {
$head = false;
$body = true;
}
continue;
}
$line = trim($line);
$output = true;
if ($line[0] == '|') {
$line = substr($line, 1);
@ -870,7 +1016,9 @@ class HyperDown
foreach ($rows as $row) {
if (strlen($row) > 0) {
$last ++;
$columns[$last] = array(isset($columns[$last]) ? $columns[$last][0] + 1 : 1, $row);
$columns[$last] = array(
isset($columns[$last]) ? $columns[$last][0] + 1 : 1, $row
);
} else if (isset($columns[$last])) {
$columns[$last][0] ++;
} else {
@ -995,13 +1143,30 @@ class HyperDown
return implode("\n", $lines);
}
/**
* @param $url
* @return string
*/
public function cleanUrl($url)
{
if (preg_match("/^\s*((http|https|ftp|mailto):[x80-xff_a-z0-9-\.\/%#@\?\+=~\|\,&\(\)]+)/i", $url, $matches)) {
return $matches[1];
} else if (preg_match("/^\s*([x80-xff_a-z0-9-\.\/%#@\?\+=~\|\,&]+)/i", $url, $matches)) {
return $matches[1];
} else {
return '#';
}
}
/**
* @param $str
* @return mixed
*/
private function escapeBracket($str)
public function escapeBracket($str)
{
return str_replace(array('\[', '\]', '\(', '\)'), array('[', ']', '(', ')'), $str);
return str_replace(
array('\[', '\]', '\(', '\)'), array('[', ']', '(', ')'), $str
);
}
/**
@ -1098,7 +1263,9 @@ class HyperDown
}
$this->_current = $type;
$this->_blocks[$this->_pos] = array($type, $last - $step + 1, $last, $value);
$this->_blocks[$this->_pos] = array(
$type, $last - $step + 1, $last, $value
);
return $this;
}

View File

@ -414,7 +414,7 @@ EOF;
{
return !empty($_SERVER['HTTP_APPNAME']) // SAE
|| !!getenv('HTTP_BAE_ENV_APPID') // BAE
|| !!getenv('HTTP_BAE_LOGID') // BAE 3.0
|| !!getenv('SERVER_SOFTWARE') // BAE 3.0
|| (ini_get('acl.app_id') && class_exists('Alibaba')) // ACE
|| (isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'],'Google App Engine') !== false) // GAE
;

View File

@ -0,0 +1,169 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* Typecho Blog Platform
*
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
* @version $Id: Mysqli.php 103 2008-04-09 16:22:43Z magike.net $
*/
/**
* 数据库Mysqli适配器
*
* @package Db
*/
class Typecho_Db_Adapter_Mysqli implements Typecho_Db_Adapter
{
/**
* 数据库连接字符串标示
*
* @access private
* @var resource
*/
private $_dbLink;
/**
* 判断适配器是否可用
*
* @access public
* @return boolean
*/
public static function isAvailable()
{
return function_exists('mysqli_connect');
}
/**
* 数据库连接函数
*
* @param Typecho_Config $config 数据库配置
* @throws Typecho_Db_Exception
* @return resource
*/
public function connect(Typecho_Config $config)
{
if ($this->_dbLink = @new MySQLi($config->host, $config->user, $config->password, $config->database, (empty($config->port) ? '' : $config->port))) {
if ($config->charset) {
$this->_dbLink->query("SET NAMES '{$config->charset}'");
}
return $this->_dbLink;
}
/** 数据库异常 */
throw new Typecho_Db_Adapter_Exception(@$this->_dbLink->error);
}
/**
* 执行数据库查询
*
* @param string $query 数据库查询SQL字符串
* @param mixed $handle 连接对象
* @param integer $op 数据库读写状态
* @param string $action 数据库动作
* @throws Typecho_Db_Exception
* @return resource
*/
public function query($query, $handle, $op = Typecho_Db::READ, $action = NULL)
{
if ($resource = @$this->_dbLink->query($query instanceof Typecho_Db_Query ? $query->__toString() : $query)) {
return $resource;
}
/** 数据库异常 */
throw new Typecho_Db_Query_Exception($this->_dbLink->error, $this->_dbLink->errno);
}
/**
* 将数据查询的其中一行作为数组取出,其中字段名对应数组键值
*
* @param resource $resource 查询返回资源标识
* @return array
*/
public function fetch($resource)
{
return $resource->fetch_assoc();
}
/**
* 将数据查询的其中一行作为对象取出,其中字段名对应对象属性
*
* @param resource $resource 查询的资源数据
* @return object
*/
public function fetchObject($resource)
{
return $resource->fetch_object();
}
/**
* 引号转义函数
*
* @param string $string 需要转义的字符串
* @return string
*/
public function quoteValue($string)
{
return '\'' . str_replace(array('\'', '\\'), array('\'\'', '\\\\'), $string) . '\'';
}
/**
* 对象引号过滤
*
* @access public
* @param string $string
* @return string
*/
public function quoteColumn($string)
{
return '`' . $string . '`';
}
/**
* 合成查询语句
*
* @access public
* @param array $sql 查询对象词法数组
* @return string
*/
public function parseSelect(array $sql)
{
if (!empty($sql['join'])) {
foreach ($sql['join'] as $val) {
list($table, $condition, $op) = $val;
$sql['table'] = "{$sql['table']} {$op} JOIN {$table} ON {$condition}";
}
}
$sql['limit'] = (0 == strlen($sql['limit'])) ? NULL : ' LIMIT ' . $sql['limit'];
$sql['offset'] = (0 == strlen($sql['offset'])) ? NULL : ' OFFSET ' . $sql['offset'];
return 'SELECT ' . $sql['fields'] . ' FROM ' . $sql['table'] .
$sql['where'] . $sql['group'] . $sql['having'] . $sql['order'] . $sql['limit'] . $sql['offset'];
}
/**
* 取出最后一次查询影响的行数
*
* @param resource $resource 查询的资源数据
* @param mixed $handle 连接对象
* @return integer
*/
public function affectedRows($resource, $handle)
{
return $this->_dbLink->affected_rows;
}
/*y
* 取出最后一次插入返回的主键值
*
* @param resource $resource 查询的资源数据
* @param mixed $handle 连接对象
* @return integer
*/
public function lastInsertId($resource, $handle)
{
return $this->_dbLink->insert_id;
}
}