4203 lines
219 KiB
JavaScript
4203 lines
219 KiB
JavaScript
|
/**
|
|||
|
markdeep.js
|
|||
|
Version 0.23
|
|||
|
|
|||
|
Copyright 2015-2017, Morgan McGuire, http://casual-effects.com
|
|||
|
All rights reserved.
|
|||
|
|
|||
|
-------------------------------------------------------------
|
|||
|
|
|||
|
See http://casual-effects.com/markdeep for documentation on how to
|
|||
|
use this script make your plain text documents render beautifully
|
|||
|
in web browsers.
|
|||
|
|
|||
|
Markdeep was created by Morgan McGuire. It extends the work of:
|
|||
|
|
|||
|
- John Gruber's original Markdown
|
|||
|
- Ben Hollis' Maruku Markdown dialect
|
|||
|
- Michel Fortin's Markdown Extras dialect
|
|||
|
- Ivan Sagalaev's highlight.js
|
|||
|
- Contributors to the above open source projects
|
|||
|
|
|||
|
-------------------------------------------------------------
|
|||
|
|
|||
|
You may use, extend, and redistribute this code under the terms of
|
|||
|
the BSD license at https://opensource.org/licenses/BSD-2-Clause.
|
|||
|
|
|||
|
Contains highlight.js (https://github.com/isagalaev/highlight.js) by Ivan
|
|||
|
Sagalaev, which is used for code highlighting. (BSD 3-clause license)
|
|||
|
*/
|
|||
|
/**See http://casual-effects.com/markdeep for @license and documentation.
|
|||
|
markdeep.min.js 0.23 (C) 2017 Morgan McGuire
|
|||
|
highlight.min.js 9.5.0 (C) 2016 Ivan Sagalaev https://highlightjs.org/*/
|
|||
|
(function() {
|
|||
|
'use strict';
|
|||
|
|
|||
|
var MARKDEEP_FOOTER = '<div class="markdeepFooter"><i>formatted by <a href="http://casual-effects.com/markdeep" style="color:#999">Markdeep 0.23 </a></i><div style="display:inline-block;font-size:13px;font-family:\'Times New Roman\',serif;vertical-align:middle;transform:translate(-3px,-1px)rotate(135deg);">✒</div></div>';
|
|||
|
|
|||
|
// For minification. This is admittedly scary.
|
|||
|
var _ = String.prototype;
|
|||
|
_.rp = _.replace;
|
|||
|
_.ss = _.substring;
|
|||
|
|
|||
|
// Regular expression version of String.indexOf
|
|||
|
_.regexIndexOf = function(regex, startpos) {
|
|||
|
var i = this.ss(startpos || 0).search(regex);
|
|||
|
return (i >= 0) ? (i + (startpos || 0)) : i;
|
|||
|
}
|
|||
|
|
|||
|
/** Enable for debugging to view character bounds in diagrams */
|
|||
|
var DEBUG_SHOW_GRID = false;
|
|||
|
|
|||
|
/** Overlay the non-empty characters of the original source in diagrams */
|
|||
|
var DEBUG_SHOW_SOURCE = DEBUG_SHOW_GRID;
|
|||
|
|
|||
|
/** Use to suppress passing through text in diagrams */
|
|||
|
var DEBUG_HIDE_PASSTHROUGH = DEBUG_SHOW_SOURCE;
|
|||
|
|
|||
|
/** In pixels of lines in diagrams */
|
|||
|
var STROKE_WIDTH = 2;
|
|||
|
|
|||
|
/** A box of these denotes a diagram */
|
|||
|
var DIAGRAM_MARKER = '*';
|
|||
|
|
|||
|
// http://stackoverflow.com/questions/1877475/repeat-character-n-times
|
|||
|
// ECMAScript 6 has a String.repeat method, but that's not available everywhere
|
|||
|
var DIAGRAM_START = Array(5 + 1).join(DIAGRAM_MARKER);
|
|||
|
|
|||
|
/** attribs are optional */
|
|||
|
function entag(tag, content, attribs) {
|
|||
|
return '<' + tag + (attribs ? ' ' + attribs : '') + '>' + content + '</' + tag + '>';
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
function measureFontSize(fontStack) {
|
|||
|
try {
|
|||
|
var canvas = document.createElement('canvas');
|
|||
|
var ctx = canvas.getContext('2d');
|
|||
|
ctx.font = '10pt ' + fontStack;
|
|||
|
return ctx.measureText("M").width;
|
|||
|
} catch (e) {
|
|||
|
// Needed for Firefox include...iframe canvas doesn't work for some reason
|
|||
|
return 10;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/*! highlight.js v9.12.0 | BSD3 License | git.io/hljslicense */
|
|||
|
!function(e){var n="object"==typeof window&&window||"object"==typeof self&&self;"undefined"!=typeof exports?e(exports):n&&(n.hljs=e({}),"function"==typeof define&&define.amd&&define([],function(){return n.hljs}))}(function(e){function n(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function t(e){return e.nodeName.toLowerCase()}function r(e,n){var t=e&&e.exec(n);return t&&0===t.index}function a(e){return k.test(e)}function i(e){var n,t,r,i,o=e.className+" ";if(o+=e.parentNode?e.parentNode.className:"",t=B.exec(o))return w(t[1])?t[1]:"no-highlight";for(o=o.split(/\s+/),n=0,r=o.length;r>n;n++)if(i=o[n],a(i)||w(i))return i}function o(e){var n,t={},r=Array.prototype.slice.call(arguments,1);for(n in e)t[n]=e[n];return r.forEach(function(e){for(n in e)t[n]=e[n]}),t}function u(e){var n=[];return function r(e,a){for(var i=e.firstChild;i;i=i.nextSibling)3===i.nodeType?a+=i.nodeValue.length:1===i.nodeType&&(n.push({event:"start",offset:a,node:i}),a=r(i,a),t(i).match(/br|hr|img|input/)||n.push({event:"stop",offset:a,node:i}));return a}(e,0),n}function c(e,r,a){function i(){return e.length&&r.length?e[0].offset!==r[0].offset?e[0].offset<r[0].offset?e:r:"start"===r[0].event?e:r:e.length?e:r}function o(e){function r(e){return" "+e.nodeName+'="'+n(e.value).replace('"',""")+'"'}s+="<"+t(e)+E.map.call(e.attributes,r).join("")+">"}function u(e){s+="</"+t(e)+">"}function c(e){("start"===e.event?o:u)(e.node)}for(var l=0,s="",f=[];e.length||r.length;){var g=i();if(s+=n(a.substring(l,g[0].offset)),l=g[0].offset,g===e){f.reverse().forEach(u);do c(g.splice(0,1)[0]),g=i();while(g===e&&g.length&&g[0].offset===l);f.reverse().forEach(o)}else"start"===g[0].event?f.push(g[0].node):f.pop(),c(g.splice(0,1)[0])}return s+n(a.substr(l))}function l(e){return e.v&&!e.cached_variants&&(e.cached_variants=e.v.map(function(n){return o(e,{v:null},n)})),e.cached_variants||e.eW&&[o(e)]||[e]}function s(e){function n(e){return e&&e.source||e}function t(t,r){return new RegExp(n(t),"m"+(e.cI?"i":"")+(r?"g":""))}function r(a,i){if(!a.compiled){if(a.compiled=!0,a.k=a.k||a.bK,a.k){var o={},u=function(n,t){e.cI&&(t=t.toLowerCase()),t.split(" ").forEach(function(e){var t=e.split("|");o[t[0]]=[n,t[1]?Number(t[1]):1]})};"string"==typeof a.k?u("keyword",a.k):x(a.k).forEach(function(e){u(e,a.k[e])}),a.k=o}a.lR=t(a.l||/\w+/,!0),i&&(a.bK&&(a.b="\\b("+a.bK.split(" ").join("|")+")\\b"),a.b||(a.b=/\B|\b/),a.bR=t(a.b),a.e||a.eW||(a.e=/\B|\b/),a.e&&(a.eR=t(a.e)),a.tE=n(a.e)||"",a.eW&&i.tE&&(a.tE+=(a.e?"|":"")+i.tE)),a.i&&(a.iR=t(a.i)),null==a.r&&(a.r=1),a.c||(a.c=[]),a.c=Array.prototype.concat.apply([],a.c.map(function(e){return l("self"===e?a:e)})),a.c.forEach(function(e){r(e,a)}),a.starts&&r(a.starts,i);var c=a.c.map(function(e){return e.bK?"\\.?("+e.b+")\\.?":e.b}).concat([a.tE,a.i]).map(n).filter(Boolean);a.t=c.length?t(c.join("|"),!0):{exec:function(){return null}}}}r(e)}function f(e,t,a,i){function o(e,n){var t,a;for(t=0,a=n.c.length;a>t;t++)if(r(n.c[t].bR,e))return n.c[t]}function u(e,n){if(r(e.eR,n)){for(;e.endsParent&&e.parent;)e=e.parent;return e}return e.eW?u(e.parent,n):void 0}function c(e,n){return!a&&r(n.iR,e)}function l(e,n){var t=N.cI?n[0].toLowerCase():n[0];return e.k.hasOwnProperty(t)&&e.k[t]}function p(e,n,t,r){var a=r?"":I.classPrefix,i='<span class="'+a,o=t?"":C;return i+=e+'">',i+n+o}function h(){var e,t,r,a;if(!E.k)return n(k);for(a="",t=0,E.lR.lastIndex=0,r=E.lR.exec(k);r;)a+=n(k.substring(t,r.index)),e=l(E,r),e?(B+=e[1],a+=p(e[0],n(r[0]))):a+=n(r[0]),t=E.lR.lastIndex,r=E.lR.exec(k);return a+n(k.substr(t))}function d(){var e="string"==typeof E.sL;if(e&&!y[E.sL])return n(k);var t=e?f(E.sL,k,!0,x[E.sL]):g(k,E.sL.length?E.sL:void 0);return E.r>0&&(B+=t.r),e&&(x[E.sL]=t.top),p(t.language,t.value,!1,!0)}function b(){L+=null!=E.sL?d():h(),k=""}function v(e){L+=e.cN?p(e.cN,"",!0):"",E=Object.create(e,{parent:{value:E}})}function m(e,n){if(k+=e,null==n)return b(),0;var t=o(n,E);if(t)return t.skip?k+=n:(t.eB&&(k+=n),b(),t.rB||t.eB||(k=n)),v(t,n),t.rB?0:n.length;var r=u(E,n);if(r){var a=E;a.skip?k+=n:(a.rE||a.eE||(k+=n),b(),a.eE&&(k=n)
|
|||
|
// To reduce script size in the minified file, I cut these keywords from less-frequently used languages:
|
|||
|
//abort absolute acc acce accep accept access accessed accessible account acos action activate add addtime admin administer advanced advise aes_decrypt aes_encrypt after agent aggregate ali alia alias allocate allow alter always analyze ancillary and any anydata anydataset anyschema anytype apply archive archived archivelog are as asc ascii asin assembly assertion associate asynchronous at atan atn2 attr attri attrib attribu attribut attribute attributes audit authenticated authentication authid authors auto autoallocate autodblink autoextend automatic availability avg backup badfile basicfile before begin beginning benchmark between bfile bfile_base big bigfile bin binary_double binary_float binlog bit_and bit_count bit_length bit_or bit_xor bitmap blob_base block blocksize body both bound buffer_cache buffer_pool build bulk by byte byteordermark bytes c cache caching call calling cancel capacity cascade cascaded case cast catalog category ceil ceiling chain change changed char_base char_length character_length characters characterset charindex charset charsetform charsetid check checksum checksum_agg child choose chr chunk class cleanup clear client clob clob_base clone close cluster_id cluster_probability cluster_set clustering coalesce coercibility col collate collation collect colu colum column column_value columns columns_updated comment commit compact compatibility compiled complete composite_limit compound compress compute concat concat_ws concurrent confirm conn connec connect connect_by_iscycle connect_by_isleaf connect_by_root connect_time connection consider consistent constant constraint constraints constructor container content contents context contributors controlfile conv convert convert_tz corr corr_k corr_s corresponding corruption cos cost count count_big counted covar_pop covar_samp cpu_per_call cpu_per_session crc32 create creation critical cross cube cume_dist curdate current current_date current_time current_timestamp current_user cursor curtime customdatum cycle d data database databases datafile datafiles datalength date_add date_cache date_format date_sub dateadd datediff datefromparts datename datepart datetime2fromparts day day_to_second dayname dayofmonth dayofweek dayofyear days db_role_change dbtimezone ddl deallocate declare decode decompose decrement decrypt deduplicate def defa defau defaul default defaults deferred defi defin define degrees delayed delegate delete delete_all delimited demand dense_rank depth dequeue des_decrypt des_encrypt des_key_file desc descr descri describ describe descriptor deterministic diagnostics difference dimension direct_load directory disable disable_all disallow disassociate discardfile disconnect diskgroup distinct distinctrow distribute distributed div do document domain dotnet double downgrade drop dumpfile duplicate duration e each edition editionable editions element ellipsis else elsif elt empty enable enable_all enclosed encode encoding encrypt end end-exec endian enforced engine engines enqueue enterprise entityescaping eomonth error errors escaped evalname evaluate event eventdata events except exception exceptions exchange exclude excluding execu execut execute exempt exists exit exp expire explain export export_set extended extent external external_1 external_2 externally extract f failed failed_login_attempts failover failure far fast feature_set feature_value fetch field fields file file_name_convert filesystem_like_logging final finish first first_value fixed flash_cache flashback floor flush following follows for forall force form forma format found found_rows freelist freelists freepools fresh from from_base64 from_days ftp full function g general generated get get_format get_lock getdate getutcdate global global_name globally go goto grant grants greatest group group_concat group_id grouping grouping_id groups gtid_subtract guarantee guard handler hash hashkeys having hea head headi headin heading heap help hex hierarchy high high_priority hosts hour http i id ident_current ident_incr ident_seed identified identity idle_time if ifnull ig
|
|||
|
|
|||
|
|
|||
|
// Lucida Console on Windows has capital V's that look like lower case, so don't use it
|
|||
|
var codeFontStack = "Menlo,Consolas,monospace";
|
|||
|
var codeFontSize = 105.1316178 / measureFontSize(codeFontStack) + 'px';
|
|||
|
|
|||
|
var BODY_STYLESHEET = entag('style', 'body{max-width:680px;' +
|
|||
|
'margin:auto;' +
|
|||
|
'padding:20px;' +
|
|||
|
'text-align:justify;' +
|
|||
|
'line-height:140%; ' +
|
|||
|
'-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;font-smoothing:antialiased;' +
|
|||
|
'color:#222;' +
|
|||
|
'font-family:Palatino,Georgia,"Times New Roman",serif}');
|
|||
|
|
|||
|
/** You can embed your own stylesheet AFTER the <script> tags in your
|
|||
|
file to override these defaults. */
|
|||
|
var STYLESHEET = entag('style',
|
|||
|
'body{' +
|
|||
|
'counter-reset: h1 h2 h3 h4 h5 h6 paragraph' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md code,pre{' +
|
|||
|
'font-family:' + codeFontStack + ';' +
|
|||
|
'font-size:' + codeFontSize + ';' +
|
|||
|
'line-height:140%' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md div.title{' +
|
|||
|
'font-size:26px;' +
|
|||
|
'font-weight:800;' +
|
|||
|
'line-height:120%;' +
|
|||
|
'text-align:center' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md div.afterTitles{height:10px}' +
|
|||
|
|
|||
|
'.md div.subtitle{' +
|
|||
|
'text-align:center' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .image{display:inline-block}' +
|
|||
|
'.md div.imagecaption,.md div.tablecaption,.md div.listingcaption{' +
|
|||
|
'margin:5px 5px 5px 5px;' +
|
|||
|
'text-align: justify;' +
|
|||
|
'font-style:italic' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md div.imagecaption{' +
|
|||
|
'margin-bottom:0' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md img{' +
|
|||
|
'max-width:100%;' +
|
|||
|
'page-break-inside:avoid' +
|
|||
|
'}' +
|
|||
|
|
|||
|
// Justification tends to handle URLs and code blocks poorly
|
|||
|
// when inside of a bullet, so disable it there
|
|||
|
'.md li{text-align:left};' +
|
|||
|
|
|||
|
// Force captions on line listings down close and then center them
|
|||
|
'.md div.tilde{' +
|
|||
|
'margin:20px 0 -10px;' +
|
|||
|
'text-align:center' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md blockquote.fancyquote{' +
|
|||
|
'margin:25px 0 25px;' +
|
|||
|
'text-align:left;' +
|
|||
|
'line-height:160%' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md blockquote.fancyquote::before{' +
|
|||
|
'content:"\u201C";' +
|
|||
|
'color:#DDD;' +
|
|||
|
'font-family:Times New Roman;' +
|
|||
|
'font-size:45px;' +
|
|||
|
'line-height:0;' +
|
|||
|
'margin-right:6px;' +
|
|||
|
'vertical-align:-0.3em' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md span.fancyquote{' +
|
|||
|
'font-size:118%;' +
|
|||
|
'color:#777;' +
|
|||
|
'font-style:italic' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md span.fancyquote::after{' +
|
|||
|
'content:"\u201D";' +
|
|||
|
'font-style:normal;' +
|
|||
|
'color:#DDD;' +
|
|||
|
'font-family:Times New Roman;' +
|
|||
|
'font-size:45px;' +
|
|||
|
'line-height:0;' +
|
|||
|
'margin-left:6px;' +
|
|||
|
'vertical-align:-0.3em' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md blockquote.fancyquote .author{' +
|
|||
|
'width:100%;' +
|
|||
|
'margin-top:10px;' +
|
|||
|
'display:inline-block;' +
|
|||
|
'text-align:right' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md small{font-size:60%}' +
|
|||
|
|
|||
|
'.md div.title,contents,.md .tocHeader,h1,h2,h3,h4,h5,h6,.md .shortTOC,.md .mediumTOC,.nonumberh1,.nonumberh2,.nonumberh3,.nonumberh4,.nonumberh5,.nonumberh6{' +
|
|||
|
'font-family:Verdana,Helvetica,Arial,sans-serif;' +
|
|||
|
'margin:13.4px 0 13.4px;' +
|
|||
|
'padding:15px 0 3px;' +
|
|||
|
'border-top:none;' +
|
|||
|
'clear:both' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md h1,.md h2,.md h3,.md h4,.md h5,.md h6,.md .nonumberh1,.md .nonumberh2,.md .nonumberh3,.md .nonumberh4,.md .nonumberh5,.md .nonumberh6{' +
|
|||
|
'page-break-after:avoid;break-after:avoid' +
|
|||
|
'}'+
|
|||
|
|
|||
|
'.md svg.diagram{' +
|
|||
|
'display:block;' +
|
|||
|
'font-family:' + codeFontStack + ';' +
|
|||
|
'font-size:' + codeFontSize + ';' +
|
|||
|
'text-align:center;' +
|
|||
|
'stroke-linecap:round;' +
|
|||
|
'stroke-width:' + STROKE_WIDTH + 'px;'+
|
|||
|
'page-break-inside:avoid;' +
|
|||
|
'stroke:#000;' +
|
|||
|
'fill:#000' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md svg.diagram .opendot{' +
|
|||
|
'fill:#FFF' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md svg.diagram text{' +
|
|||
|
'stroke:none' +
|
|||
|
'}' +
|
|||
|
|
|||
|
// pagebreak hr
|
|||
|
'@media print{.md .pagebreak{page-break-after:always;visibility:hidden}}' +
|
|||
|
|
|||
|
// Not restricted to a:link because we want things like svn URLs to have this font, which
|
|||
|
// makes "//" look better.
|
|||
|
'.md a{font-family:Georgia,Palatino,\'Times New Roman\'}' +
|
|||
|
|
|||
|
'.md h1,.md .tocHeader,.md .nonumberh1{' +
|
|||
|
'border-bottom:3px solid;' +
|
|||
|
'font-size:20px;' +
|
|||
|
'font-weight:bold;' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md h1,.md .nonumberh1{' +
|
|||
|
'counter-reset: h2 h3 h4 h5 h6' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md h2,.md .nonumberh2{' +
|
|||
|
'counter-reset: h3 h4 h5 h6;' +
|
|||
|
'border-bottom:2px solid #999;' +
|
|||
|
'color:#555;' +
|
|||
|
'font-weight:bold;'+
|
|||
|
'font-size:18px;' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md h3,.md h4,.md h5,.md h6,.md .nonumberh3,.md .nonumberh4,.md .nonumberh5,.md .nonumberh6{' +
|
|||
|
'font-family:Helvetica,Arial,sans-serif;' +
|
|||
|
'color:#555;' +
|
|||
|
'font-size:16px;' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md h3{counter-reset:h4 h5 h6}' +
|
|||
|
'.md h4{counter-reset:h5 h6}' +
|
|||
|
'.md h5{counter-reset:h6}' +
|
|||
|
|
|||
|
'.md div.table{' +
|
|||
|
'margin:16px 0 16px 0' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md table{' +
|
|||
|
'font-size:9px;'+
|
|||
|
'border-collapse:collapse;' +
|
|||
|
'line-height:140%;' +
|
|||
|
'page-break-inside:avoid' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md table.table{' +
|
|||
|
'margin:auto' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md table.calendar{' +
|
|||
|
'width:100%;' +
|
|||
|
'margin:auto;' +
|
|||
|
'font-size:11px;' +
|
|||
|
'font-family:Helvetica,Arial,sans-serif' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md table.calendar th{' +
|
|||
|
'font-size:16px' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .today{' +
|
|||
|
'background:#ECF8FA' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .calendar .parenthesized{' +
|
|||
|
'color:#999;' +
|
|||
|
'font-style:italic' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md div.tablecaption{' +
|
|||
|
'text-align:center' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md table.table th{' +
|
|||
|
'color:#FFF;' +
|
|||
|
'background-color:#AAA;' +
|
|||
|
'border:1px solid #888;' +
|
|||
|
// top right bottom left
|
|||
|
'padding:8px 15px 8px 15px' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md table.table td{' +
|
|||
|
// top right bottom left
|
|||
|
'padding:5px 15px 5px 15px;' +
|
|||
|
'border:1px solid #888' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md table.table tr:nth-child(even){'+
|
|||
|
'background:#EEE' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md pre.tilde{' +
|
|||
|
'border-top: 1px solid #CCC;' +
|
|||
|
'border-bottom: 1px solid #CCC;' +
|
|||
|
'padding: 5px 0 5px 20px;' +
|
|||
|
'margin:0 0 0 0;' +
|
|||
|
'background:#FCFCFC;' +
|
|||
|
'page-break-inside:avoid' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md a.target{width:0px;height:0px;visibility:hidden;font-size:0px;display:inline-block}' +
|
|||
|
'.md a:link, .md a:visited{color:#38A;text-decoration:none}' +
|
|||
|
'.md a:link:hover{text-decoration:underline}' +
|
|||
|
|
|||
|
'.md dt{' +
|
|||
|
'font-weight:700' +
|
|||
|
'}' +
|
|||
|
|
|||
|
// Remove excess space above definitions due to paragraph breaks, and add some at the bottom
|
|||
|
'.md dl>dd{margin-top:-8px; margin-bottom:8px}' +
|
|||
|
|
|||
|
// Extra space around terse definition lists
|
|||
|
'.md dl>table{' +
|
|||
|
'margin:35px 0 30px' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md code{' +
|
|||
|
'white-space:pre;' +
|
|||
|
'page-break-inside:avoid' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .endnote{' +
|
|||
|
'font-size:13px;' +
|
|||
|
'line-height:15px;' +
|
|||
|
'padding-left:10px;' +
|
|||
|
'text-indent:-10px' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .bib{' +
|
|||
|
'padding-left:80px;' +
|
|||
|
'text-indent:-80px;' +
|
|||
|
'text-align:left' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.markdeepFooter{font-size:9px;text-align:right;padding-top:80px;color:#999}' +
|
|||
|
|
|||
|
'.md .mediumTOC{float:right;font-size:12px;line-height:15px;border-left:1px solid #CCC;padding-left:15px;margin:15px 0px 15px 25px}' +
|
|||
|
|
|||
|
'.md .mediumTOC .level1{font-weight:600}' +
|
|||
|
|
|||
|
'.md .longTOC .level1{font-weight:600;display:block;padding-top:12px;margin:0 0 -20px}' +
|
|||
|
|
|||
|
'.md .shortTOC{text-align:center;font-weight:bold;margin-top:15px;font-size:14px}' +
|
|||
|
|
|||
|
'.md .admonition{' +
|
|||
|
'position:relative;' +
|
|||
|
'margin:1em 0;' +
|
|||
|
'padding:.4rem 1rem;' +
|
|||
|
'border-radius:.2rem;' +
|
|||
|
'border-left:2.5rem solid rgba(68,138,255,.4);' +
|
|||
|
'background-color:rgba(68,138,255,.15);' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .admonition-title{' +
|
|||
|
'font-weight:bold;' +
|
|||
|
'border-bottom:solid 1px rgba(68,138,255,.4);' +
|
|||
|
'padding-bottom:4px;' +
|
|||
|
'margin-bottom:4px;' +
|
|||
|
'margin-left: -1rem;' +
|
|||
|
'padding-left:1rem;' +
|
|||
|
'margin-right:-1rem;' +
|
|||
|
'border-color:rgba(68,138,255,.4)' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .admonition.tip{' +
|
|||
|
'border-left:2.5rem solid rgba(50,255,90,.4);' +
|
|||
|
'background-color:rgba(50,255,90,.15)' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .admonition.tip::before{' +
|
|||
|
'content:"\\24d8";' +
|
|||
|
'font-weight:bold;' +
|
|||
|
'font-size:150%;' +
|
|||
|
'position:relative;' +
|
|||
|
'top:3px;' +
|
|||
|
'color:rgba(26,128,46,.8);' +
|
|||
|
'left:-2.95rem;' +
|
|||
|
'display:block;' +
|
|||
|
'width:0;' +
|
|||
|
'height:0' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .admonition.tip>.admonition-title{' +
|
|||
|
'border-color:rgba(50,255,90,.4)' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .admonition.warn,.md .admonition.warning{' +
|
|||
|
'border-left:2.5rem solid rgba(255,145,0,.4);' +
|
|||
|
'background-color:rgba(255,145,0,.15)' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .admonition.warn::before,.md .admonition.warning::before{' +
|
|||
|
'content:"\\26A0";' +
|
|||
|
'font-weight:bold;' +
|
|||
|
'font-size:150%;' +
|
|||
|
'position:relative;' +
|
|||
|
'top:2px;' +
|
|||
|
'color:rgba(128,73,0,.8);' +
|
|||
|
'left:-2.95rem;' +
|
|||
|
'display:block;' +
|
|||
|
'width:0;' +
|
|||
|
'height:0' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .admonition.warn>.admonition-title{' +
|
|||
|
'border-color:rgba(255,145,0,.4)' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .admonition.error{' +
|
|||
|
'border-left: 2.5rem solid rgba(255,23,68,.4);'+
|
|||
|
'background-color:rgba(255,23,68,.15)' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .admonition.error>.admonition-title{' +
|
|||
|
'border-color:rgba(255,23,68,.4)'+
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .admonition.error::before{' +
|
|||
|
'content: "\\2612";' +
|
|||
|
'font-family:"Arial";' +
|
|||
|
'font-size:200%;' +
|
|||
|
'position:relative;' +
|
|||
|
'color:rgba(128,12,34,.8);' +
|
|||
|
'top:-2px;' +
|
|||
|
'left:-3rem;' +
|
|||
|
'display:block;' +
|
|||
|
'width:0;' +
|
|||
|
'height:0' +
|
|||
|
'}' +
|
|||
|
|
|||
|
'.md .admonition p:last-child{margin-bottom:0}'
|
|||
|
);
|
|||
|
|
|||
|
var MARKDEEP_LINE = '<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js"></script><script src="https://casual-effects.com/markdeep/latest/markdeep.min.js?"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>';
|
|||
|
|
|||
|
// Language options:
|
|||
|
var FRENCH = {
|
|||
|
keyword: {
|
|||
|
table: 'tableau',
|
|||
|
figure: 'figure',
|
|||
|
listing: 'liste',
|
|||
|
diagram: 'diagramme',
|
|||
|
contents: 'Table des matières',
|
|||
|
|
|||
|
sec: 'sec',
|
|||
|
section: 'section',
|
|||
|
subsection: 'paragraphe',
|
|||
|
|
|||
|
Monday: 'lundi',
|
|||
|
Tuesday: 'mardi',
|
|||
|
Wednesday: 'mercredi',
|
|||
|
Thursday: 'jeudi',
|
|||
|
Friday: 'vendredi',
|
|||
|
Saturday: 'samedi',
|
|||
|
Sunday: 'dimanche',
|
|||
|
|
|||
|
January: 'Janvier',
|
|||
|
February: 'Février',
|
|||
|
March: 'Mars',
|
|||
|
April: 'Avril',
|
|||
|
May: 'Mai',
|
|||
|
June: 'Juin',
|
|||
|
July: 'Julliet',
|
|||
|
August: 'Août',
|
|||
|
September: 'Septembre',
|
|||
|
October: 'Octobre',
|
|||
|
November: 'Novembre',
|
|||
|
December: 'Décembre',
|
|||
|
|
|||
|
jan: 'janv',
|
|||
|
feb: 'févr',
|
|||
|
mar: 'mars',
|
|||
|
apr: 'avril',
|
|||
|
may: 'mai',
|
|||
|
jun: 'juin',
|
|||
|
jul: 'juil',
|
|||
|
aug: 'août',
|
|||
|
sep: 'sept',
|
|||
|
oct: 'oct',
|
|||
|
nov: 'nov',
|
|||
|
dec: 'déc'
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
// Translated by Zdravko Velinov
|
|||
|
var BULGARIAN = {
|
|||
|
keyword: {
|
|||
|
table: 'таблица',
|
|||
|
figure: 'фигура',
|
|||
|
listing: 'списък',
|
|||
|
diagram: 'диаграма',
|
|||
|
|
|||
|
contents: 'cъдържание',
|
|||
|
|
|||
|
sec: 'сек',
|
|||
|
section: 'раздел',
|
|||
|
subsection: 'подраздел',
|
|||
|
|
|||
|
Monday: 'понеделник',
|
|||
|
Tuesday: 'вторник',
|
|||
|
Wednesday: 'сряда',
|
|||
|
Thursday: 'четвъртък',
|
|||
|
Friday: 'петък',
|
|||
|
Saturday: 'събота',
|
|||
|
Sunday: 'неделя',
|
|||
|
|
|||
|
January: 'януари',
|
|||
|
February: 'февруари',
|
|||
|
March: 'март',
|
|||
|
April: 'април',
|
|||
|
May: 'май',
|
|||
|
June: 'юни',
|
|||
|
July: 'юли',
|
|||
|
August: 'август',
|
|||
|
September: 'септември',
|
|||
|
October: 'октомври',
|
|||
|
November: 'ноември',
|
|||
|
December: 'декември',
|
|||
|
|
|||
|
jan: 'ян',
|
|||
|
feb: 'февр',
|
|||
|
mar: 'март',
|
|||
|
apr: 'апр',
|
|||
|
may: 'май',
|
|||
|
jun: 'юни',
|
|||
|
jul: 'юли',
|
|||
|
aug: 'авг',
|
|||
|
sep: 'септ',
|
|||
|
oct: 'окт',
|
|||
|
nov: 'ноем',
|
|||
|
dec: 'дек'
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
var RUSSIAN = {
|
|||
|
keyword: {
|
|||
|
table: 'таблица',
|
|||
|
figure: 'рисунок',
|
|||
|
listing: 'листинг',
|
|||
|
diagram: 'диаграмма',
|
|||
|
|
|||
|
contents: 'Содержание',
|
|||
|
|
|||
|
sec: 'сек',
|
|||
|
section: 'раздел',
|
|||
|
subsection: 'подраздел',
|
|||
|
|
|||
|
Monday: 'понедельник',
|
|||
|
Tuesday: 'вторник',
|
|||
|
Wednesday: 'среда',
|
|||
|
Thursday: 'четверг',
|
|||
|
Friday: 'пятница',
|
|||
|
Saturday: 'суббота',
|
|||
|
Sunday: 'воскресенье',
|
|||
|
|
|||
|
January: 'январьr',
|
|||
|
February: 'февраль',
|
|||
|
March: 'март',
|
|||
|
April: 'апрель',
|
|||
|
May: 'май',
|
|||
|
June: 'июнь',
|
|||
|
July: 'июль',
|
|||
|
August: 'август',
|
|||
|
September: 'сентябрь',
|
|||
|
October: 'октябрь',
|
|||
|
November: 'ноябрь',
|
|||
|
December: 'декабрь',
|
|||
|
|
|||
|
jan: 'янв',
|
|||
|
feb: 'февр',
|
|||
|
mar: 'март',
|
|||
|
apr: 'апр',
|
|||
|
may: 'май',
|
|||
|
jun: 'июнь',
|
|||
|
jul: 'июль',
|
|||
|
aug: 'авг',
|
|||
|
sep: 'сент',
|
|||
|
oct: 'окт',
|
|||
|
nov: 'ноябрь',
|
|||
|
dec: 'дек'
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
// Translated by Dariusz Kuśnierek
|
|||
|
var POLISH = {
|
|||
|
keyword: {
|
|||
|
table: 'tabela',
|
|||
|
figure: 'ilustracja',
|
|||
|
listing: 'wykaz',
|
|||
|
diagram: 'diagram',
|
|||
|
contents: 'Spis treści',
|
|||
|
|
|||
|
sec: 'rozdz.',
|
|||
|
section: 'rozdział',
|
|||
|
subsection: 'podrozdział',
|
|||
|
|
|||
|
Monday: 'Poniedziałek',
|
|||
|
Tuesday: 'Wtorek',
|
|||
|
Wednesday: 'Środa',
|
|||
|
Thursday: 'Czwartek',
|
|||
|
Friday: 'Piątek',
|
|||
|
Saturday: 'Sobota',
|
|||
|
Sunday: 'Niedziela',
|
|||
|
|
|||
|
January: 'Styczeń',
|
|||
|
February: 'Luty',
|
|||
|
March: 'Marzec',
|
|||
|
April: 'Kwiecień',
|
|||
|
May: 'Maj',
|
|||
|
June: 'Czerwiec',
|
|||
|
July: 'Lipiec',
|
|||
|
August: 'Sierpień',
|
|||
|
September: 'Wrzesień',
|
|||
|
October: 'Październik',
|
|||
|
November: 'Listopad',
|
|||
|
December: 'Grudzień',
|
|||
|
|
|||
|
jan: 'sty',
|
|||
|
feb: 'lut',
|
|||
|
mar: 'mar',
|
|||
|
apr: 'kwi',
|
|||
|
may: 'maj',
|
|||
|
jun: 'cze',
|
|||
|
jul: 'lip',
|
|||
|
aug: 'sie',
|
|||
|
sep: 'wrz',
|
|||
|
oct: 'paź',
|
|||
|
nov: 'lis',
|
|||
|
dec: 'gru'
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
// Translated by Sandor Berczi
|
|||
|
var HUNGARIAN = {
|
|||
|
keyword: {
|
|||
|
table: 'táblázat',
|
|||
|
figure: 'ábra',
|
|||
|
listing: 'lista',
|
|||
|
diagram: 'diagramm',
|
|||
|
|
|||
|
contents: 'Tartalomjegyzék',
|
|||
|
|
|||
|
sec: 'fej', // Abbreviation for section
|
|||
|
section: 'fejezet',
|
|||
|
subsection:'alfejezet',
|
|||
|
|
|||
|
Monday: 'hétfő',
|
|||
|
Tuesday: 'kedd',
|
|||
|
Wednesday: 'szerda',
|
|||
|
Thursday: 'csütörtök',
|
|||
|
Friday: 'péntek',
|
|||
|
Saturday: 'szombat',
|
|||
|
Sunday: 'vasárnap',
|
|||
|
|
|||
|
January: 'január',
|
|||
|
February: 'február',
|
|||
|
March: 'március',
|
|||
|
April: 'április',
|
|||
|
May: 'május',
|
|||
|
June: 'június',
|
|||
|
July: 'július',
|
|||
|
August: 'augusztus',
|
|||
|
September: 'szeptember',
|
|||
|
October: 'október',
|
|||
|
November: 'november',
|
|||
|
December: 'december',
|
|||
|
|
|||
|
jan: 'jan',
|
|||
|
feb: 'febr',
|
|||
|
mar: 'márc',
|
|||
|
apr: 'ápr',
|
|||
|
may: 'máj',
|
|||
|
jun: 'jún',
|
|||
|
jul: 'júl',
|
|||
|
aug: 'aug',
|
|||
|
sep: 'szept',
|
|||
|
oct: 'okt',
|
|||
|
nov: 'nov',
|
|||
|
dec: 'dec'
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
// Translated by Takashi Masuyama
|
|||
|
var JAPANESE = {
|
|||
|
keyword: {
|
|||
|
table: '表',
|
|||
|
figure: '図',
|
|||
|
listing: '一覧',
|
|||
|
diagram: '図',
|
|||
|
contents: '目次',
|
|||
|
|
|||
|
sec: '章',
|
|||
|
section: '節',
|
|||
|
subsection: '項',
|
|||
|
|
|||
|
Monday: '月',
|
|||
|
Tuesday: '火',
|
|||
|
Wednesday: '水',
|
|||
|
Thursday: '木',
|
|||
|
Friday: '金',
|
|||
|
Saturday: '土',
|
|||
|
Sunday: '日',
|
|||
|
|
|||
|
January: '1月',
|
|||
|
February: '2月',
|
|||
|
March: '3月',
|
|||
|
April: '4月',
|
|||
|
May: '5月',
|
|||
|
June: '6月',
|
|||
|
July: '7月',
|
|||
|
August: '8月',
|
|||
|
September: '9月',
|
|||
|
October: '10月',
|
|||
|
November: '11月',
|
|||
|
December: '12月',
|
|||
|
|
|||
|
jan: '1月',
|
|||
|
feb: '2月',
|
|||
|
mar: '3月',
|
|||
|
apr: '4月',
|
|||
|
may: '5月',
|
|||
|
jun: '6月',
|
|||
|
jul: '7月',
|
|||
|
aug: '8月',
|
|||
|
sep: '9月',
|
|||
|
oct: '10月',
|
|||
|
nov: '11月',
|
|||
|
dec: '12月'
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
// Translated by Sandor Berczi
|
|||
|
var GERMAN = {
|
|||
|
keyword: {
|
|||
|
table: 'Tabelle',
|
|||
|
figure: 'Abbildung',
|
|||
|
listing: 'Auflistung',
|
|||
|
diagram: 'Diagramm',
|
|||
|
|
|||
|
contents: 'Inhaltsverzeichnis',
|
|||
|
|
|||
|
sec: 'Kap',
|
|||
|
section: 'Kapitel',
|
|||
|
subsection:'Unterabschnitt',
|
|||
|
|
|||
|
Monday: 'Montag',
|
|||
|
Tuesday: 'Dienstag',
|
|||
|
Wednesday: 'Mittwoch',
|
|||
|
Thursday: 'Donnerstag',
|
|||
|
Friday: 'Freitag',
|
|||
|
Saturday: 'Samstag',
|
|||
|
Sunday: 'Sonntag',
|
|||
|
|
|||
|
January: 'Januar',
|
|||
|
February: 'Februar',
|
|||
|
March: 'März',
|
|||
|
April: 'April',
|
|||
|
May: 'Mai',
|
|||
|
June: 'Juni',
|
|||
|
July: 'Juli',
|
|||
|
August: 'August',
|
|||
|
September: 'September',
|
|||
|
October: 'Oktober',
|
|||
|
November: 'November',
|
|||
|
December: 'Dezember',
|
|||
|
|
|||
|
jan: 'Jan',
|
|||
|
feb: 'Feb',
|
|||
|
mar: 'Mär',
|
|||
|
apr: 'Apr',
|
|||
|
may: 'Mai',
|
|||
|
jun: 'Jun',
|
|||
|
jul: 'Jul',
|
|||
|
aug: 'Aug',
|
|||
|
sep: 'Sep',
|
|||
|
oct: 'Okt',
|
|||
|
nov: 'Nov',
|
|||
|
dec: 'Dez'
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
// Translated by Nils Nilsson
|
|||
|
var SWEDISH = {
|
|||
|
keyword: {
|
|||
|
table: 'tabell',
|
|||
|
figure: 'figur',
|
|||
|
listing: 'lista',
|
|||
|
diagram: 'diagram',
|
|||
|
|
|||
|
contents: 'innehållsförteckning',
|
|||
|
sec: 'sek',
|
|||
|
subsection:'undersektion',
|
|||
|
|
|||
|
Monday: 'måndag',
|
|||
|
Tuesday: 'tisdag',
|
|||
|
Wednesday: 'onsdag',
|
|||
|
Thursday: 'torsdag',
|
|||
|
Friday: 'fredag',
|
|||
|
Saturday: 'lördag',
|
|||
|
Sunday: 'söndag',
|
|||
|
|
|||
|
January: 'januari',
|
|||
|
February: 'februari',
|
|||
|
March: 'mars',
|
|||
|
April: 'april',
|
|||
|
May: 'maj',
|
|||
|
June: 'juni',
|
|||
|
July: 'juli',
|
|||
|
August: 'augusti',
|
|||
|
September: 'september',
|
|||
|
October: 'oktober',
|
|||
|
November: 'november',
|
|||
|
December: 'december',
|
|||
|
|
|||
|
jan: 'jan',
|
|||
|
feb: 'feb',
|
|||
|
mar: 'mar',
|
|||
|
apr: 'apr',
|
|||
|
may: 'maj',
|
|||
|
jun: 'jun',
|
|||
|
jul: 'jul',
|
|||
|
aug: 'aug',
|
|||
|
sep: 'sep',
|
|||
|
oct: 'okt',
|
|||
|
nov: 'nov',
|
|||
|
dec: 'dec'
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
var DEFAULT_OPTIONS = {
|
|||
|
mode: 'markdeep',
|
|||
|
detectMath: true,
|
|||
|
lang: {keyword:{}}, // English
|
|||
|
tocStyle: 'auto',
|
|||
|
hideEmptyWeekends: true,
|
|||
|
showLabels: false,
|
|||
|
sortScheduleLists: true,
|
|||
|
captionAbove: {diagram: false,
|
|||
|
image: false,
|
|||
|
table: false,
|
|||
|
listing: false}
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
// See http://www.i18nguy.com/unicode/language-identifiers.html for keys
|
|||
|
var LANG_TABLE = {
|
|||
|
en: {keyword:{}},
|
|||
|
ru: RUSSIAN,
|
|||
|
fr: FRENCH,
|
|||
|
pl: POLISH,
|
|||
|
bg: BULGARIAN,
|
|||
|
de: GERMAN,
|
|||
|
hu: HUNGARIAN,
|
|||
|
sv: SWEDISH,
|
|||
|
ja: JAPANESE
|
|||
|
// Awaiting localization by a native speaker:
|
|||
|
// es: SPANISH
|
|||
|
// ...
|
|||
|
};
|
|||
|
|
|||
|
[].slice.call(document.getElementsByTagName('meta')).forEach(function(elt) {
|
|||
|
var att = elt.getAttribute('lang');
|
|||
|
if (att) {
|
|||
|
var lang = LANG_TABLE[att];
|
|||
|
if (lang) {
|
|||
|
DEFAULT_OPTIONS.lang = lang;
|
|||
|
}
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
|
|||
|
var max = Math.max;
|
|||
|
var min = Math.min;
|
|||
|
var abs = Math.abs;
|
|||
|
var sign = Math.sign || function (x) {
|
|||
|
return ( +x === x ) ? ((x === 0) ? x : (x > 0) ? 1 : -1) : NaN;
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
/** Get an option, or return the corresponding value from DEFAULT_OPTIONS */
|
|||
|
function option(key, key2) {
|
|||
|
if (window.markdeepOptions && (window.markdeepOptions[key] !== undefined)) {
|
|||
|
var val = window.markdeepOptions[key];
|
|||
|
if (key2) {
|
|||
|
val = val[key2]
|
|||
|
if (val !== undefined) {
|
|||
|
return val;
|
|||
|
} else {
|
|||
|
return DEFAULT_OPTIONS[key][key2];
|
|||
|
}
|
|||
|
} else {
|
|||
|
return window.markdeepOptions[key];
|
|||
|
}
|
|||
|
} else if (DEFAULT_OPTIONS[key] !== undefined) {
|
|||
|
if (key2) {
|
|||
|
return DEFAULT_OPTIONS[key][key2];
|
|||
|
} else {
|
|||
|
return DEFAULT_OPTIONS[key];
|
|||
|
}
|
|||
|
} else {
|
|||
|
console.warn('Illegal option: "' + key + '"');
|
|||
|
return undefined;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
function maybeShowLabel(url, tag) {
|
|||
|
if (option('showLabels')) {
|
|||
|
var text = ' {\u00A0' + url + '\u00A0}';
|
|||
|
return tag ? entag(tag, text) : text;
|
|||
|
} else {
|
|||
|
return '';
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// Returns the localized version of word, defaulting to the word itself
|
|||
|
function keyword(word) {
|
|||
|
return option('lang').keyword[word.toLowerCase()] || word;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/** Converts <>&" to their HTML escape sequences */
|
|||
|
function escapeHTMLEntities(str) {
|
|||
|
return String(str).rp(/&/g, '&').rp(/</g, '<').rp(/>/g, '>').rp(/"/g, '"');
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/** Restores the original source string's '<' and '>' as entered in
|
|||
|
the document, before the browser processed it as HTML. There is no
|
|||
|
way in an HTML document to distinguish an entity that was entered
|
|||
|
as an entity. */
|
|||
|
function unescapeHTMLEntities(str) {
|
|||
|
// Process & last so that we don't recursively unescape
|
|||
|
// escaped escape sequences.
|
|||
|
return str.
|
|||
|
rp(/</g, '<').
|
|||
|
rp(/>/g, '>').
|
|||
|
rp(/"/g, '"').
|
|||
|
rp(/'/g, "'").
|
|||
|
rp(/–/g, '\u2013').
|
|||
|
rp(/—/g, '---').
|
|||
|
rp(/&/g, '&');
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
function removeHTMLTags(str) {
|
|||
|
return str.rp(/<.*?>/g, '');
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/** Turn the argument into a legal URL anchor */
|
|||
|
function mangle(text) {
|
|||
|
return encodeURI(text.rp(/\s/g, '').toLowerCase());
|
|||
|
}
|
|||
|
|
|||
|
/** Creates a style sheet containing elements like:
|
|||
|
|
|||
|
hn::before {
|
|||
|
content: counter(h1) "." counter(h2) "." ... counter(hn) " ";
|
|||
|
counter-increment: hn;
|
|||
|
}
|
|||
|
*/
|
|||
|
function sectionNumberingStylesheet() {
|
|||
|
var s = '';
|
|||
|
|
|||
|
for (var i = 1; i <= 6; ++i) {
|
|||
|
s += '.md h' + i + '::before {\ncontent:';
|
|||
|
for (var j = 1; j <= i; ++j) {
|
|||
|
s += 'counter(h' + j + ') "' + ((j < i) ? '.' : ' ') + '"';
|
|||
|
}
|
|||
|
s += ';\ncounter-increment: h' + i + ';margin-right:10px}';
|
|||
|
}
|
|||
|
|
|||
|
return entag('style', s);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
\param node A node from an HTML DOM
|
|||
|
|
|||
|
\return A String that is a very good reconstruction of what the
|
|||
|
original source looked like before the browser tried to correct
|
|||
|
it to legal HTML.
|
|||
|
*/
|
|||
|
function nodeToMarkdeepSource(node, leaveEscapes) {
|
|||
|
var source = node.innerHTML;
|
|||
|
|
|||
|
// Markdown uses <john@bar.com> e-mail syntax, which HTML parsing
|
|||
|
// will try to close by inserting the matching close tags at the end of the
|
|||
|
// document. Remove anything that looks like that and comes *after*
|
|||
|
// the first fallback style.
|
|||
|
source = source.rp(/(?:<style class="fallback">[\s\S]*?<\/style>[\s\S]*)<\/\S+@\S+\.\S+?>/gim, '');
|
|||
|
|
|||
|
// Remove artificially inserted close tags
|
|||
|
source = source.rp(/<\/h?ttps?:.*>/gi, '');
|
|||
|
|
|||
|
// Now try to fix the URLs themselves, which will be
|
|||
|
// transformed like this: <http: casual-effects.com="" markdeep="">
|
|||
|
source = source.rp(/<(https?): (.*?)>/gi, function (match, protocol, list) {
|
|||
|
|
|||
|
// Remove any quotes--they wouldn't have been legal in the URL anyway
|
|||
|
var s = '<' + protocol + '://' + list.rp(/=""\s/g, '/');
|
|||
|
|
|||
|
if (s.ss(s.length - 3) === '=""') {
|
|||
|
s = s.ss(0, s.length - 3);
|
|||
|
}
|
|||
|
|
|||
|
// Remove any lingering quotes (since they
|
|||
|
// wouldn't have been legal in the URL)
|
|||
|
s = s.rp(/"/g, '');
|
|||
|
|
|||
|
return s + '>';
|
|||
|
});
|
|||
|
|
|||
|
// Remove the "fallback" style tags
|
|||
|
source = source.rp(/<style class=["']fallback["']>.*?<\/style>/gmi, '');
|
|||
|
|
|||
|
source = unescapeHTMLEntities(source);
|
|||
|
|
|||
|
return source;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/** Extracts one diagram from a Markdown string.
|
|||
|
|
|||
|
Returns {beforeString, diagramString, alignmentHint, afterString}
|
|||
|
diagramString will be empty if nothing was found. The
|
|||
|
DIAGRAM_MARKER is stripped from the diagramString.
|
|||
|
|
|||
|
alignmentHint may be:
|
|||
|
floatleft
|
|||
|
floatright
|
|||
|
center
|
|||
|
flushleft
|
|||
|
|
|||
|
diagramString does not include the marker characters.
|
|||
|
If there is a caption, it will appear in the afterString and not be parsed.
|
|||
|
*/
|
|||
|
function extractDiagram(sourceString) {
|
|||
|
// Returns the number of wide Unicode symbols (outside the BMP) in string s between indices
|
|||
|
// start and end - 1
|
|||
|
function unicodeSyms(s, start, end) {
|
|||
|
var p = start;
|
|||
|
for (var i = start; i < end; ++i, ++p) {
|
|||
|
var c = s.charCodeAt(p);
|
|||
|
p += (c >= 0xD800) && (c <= 0xDBFF);
|
|||
|
}
|
|||
|
return p - end;
|
|||
|
}
|
|||
|
|
|||
|
function advance() {
|
|||
|
nextLineBeginning = sourceString.indexOf('\n', lineBeginning) + 1;
|
|||
|
wideCharacters = unicodeSyms(sourceString, lineBeginning + xMin, lineBeginning + xMax);
|
|||
|
textOnLeft = textOnLeft || /\S/.test(sourceString.ss(lineBeginning, lineBeginning + xMin));
|
|||
|
// Text on the right ... if the line is not all '*'
|
|||
|
textOnRight = textOnRight || /[^ *\t\n\r]/.test(sourceString.ss(lineBeginning + xMax + wideCharacters + 1, nextLineBeginning));
|
|||
|
}
|
|||
|
|
|||
|
var noDiagramResult = {beforeString: sourceString, diagramString: '', alignmentHint: '', afterString: ''};
|
|||
|
|
|||
|
// Search sourceString for the first rectangle of enclosed
|
|||
|
// DIAGRAM_MARKER characters at least DIAGRAM_START.length wide
|
|||
|
for (var i = sourceString.indexOf(DIAGRAM_START);
|
|||
|
i >= 0;
|
|||
|
i = sourceString.indexOf(DIAGRAM_START, i + DIAGRAM_START.length)) {
|
|||
|
|
|||
|
// We found what looks like a diagram start. See if it has either a full border of
|
|||
|
// aligned '*' characters, or top-left-bottom borders and nothing but white space on
|
|||
|
// the left.
|
|||
|
|
|||
|
// Look backwards to find the beginning of the line (or of the string)
|
|||
|
// and measure the start character relative to it
|
|||
|
var lineBeginning = max(0, sourceString.lastIndexOf('\n', i)) + 1;
|
|||
|
var xMin = i - lineBeginning;
|
|||
|
|
|||
|
// Find the first non-diagram character on this line...or the end of the entire source string
|
|||
|
var j;
|
|||
|
for (j = i + DIAGRAM_START.length; sourceString[j] === DIAGRAM_MARKER; ++j) {}
|
|||
|
var xMax = j - lineBeginning - 1;
|
|||
|
|
|||
|
// We have a potential hit. Start accumulating a result. If there was anything
|
|||
|
// between the newline and the diagram, move it to the after string for proper alignment.
|
|||
|
var result = {
|
|||
|
beforeString: sourceString.ss(0, lineBeginning),
|
|||
|
diagramString: '',
|
|||
|
alignmentHint: 'center',
|
|||
|
afterString: sourceString.ss(lineBeginning, i).rp(/[ \t]+$/, ' ')
|
|||
|
};
|
|||
|
|
|||
|
var nextLineBeginning = 0, wideCharacters = 0;
|
|||
|
var textOnLeft = false, textOnRight = false;
|
|||
|
|
|||
|
advance();
|
|||
|
|
|||
|
// Now, see if the pattern repeats on subsequent lines
|
|||
|
for (var good = true, previousEnding = j; good; ) {
|
|||
|
// Find the next line
|
|||
|
lineBeginning = nextLineBeginning;
|
|||
|
advance();
|
|||
|
if (lineBeginning === 0) {
|
|||
|
// Hit the end of the string before the end of the pattern
|
|||
|
return noDiagramResult;
|
|||
|
}
|
|||
|
|
|||
|
if (textOnLeft) {
|
|||
|
// Even if there is text on *both* sides
|
|||
|
result.alignmentHint = 'floatright';
|
|||
|
} else if (textOnRight) {
|
|||
|
result.alignmentHint = 'floatleft';
|
|||
|
}
|
|||
|
|
|||
|
// See if there are markers at the correct locations on the next line
|
|||
|
if ((sourceString[lineBeginning + xMin] === DIAGRAM_MARKER) &&
|
|||
|
(! textOnLeft || (sourceString[lineBeginning + xMax + wideCharacters] === DIAGRAM_MARKER))) {
|
|||
|
|
|||
|
// See if there's a complete line of DIAGRAM_MARKER, which would end the diagram
|
|||
|
var x;
|
|||
|
for (x = xMin; (x < xMax) && (sourceString[lineBeginning + x] === DIAGRAM_MARKER); ++x) {}
|
|||
|
|
|||
|
var begin = lineBeginning + xMin;
|
|||
|
var end = lineBeginning + xMax + wideCharacters;
|
|||
|
|
|||
|
if (! textOnLeft) {
|
|||
|
// This may be an incomplete line
|
|||
|
var newlineLocation = sourceString.indexOf('\n', begin);
|
|||
|
if (newlineLocation !== -1) {
|
|||
|
end = Math.min(end, newlineLocation);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// Trim any excess whitespace caused by our truncation because Markdown will
|
|||
|
// interpret that as fixed-formatted lines
|
|||
|
result.afterString += sourceString.ss(previousEnding, begin).rp(/^[ \t]*[ \t]/, ' ').rp(/[ \t][ \t]*$/, ' ');
|
|||
|
if (x === xMax) {
|
|||
|
// We found the last row. Put everything else into
|
|||
|
// the afterString and return the result.
|
|||
|
|
|||
|
result.afterString += sourceString.ss(lineBeginning + xMax + 1);
|
|||
|
return result;
|
|||
|
} else {
|
|||
|
// A line of a diagram. Extract everything before
|
|||
|
// the diagram line started into the string of
|
|||
|
// content to be placed after the diagram in the
|
|||
|
// final HTML
|
|||
|
result.diagramString += sourceString.ss(begin + 1, end) + '\n';
|
|||
|
previousEnding = end + 1;
|
|||
|
}
|
|||
|
} else {
|
|||
|
// Found an incorrectly delimited line. Abort
|
|||
|
// processing of this potential diagram, which is now
|
|||
|
// known to NOT be a diagram after all.
|
|||
|
good = false;
|
|||
|
}
|
|||
|
} // Iterate over verticals in the potential box
|
|||
|
} // Search for the start
|
|||
|
|
|||
|
return noDiagramResult;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
Find the specified delimiterRegExp used as a quote (e.g., *foo*)
|
|||
|
and replace it with the HTML tag and optional attributes.
|
|||
|
*/
|
|||
|
function replaceMatched(string, delimiterRegExp, tag, attribs) {
|
|||
|
var delimiter = delimiterRegExp.source;
|
|||
|
var flanking = '[^ \\t\\n' + delimiter + ']';
|
|||
|
var pattern = '([^A-Za-z0-9])(' + delimiter + ')' +
|
|||
|
'(' + flanking + '.*?(\\n.+?)*?)' +
|
|||
|
delimiter + '(?![A-Za-z0-9])';
|
|||
|
|
|||
|
return string.rp(new RegExp(pattern, 'g'),
|
|||
|
'$1<' + tag + (attribs ? ' ' + attribs : '') +
|
|||
|
'>$3</' + tag + '>');
|
|||
|
}
|
|||
|
|
|||
|
/** Maruku ("github")-style table processing */
|
|||
|
function replaceTables(s, protect) {
|
|||
|
var TABLE_ROW = /(?:\n[ \t]*(?:(?:\|?[ \t\S]+?(?:\|[ \t\S]+?)+\|?)|\|[ \t\S]+\|)(?=\n))/.source;
|
|||
|
var TABLE_SEPARATOR = /\n[ \t]*(?:(?:\|? *\:?-+\:?(?: *\| *\:?-+\:?)+ *\|?|)|\|[\:-]+\|)(?=\n)/.source;
|
|||
|
var TABLE_CAPTION = /\n[ \t]*\[[^\n\|]+\][ \t]*(?=\n)/.source;
|
|||
|
var TABLE_REGEXP = new RegExp(TABLE_ROW + TABLE_SEPARATOR + TABLE_ROW + '+(' + TABLE_CAPTION + ')?', 'g');
|
|||
|
|
|||
|
function trimTableRowEnds(row) {
|
|||
|
return row.trim().rp(/^\||\|$/g, '');
|
|||
|
}
|
|||
|
|
|||
|
s = s.rp(TABLE_REGEXP, function (match) {
|
|||
|
// Found a table, actually parse it by rows
|
|||
|
var rowArray = match.split('\n');
|
|||
|
|
|||
|
var result = '';
|
|||
|
|
|||
|
// Skip the bogus leading row
|
|||
|
var startRow = (rowArray[0] === '') ? 1 : 0;
|
|||
|
|
|||
|
var caption = rowArray[rowArray.length - 1].trim();
|
|||
|
|
|||
|
if ((caption.length > 3) && (caption[0] === '[') && (caption[caption.length - 1] === ']')) {
|
|||
|
// Remove the caption from the row array
|
|||
|
rowArray.pop();
|
|||
|
caption = caption.ss(1, caption.length - 1);
|
|||
|
} else {
|
|||
|
caption = undefined;
|
|||
|
}
|
|||
|
|
|||
|
// Parse the separator row for left/center/right-indicating colons
|
|||
|
var columnStyle = [];
|
|||
|
trimTableRowEnds(rowArray[startRow + 1]).rp(/:?-+:?/g, function (match) {
|
|||
|
var left = (match[0] === ':');
|
|||
|
var right = (match[match.length - 1] === ':');
|
|||
|
columnStyle.push(protect(' style="text-align:' + ((left && right) ? 'center' : (right ? 'right' : 'left')) + '"'));
|
|||
|
});
|
|||
|
|
|||
|
var row = rowArray[startRow + 1].trim();
|
|||
|
var hasLeadingBar = row[0] === '|';
|
|||
|
var hasTrailingBar = row[row.length - 1] === '|';
|
|||
|
|
|||
|
var tag = 'th';
|
|||
|
|
|||
|
for (var r = startRow; r < rowArray.length; ++r) {
|
|||
|
// Remove leading and trailing whitespace and column delimiters
|
|||
|
row = rowArray[r].trim();
|
|||
|
|
|||
|
if (! hasLeadingBar && (row[0] === '|')) {
|
|||
|
// Empty first column
|
|||
|
row = ' ' + row;
|
|||
|
}
|
|||
|
|
|||
|
if (! hasTrailingBar && (row[row.length - 1] === '|')) {
|
|||
|
// Empty last column
|
|||
|
row += ' ';
|
|||
|
}
|
|||
|
|
|||
|
row = trimTableRowEnds(row);
|
|||
|
var i = 0;
|
|||
|
result += entag('tr', '<' + tag + columnStyle[0] + '> ' +
|
|||
|
row.rp(/ *\| */g, function () {
|
|||
|
++i;
|
|||
|
return ' </' + tag + '><' + tag + columnStyle[i] + '> ';
|
|||
|
}) + ' </' + tag + '>') + '\n';
|
|||
|
|
|||
|
// Skip the header-separator row
|
|||
|
if (r == startRow) {
|
|||
|
++r;
|
|||
|
tag = 'td';
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
result = entag('table', result, protect('class="table"'));
|
|||
|
|
|||
|
if (caption) {
|
|||
|
caption = entag('div', caption, protect('class="tablecaption"'));
|
|||
|
if (option('captionAbove', 'table')) {
|
|||
|
result = caption + result;
|
|||
|
} else {
|
|||
|
result = '\n' + result + caption;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return entag('div', result, "class='table'");
|
|||
|
});
|
|||
|
|
|||
|
return s;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
function replaceLists(s, protect) {
|
|||
|
// Identify list blocks:
|
|||
|
// Blank line or line ending in colon, line that starts with #., *, +, or -,
|
|||
|
// and then any number of lines until another blank line
|
|||
|
var BLANK_LINES = /\n\s*\n/.source;
|
|||
|
|
|||
|
// Preceding line ending in a colon
|
|||
|
var PREFIX = /[:,]\s*\n/.source;
|
|||
|
var LIST_BLOCK_REGEXP =
|
|||
|
new RegExp('(' + PREFIX + '|' + BLANK_LINES + '|<p>\s*\n|<br/>\s*\n?)' +
|
|||
|
/((?:[ \t]*(?:\d+\.|-|\+|\*)(?:[ \t]+.+\n(?:[ \t]*\n)?)+)+)/.source, 'gm');
|
|||
|
|
|||
|
var keepGoing = true;
|
|||
|
|
|||
|
var ATTRIBS = {'+': protect('class="plus"'), '-': protect('class="minus"'), '*': protect('class="asterisk"')};
|
|||
|
var NUMBER_ATTRIBS = protect('class="number"');
|
|||
|
|
|||
|
// Sometimes the list regexp grabs too much because subsequent lines are indented *less*
|
|||
|
// than the first line. So, if that case is found, re-run the regexp.
|
|||
|
while (keepGoing) {
|
|||
|
keepGoing = false;
|
|||
|
s = s.rp(LIST_BLOCK_REGEXP, function (match, prefix, block) {
|
|||
|
var result = prefix;
|
|||
|
|
|||
|
// Contains {indentLevel, tag}
|
|||
|
var stack = [];
|
|||
|
var current = {indentLevel: -1};
|
|||
|
|
|||
|
/* function logStack(stack) {
|
|||
|
var s = '[';
|
|||
|
stack.forEach(function(v) { s += v.indentLevel + ', '; });
|
|||
|
console.log(s.ss(0, s.length - 2) + ']');
|
|||
|
} */
|
|||
|
block.split('\n').forEach(function (line) {
|
|||
|
var trimmed = line.rp(/^\s*/, '');
|
|||
|
|
|||
|
var indentLevel = line.length - trimmed.length;
|
|||
|
|
|||
|
// Add a CSS class based on the type of list bullet
|
|||
|
var attribs = ATTRIBS[trimmed[0]];
|
|||
|
var isUnordered = !! attribs; // JavaScript for: attribs !== undefined
|
|||
|
attribs = attribs || NUMBER_ATTRIBS;
|
|||
|
var isOrdered = /^\d+\.[ \t]/.test(trimmed);
|
|||
|
var isBlank = trimmed === '';
|
|||
|
var start = isOrdered ? ' ' + protect('start=' + trimmed.match(/^\d+/)[0]) : '';
|
|||
|
|
|||
|
if (isOrdered || isUnordered) {
|
|||
|
// Add the indentation for the bullet itself
|
|||
|
indentLevel += 2;
|
|||
|
}
|
|||
|
|
|||
|
if (! current) {
|
|||
|
// Went below top-level indent
|
|||
|
result += '\n' + line;
|
|||
|
} else if (! isOrdered && ! isUnordered && (isBlank || (indentLevel >= current.indentLevel))) {
|
|||
|
// Line without a marker
|
|||
|
result += '\n' + current.indentChars + line;
|
|||
|
} else {
|
|||
|
//console.log(indentLevel + ":" + line);
|
|||
|
if (indentLevel !== current.indentLevel) {
|
|||
|
// Enter or leave indentation level
|
|||
|
if ((current.indentLevel !== -1) && (indentLevel < current.indentLevel)) {
|
|||
|
while (current && (indentLevel < current.indentLevel)) {
|
|||
|
stack.pop();
|
|||
|
// End the current list and decrease indentation
|
|||
|
result += '\n</li></' + current.tag + '>';
|
|||
|
current = stack[stack.length - 1];
|
|||
|
}
|
|||
|
} else {
|
|||
|
// Start a new list that is more indented
|
|||
|
current = {indentLevel: indentLevel,
|
|||
|
tag: isOrdered ? 'ol' : 'ul',
|
|||
|
// Subtract off the two indent characters we added above
|
|||
|
indentChars: line.ss(0, indentLevel - 2)};
|
|||
|
stack.push(current);
|
|||
|
result += '\n<' + current.tag + start + '>';
|
|||
|
}
|
|||
|
} else if (current.indentLevel !== -1) {
|
|||
|
// End previous list item, if there was one
|
|||
|
result += '\n</li>';
|
|||
|
} // Indent level changed
|
|||
|
|
|||
|
if (current) {
|
|||
|
// Add the list item
|
|||
|
result += '\n' + current.indentChars + '<li ' + attribs + '>' + trimmed.rp(/^(\d+\.|-|\+|\*) /, '');
|
|||
|
} else {
|
|||
|
// Just reached something that is *less* indented than the root--
|
|||
|
// copy forward and then re-process that list
|
|||
|
result += '\n' + line;
|
|||
|
keepGoing = true;
|
|||
|
}
|
|||
|
}
|
|||
|
}); // For each line
|
|||
|
|
|||
|
// Remove trailing whitespace
|
|||
|
result = result.replace(/\s+$/,'');
|
|||
|
|
|||
|
// Finish the last item and anything else on the stack (if needed)
|
|||
|
for (current = stack.pop(); current; current = stack.pop()) {
|
|||
|
result += '</li></' + current.tag + '>';
|
|||
|
}
|
|||
|
|
|||
|
return result + '\n\n';
|
|||
|
});
|
|||
|
} // while keep going
|
|||
|
|
|||
|
return s;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
Identifies schedule lists, which look like:
|
|||
|
|
|||
|
date: title
|
|||
|
events
|
|||
|
|
|||
|
Where date must contain a day, month, and four-number year and may
|
|||
|
also contain a day of the week. Note that the date must not be
|
|||
|
indented and the events must be indented.
|
|||
|
|
|||
|
Multiple events per date are permitted.
|
|||
|
*/
|
|||
|
function replaceScheduleLists(str, protect) {
|
|||
|
// Must open with something other than indentation or a list
|
|||
|
// marker. There must be a four-digit number somewhere on the
|
|||
|
// line. Exclude lines that begin with an HTML tag...this will
|
|||
|
// avoid parsing headers that have dates in them.
|
|||
|
var BEGINNING = /^(?:[^\|<>\s-\+\*\d].*[12]\d{3}(?!\d).*?|(?:[12]\d{3}(?!\.).*\d.*?)|(?:\d{1,3}(?!\.).*[12]\d{3}(?!\d).*?))/.source;
|
|||
|
|
|||
|
// There must be at least one more number in a date, a colon, and then some more text
|
|||
|
var DATE_AND_TITLE = '(' + BEGINNING + '):' + /[ \t]+([^ \t\n].*)\n/.source;
|
|||
|
|
|||
|
// The body of the schedule item. It may begin with a blank line and contain
|
|||
|
// multiple paragraphs separated by blank lines...as long as there is indenting
|
|||
|
var EVENTS = /(?:[ \t]*\n)?((?:[ \t]+.+\n(?:[ \t]*\n){0,3})*)/.source;
|
|||
|
var ENTRY = DATE_AND_TITLE + EVENTS;
|
|||
|
|
|||
|
var ENTRY_REGEXP = new RegExp(ENTRY, 'gm');
|
|||
|
|
|||
|
var rowAttribs = protect('valign="top"');
|
|||
|
var dateTDAttribs = protect('style="width:100px;padding-right:15px" rowspan="2"');
|
|||
|
var eventTDAttribs = protect('style="padding-bottom:25px"');
|
|||
|
|
|||
|
var DAY_NAME = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'].map(keyword);
|
|||
|
var MONTH_NAME = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'].map(keyword);
|
|||
|
var MONTH_NAME_LIST = MONTH_NAME.join('|');
|
|||
|
var MONTH_FULL_NAME = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'].map(keyword);
|
|||
|
|
|||
|
// Used to mark the center of each day. Not close to midnight to avoid daylight
|
|||
|
// savings problems.
|
|||
|
var standardHour = 9;
|
|||
|
|
|||
|
try {
|
|||
|
var scheduleNumber = 0;
|
|||
|
str =
|
|||
|
str.rp(new RegExp('(' + ENTRY + '){2,}', 'gm'),
|
|||
|
function (schedule) {
|
|||
|
++scheduleNumber;
|
|||
|
|
|||
|
// Each entry has the form {date:date, title:string, text:string}
|
|||
|
var entryArray = [];
|
|||
|
|
|||
|
// Now parse the schedule into individual day entries
|
|||
|
|
|||
|
var anyWeekendEvents = false;
|
|||
|
|
|||
|
schedule.rp(ENTRY_REGEXP,
|
|||
|
function (entry, date, title, events) {
|
|||
|
// Remove the day from the date (we'll reconstruct it below). This is actually unnecessary, since we
|
|||
|
// explicitly compute the value anyway and the parser is robust to extra characters, but it aides
|
|||
|
// in debugging.
|
|||
|
//
|
|||
|
// date = date.rp(/(?:(?:sun|mon|tues|wednes|thurs|fri|satur)day|(?:sun|mon|tue|wed|thu|fri|sat)\.?|(?:su|mo|tu|we|th|fr|sa)),?/gi, '');
|
|||
|
|
|||
|
// Parse the date. The Javascript Date class's parser is useless because it
|
|||
|
// is locale dependent, so we do this with a regexp.
|
|||
|
|
|||
|
var year = '', month = '', day = '', parenthesized = false;
|
|||
|
|
|||
|
date = date.trim();
|
|||
|
|
|||
|
if ((date[0] === '(') && (date.slice(-1) === ')')) {
|
|||
|
// This is a parenthesized entry
|
|||
|
date = date.slice(1, -1);
|
|||
|
parenthesized = true;
|
|||
|
}
|
|||
|
|
|||
|
// DD MM YYYY
|
|||
|
var match = date.match(RegExp('([0123]?\\d)\\D+([01]?\\d|' + MONTH_NAME_LIST + ')\\D+([12]\\d{3})', 'i'));
|
|||
|
|
|||
|
if (match) {
|
|||
|
day = match[1]; month = match[2]; year = match[3];
|
|||
|
} else {
|
|||
|
// YYYY MM DD
|
|||
|
match = date.match(RegExp('([12]\\d{3})\\D+([01]?\\d|' + MONTH_NAME_LIST + ')\\D+([0123]?\\d)', 'i'));
|
|||
|
if (match) {
|
|||
|
day = match[3]; month = match[2]; year = match[1];
|
|||
|
} else {
|
|||
|
// monthname day year
|
|||
|
match = date.match(RegExp('(' + MONTH_NAME_LIST + ')\\D+([0123]?\\d)\\D+([12]\\d{3})', 'i'));
|
|||
|
if (match) {
|
|||
|
day = match[2]; month = match[1]; year = match[3];
|
|||
|
} else {
|
|||
|
throw "Could not parse date";
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// Reconstruct standardized date format
|
|||
|
date = day + ' ' + month + ' ' + year;
|
|||
|
|
|||
|
// Detect the month
|
|||
|
var monthNumber = parseInt(month) - 1;
|
|||
|
if (isNaN(monthNumber)) {
|
|||
|
monthNumber = MONTH_NAME.indexOf(month.toLowerCase());
|
|||
|
}
|
|||
|
|
|||
|
var dateVal = new Date(Date.UTC(parseInt(year), monthNumber, parseInt(day), standardHour));
|
|||
|
// Reconstruct the day of the week
|
|||
|
var dayOfWeek = dateVal.getUTCDay();
|
|||
|
date = DAY_NAME[dayOfWeek] + '<br/>' + date;
|
|||
|
|
|||
|
anyWeekendEvents = anyWeekendEvents || (dayOfWeek === 0) || (dayOfWeek === 6);
|
|||
|
|
|||
|
entryArray.push({date: dateVal,
|
|||
|
title: title,
|
|||
|
sourceOrder: entryArray.length,
|
|||
|
parenthesized: parenthesized,
|
|||
|
|
|||
|
// Don't show text if parenthesized with no body
|
|||
|
text: parenthesized ? '' :
|
|||
|
entag('tr',
|
|||
|
entag('td',
|
|||
|
'<a ' + protect('class="target" name="schedule' + scheduleNumber + '_' + dateVal.getUTCFullYear() + '-' + (dateVal.getUTCMonth() + 1) + '-' + dateVal.getUTCDate() + '"') + '> </a>' +
|
|||
|
date, dateTDAttribs) +
|
|||
|
entag('td', entag('b', title)), rowAttribs) +
|
|||
|
entag('tr', entag('td', '\n\n' + events, eventTDAttribs), rowAttribs)});
|
|||
|
|
|||
|
return '';
|
|||
|
});
|
|||
|
|
|||
|
// Shallow copy the entries to bypass sorting if needed
|
|||
|
var sourceEntryArray = option('sortScheduleLists') ? entryArray : entryArray.slice(0);
|
|||
|
|
|||
|
// Sort by date
|
|||
|
entryArray.sort(function (a, b) {
|
|||
|
// Javascript's sort is not specified to be
|
|||
|
// stable, so we have to preserve
|
|||
|
// sourceOrder in ties.
|
|||
|
var ta = a.date.getTime();
|
|||
|
var tb = b.date.getTime();
|
|||
|
return (ta === tb) ? (a.sourceOrder - b.sourceOrder) : (ta - tb);
|
|||
|
});
|
|||
|
|
|||
|
var MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24;
|
|||
|
|
|||
|
// May be slightly off due to daylight savings time
|
|||
|
var approximateDaySpan = (entryArray[entryArray.length - 1].date.getTime() - entryArray[0].date.getTime()) / MILLISECONDS_PER_DAY;
|
|||
|
|
|||
|
var today = new Date();
|
|||
|
// Move back to midnight
|
|||
|
today = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate(), standardHour));
|
|||
|
|
|||
|
var calendar = '';
|
|||
|
// Make a calendar view with links, if suitable
|
|||
|
if ((approximateDaySpan > 14) && (approximateDaySpan / entryArray.length < 16)) {
|
|||
|
var DAY_HEADER_ATTRIBS = protect('colspan="2" width="14%" style="padding-top:5px;text-align:center;font-style:italic"');
|
|||
|
var DATE_ATTRIBS = protect('width="1%" height="30px" style="text-align:right;border:1px solid #EEE;border-right:none;"');
|
|||
|
var FADED_ATTRIBS = protect('width="1%" height="30px" style="color:#BBB;text-align:right;"');
|
|||
|
var ENTRY_ATTRIBS = protect('width="14%" style="border:1px solid #EEE;border-left:none;"');
|
|||
|
var PARENTHESIZED_ATTRIBS = protect('class="parenthesized"');
|
|||
|
|
|||
|
// Find the first day of the first month
|
|||
|
var date = entryArray[0].date;
|
|||
|
var index = 0;
|
|||
|
|
|||
|
var hideWeekends = ! anyWeekendEvents && option('hideEmptyWeekends');
|
|||
|
var showDate = hideWeekends ? function(date) { return (date.getUTCDay() > 0) && (date.getUTCDay() < 6);} : function() { return true; };
|
|||
|
|
|||
|
var sameDay = function (d1, d2) {
|
|||
|
// Account for daylight savings time
|
|||
|
return (abs(d1.getTime() - d2.getTime()) < MILLISECONDS_PER_DAY / 2);
|
|||
|
}
|
|||
|
|
|||
|
// Go to the first of the month
|
|||
|
date = new Date(date.getUTCFullYear(), date.getUTCMonth(), 1, standardHour);
|
|||
|
|
|||
|
while (date.getTime() < entryArray[entryArray.length - 1].date.getTime()) {
|
|||
|
|
|||
|
// Create the calendar header
|
|||
|
calendar += '<table ' + protect('class="calendar"') + '>\n' +
|
|||
|
entag('tr', entag('th', MONTH_FULL_NAME[date.getUTCMonth()] + ' ' + date.getUTCFullYear(), protect('colspan="14"'))) + '<tr>';
|
|||
|
|
|||
|
(hideWeekends ? DAY_NAME.slice(1, 6) : DAY_NAME).forEach(function (name) {
|
|||
|
calendar += entag('td', name, DAY_HEADER_ATTRIBS);
|
|||
|
});
|
|||
|
calendar += '</tr>';
|
|||
|
|
|||
|
// Go back into the previous month to reach a Sunday. Check the time at noon
|
|||
|
// to avoid problems with daylight saving time occuring early in the morning
|
|||
|
while (date.getUTCDay() !== 0) {
|
|||
|
date = new Date(date.getTime() - MILLISECONDS_PER_DAY);
|
|||
|
}
|
|||
|
|
|||
|
// Insert the days from the previous month
|
|||
|
if (date.getDate() !== 1) {
|
|||
|
calendar += '<tr ' + rowAttribs + '>';
|
|||
|
while (date.getDate() !== 1) {
|
|||
|
if (showDate(date)) { calendar += '<td ' + FADED_ATTRIBS + '>' + date.getUTCDate() + '</td><td> </td>'; }
|
|||
|
date = new Date(date.getTime() + MILLISECONDS_PER_DAY);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// Run until the end of the month
|
|||
|
do {
|
|||
|
if (date.getUTCDay() === 0) {
|
|||
|
// Sunday, start a row
|
|||
|
calendar += '<tr ' + rowAttribs + '>';
|
|||
|
}
|
|||
|
|
|||
|
if (showDate(date)) {
|
|||
|
var attribs = '';
|
|||
|
if (sameDay(date, today)) {
|
|||
|
attribs = protect('class="today"');
|
|||
|
}
|
|||
|
|
|||
|
// Insert links as needed from entries
|
|||
|
var contents = '';
|
|||
|
|
|||
|
for (var entry = entryArray[index]; entry && sameDay(entry.date, date); ++index, entry = entryArray[index]) {
|
|||
|
if (contents) { contents += '<br/>'; }
|
|||
|
if (entry.parenthesized) {
|
|||
|
// Parenthesized with no body, no need for a link
|
|||
|
contents += entag('span', entry.title, PARENTHESIZED_ATTRIBS);
|
|||
|
} else {
|
|||
|
contents += entag('a', entry.title, protect('href="#schedule' + scheduleNumber + '_' + date.getUTCFullYear() + '-' + (date.getUTCMonth() + 1) + '-' + date.getUTCDate() + '"'));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (contents) {
|
|||
|
calendar += entag('td', entag('b', date.getUTCDate()), DATE_ATTRIBS + attribs) + entag('td', contents, ENTRY_ATTRIBS + attribs);
|
|||
|
} else {
|
|||
|
calendar += '<td ' + DATE_ATTRIBS + attribs + '></a>' + date.getUTCDate() + '</td><td ' + ENTRY_ATTRIBS + attribs + '> </td>';
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (date.getUTCDay() === 6) {
|
|||
|
// Saturday, end a row
|
|||
|
calendar += '</tr>';
|
|||
|
}
|
|||
|
|
|||
|
// Go to (approximately) the next day
|
|||
|
date = new Date(date.getTime() + MILLISECONDS_PER_DAY);
|
|||
|
} while (date.getUTCDate() > 1);
|
|||
|
|
|||
|
// Finish out the week after the end of the month
|
|||
|
if (date.getUTCDay() !== 0) {
|
|||
|
while (date.getUTCDay() !== 0) {
|
|||
|
if (showDate(date)) { calendar += '<td ' + FADED_ATTRIBS + '>' + date.getUTCDate() + '</td><td> </td>'; }
|
|||
|
date = new Date(date.getTime() + MILLISECONDS_PER_DAY);
|
|||
|
}
|
|||
|
|
|||
|
calendar += '</tr>';
|
|||
|
}
|
|||
|
|
|||
|
calendar += '</table><br/>\n';
|
|||
|
|
|||
|
// Go to the first of the (new) month
|
|||
|
date = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 1, standardHour));
|
|||
|
|
|||
|
} // Until all days covered
|
|||
|
} // if add calendar
|
|||
|
|
|||
|
// Construct the schedule
|
|||
|
schedule = '';
|
|||
|
sourceEntryArray.forEach(function (entry) {
|
|||
|
schedule += entry.text;
|
|||
|
});
|
|||
|
|
|||
|
return calendar + entag('table', schedule, protect('class="schedule"')) + '\n\n';
|
|||
|
});
|
|||
|
} catch (ignore) {
|
|||
|
// Maybe this wasn't a schedule after all, since we couldn't parse a date
|
|||
|
console.log(ignore + ' in a Markdeep schedule list');
|
|||
|
}
|
|||
|
|
|||
|
return str;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
Term
|
|||
|
: description, which might be multiple
|
|||
|
lines and include blanks.
|
|||
|
|
|||
|
Next Term
|
|||
|
|
|||
|
becomes
|
|||
|
|
|||
|
<dl>
|
|||
|
<dt>Term</dt>
|
|||
|
<dd> description, which might be multiple
|
|||
|
lines and include blanks.</dd>
|
|||
|
<dt>Next Term</dt>
|
|||
|
</dl>
|
|||
|
|
|||
|
... unless it is very short, in which case it becomes a table.
|
|||
|
|
|||
|
*/
|
|||
|
function replaceDefinitionLists(s, protect) {
|
|||
|
var TERM = /^.+\n:(?=[ \t])/.source;
|
|||
|
|
|||
|
// Definition can contain multiple paragraphs
|
|||
|
var DEFINITION = '(\s*\n|[: \t].+\n)+';
|
|||
|
|
|||
|
s = s.rp(new RegExp('(' + TERM + DEFINITION + ')+', 'gm'),
|
|||
|
function (block) {
|
|||
|
|
|||
|
var list = [];
|
|||
|
|
|||
|
// Parse the block
|
|||
|
var currentEntry = null;
|
|||
|
|
|||
|
block.split('\n').forEach(function (line, i) {
|
|||
|
// What kind of line is this?
|
|||
|
if (line.trim().length === 0) {
|
|||
|
if (currentEntry) {
|
|||
|
// Empty line
|
|||
|
currentEntry.definition += '\n';
|
|||
|
}
|
|||
|
} else if (! /\s/.test(line[0]) && (line[0] !== ':')) {
|
|||
|
currentEntry = {term: line, definition: ''};
|
|||
|
list.push(currentEntry);
|
|||
|
} else {
|
|||
|
// Add the line to the current definition, stripping any single leading ':'
|
|||
|
if (line[0] === ':') { line = ' ' + line.ss(1); }
|
|||
|
currentEntry.definition += line + '\n';
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
var longestDefinition = 0;
|
|||
|
list.forEach(function (entry) {
|
|||
|
if (/\n\s*\n/.test(entry.definition.trim())) {
|
|||
|
// This definition contains multiple paragraphs. Force it into long mode
|
|||
|
longestDefinition = Infinity;
|
|||
|
} else {
|
|||
|
// Normal case
|
|||
|
longestDefinition = max(longestDefinition, unescapeHTMLEntities(removeHTMLTags(entry.definition)).length);
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
var result = '';
|
|||
|
if (longestDefinition < 160) {
|
|||
|
var rowAttribs = protect('valign=top');
|
|||
|
// This list has short definitions. Format it as a table
|
|||
|
list.forEach(function (entry) {
|
|||
|
result += entag('tr',
|
|||
|
entag('td', entag('dt', entry.term)) +
|
|||
|
entag('td', entag('dd', entag('p', entry.definition))),
|
|||
|
rowAttribs);
|
|||
|
});
|
|||
|
result = entag('table', result);
|
|||
|
|
|||
|
} else {
|
|||
|
list.forEach(function (entry) {
|
|||
|
// Leave *two* blanks at the start of a
|
|||
|
// definition so that subsequent processing
|
|||
|
// can detect block formatting within it.
|
|||
|
result += entag('dt', entry.term) + entag('dd', entag('p', entry.definition));
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
return entag('dl', result);
|
|||
|
|
|||
|
});
|
|||
|
|
|||
|
return s;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/** Inserts a table of contents in the document and then returns
|
|||
|
[string, table], where the table maps strings to levels. */
|
|||
|
function insertTableOfContents(s, protect) {
|
|||
|
|
|||
|
// Gather headers for table of contents (TOC). We
|
|||
|
// accumulate a long and short TOC and then choose which
|
|||
|
// to insert at the end.
|
|||
|
var fullTOC = '';
|
|||
|
var shortTOC = '';
|
|||
|
|
|||
|
// headerCounter[i] is the current counter for header level (i - 1)
|
|||
|
var headerCounter = [0];
|
|||
|
var currentLevel = 0;
|
|||
|
var numAboveLevel1 = 0;
|
|||
|
|
|||
|
var table = {};
|
|||
|
s = s.rp(/<h([1-6])>(.*?)<\/h\1>/gi, function (header, level, text) {
|
|||
|
level = parseInt(level)
|
|||
|
text = text.trim();
|
|||
|
// If becoming more nested:
|
|||
|
for (var i = currentLevel; i < level; ++i) { headerCounter[i] = 0; }
|
|||
|
|
|||
|
// If becoming less nested:
|
|||
|
headerCounter.splice(level, currentLevel - level);
|
|||
|
currentLevel = level;
|
|||
|
|
|||
|
++headerCounter[currentLevel - 1];
|
|||
|
|
|||
|
// Generate a unique name for this element
|
|||
|
var number = headerCounter.join('.');
|
|||
|
var name = 'toc' + number;
|
|||
|
|
|||
|
table[removeHTMLTags(text).trim().toLowerCase()] = number;
|
|||
|
|
|||
|
// Only insert for the first three levels
|
|||
|
if (level <= 3) {
|
|||
|
// Indent and append (the Array() call generates spaces)
|
|||
|
fullTOC += Array(level).join(' ') + '<a href="#' + name + '" class="level' + level + '"><span class="tocNumber">' + number + ' </span>' + text + '</a><br/>\n';
|
|||
|
|
|||
|
if (level === 1) {
|
|||
|
shortTOC += ' · <a href="#' + name + '">' + text + '</a>';
|
|||
|
} else {
|
|||
|
++numAboveLevel1;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return entag('a', ' ', protect('class="target" name="' + name + '"')) + header;
|
|||
|
});
|
|||
|
|
|||
|
if (shortTOC.length > 0) {
|
|||
|
// Strip the leading " · "
|
|||
|
shortTOC = shortTOC.ss(10);
|
|||
|
}
|
|||
|
|
|||
|
var numLevel1 = headerCounter[0];
|
|||
|
var numHeaders = numLevel1 + numAboveLevel1;
|
|||
|
|
|||
|
// The location of the first header is indicative of the length of
|
|||
|
// the abstract...as well as where we insert. The first header may be accompanied by
|
|||
|
// <a name> tags, which we want to appear before.
|
|||
|
var firstHeaderLocation = s.regexIndexOf(/((<a\s+\S+> <\/a>)\s*)*?<h\d>/i);
|
|||
|
if (firstHeaderLocation === -1) { firstHeaderLocation = 0; }
|
|||
|
|
|||
|
var AFTER_TITLES = '<div class="afterTitles"><\/div>';
|
|||
|
var insertLocation = s.indexOf(AFTER_TITLES);
|
|||
|
if (insertLocation === -1) {
|
|||
|
insertLocation = 0;
|
|||
|
} else {
|
|||
|
insertLocation += AFTER_TITLES.length;
|
|||
|
}
|
|||
|
|
|||
|
// Which TOC style should we use?
|
|||
|
var tocStyle = option('tocStyle');
|
|||
|
|
|||
|
var TOC = '';
|
|||
|
if ((tocStyle === 'auto') || (tocStyle === '')) {
|
|||
|
if (((numHeaders < 4) && (numLevel1 <= 1)) || (s.length < 2048)) {
|
|||
|
// No TOC; this document is really short
|
|||
|
tocStyle = 'none';
|
|||
|
} else if ((numLevel1 < 7) && (numHeaders / numLevel1 < 2.5)) {
|
|||
|
// We can use the short TOC
|
|||
|
tocStyle = 'short';
|
|||
|
} else if ((firstHeaderLocation === -1) || (firstHeaderLocation / 55 > numHeaders)) {
|
|||
|
// The abstract is long enough to float alongside, and there
|
|||
|
// are not too many levels.
|
|||
|
// Insert the medium-length TOC floating
|
|||
|
tocStyle = 'medium';
|
|||
|
} else {
|
|||
|
// This is a long table of contents or a short abstract
|
|||
|
// Insert a long toc...right before the first header
|
|||
|
tocStyle = 'long';
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
switch (tocStyle) {
|
|||
|
case 'none':
|
|||
|
case '':
|
|||
|
break;
|
|||
|
|
|||
|
case 'short':
|
|||
|
TOC = '<div class="shortTOC">' + shortTOC + '</div>';
|
|||
|
break;
|
|||
|
|
|||
|
case 'medium':
|
|||
|
TOC = '<div class="mediumTOC"><center><b>' + keyword('Contents') + '</b></center><p>' + fullTOC + '</p></div>';
|
|||
|
break;
|
|||
|
|
|||
|
case 'long':
|
|||
|
insertLocation = firstHeaderLocation;
|
|||
|
TOC = '<div class="longTOC"><div class="tocHeader">' + keyword('Contents') + '</div><p>' + fullTOC + '</p></div>';
|
|||
|
break;
|
|||
|
|
|||
|
default:
|
|||
|
console.log('markdeepOptions.tocStyle = "' + tocStyle + '" specified in your document is not a legal value');
|
|||
|
}
|
|||
|
|
|||
|
s = s.ss(0, insertLocation) + TOC + s.ss(insertLocation);
|
|||
|
|
|||
|
return [s, table];
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
function escapeRegExpCharacters(str) {
|
|||
|
return str.rp(/([\.\[\]\(\)\*\+\?\^\$\\\{\}\|])/g, '\\$1');
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/** Returns true if there are at least two newlines in each of the arguments */
|
|||
|
function isolated(preSpaces, postSpaces) {
|
|||
|
if (preSpaces && postSpaces) {
|
|||
|
preSpaces = preSpaces.match(/\n/g);
|
|||
|
postSpaces = postSpaces.match(/\n/g);
|
|||
|
return preSpaces && (preSpaces.length > 1) && postSpaces && (postSpaces.length > 1);
|
|||
|
} else {
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
Performs Markdeep processing on str, which must be a string or a
|
|||
|
DOM element. Returns a string that is the HTML to display for the
|
|||
|
body. The result does not include the header: Markdeep stylesheet
|
|||
|
and script tags for including a math library, or the Markdeep
|
|||
|
signature footer.
|
|||
|
|
|||
|
Optional argument elementMode defaults to true. This avoids turning a bold first word into a
|
|||
|
title or introducing a table of contents. Section captions are unaffected by this argument.
|
|||
|
Set elementMode = false if processing a whole document instead of an internal node.
|
|||
|
|
|||
|
*/
|
|||
|
function markdeepToHTML(str, elementMode) {
|
|||
|
// Map names to the number used for end notes, in the order
|
|||
|
// encountered in the text.
|
|||
|
var endNoteTable = {}, endNoteCount = 0;
|
|||
|
|
|||
|
// Reference links
|
|||
|
var referenceLinkTable = {};
|
|||
|
|
|||
|
// In the private use area
|
|||
|
var PROTECT_CHARACTER = '\ue010';
|
|||
|
|
|||
|
// Use base 36 for encoding numbers
|
|||
|
var PROTECT_RADIX = 35;
|
|||
|
var protectedStringArray = [];
|
|||
|
|
|||
|
// Gives 1.5M possible sequences in base 56
|
|||
|
var PROTECT_DIGITS = 4;
|
|||
|
|
|||
|
// Put the protect character at BOTH ends to avoid having the protected number encoding
|
|||
|
// look like an actual number to further markdown processing
|
|||
|
var PROTECT_REGEXP = RegExp(PROTECT_CHARACTER + '[0-9a-wyz]{' + PROTECT_DIGITS + ',' + PROTECT_DIGITS + '}' + PROTECT_CHARACTER, 'g');
|
|||
|
|
|||
|
/** Given an arbitrary string, returns an escaped identifier
|
|||
|
string to temporarily replace it with to prevent Markdeep from
|
|||
|
processing the contents. See expose() */
|
|||
|
function protect(s) {
|
|||
|
var i = (protectedStringArray.push(s) - 1).toString(PROTECT_RADIX);
|
|||
|
|
|||
|
// Avoid the pattern "#x#", which Markdeep would interpret as a dimension eligible for
|
|||
|
// beautification.
|
|||
|
i = i.rp(/x/gi, 'z');
|
|||
|
|
|||
|
// Ensure fixed length
|
|||
|
while (i.length < PROTECT_DIGITS) {
|
|||
|
i = '0' + i;
|
|||
|
}
|
|||
|
|
|||
|
return PROTECT_CHARACTER + i + PROTECT_CHARACTER;
|
|||
|
}
|
|||
|
|
|||
|
/** Given the escaped identifier string from protect(), returns
|
|||
|
the orginal string. */
|
|||
|
function expose(i) {
|
|||
|
// Strip the escape character and parse, then look up in the
|
|||
|
// dictionary.
|
|||
|
var j = parseInt(i.ss(1, i.length - 1).rp(/z/g, 'x'), PROTECT_RADIX);
|
|||
|
return protectedStringArray[j];
|
|||
|
}
|
|||
|
|
|||
|
/** First-class function to pass to String.replace to protect a
|
|||
|
sequence defined by a regular expression. */
|
|||
|
function protector(match, protectee) {
|
|||
|
return protect(protectee);
|
|||
|
}
|
|||
|
|
|||
|
function protectorWithPrefix(match, prefix, protectee) {
|
|||
|
return prefix + protect(protectee);
|
|||
|
}
|
|||
|
|
|||
|
// SECTION HEADERS
|
|||
|
// This is common code for numbered headers. Nno-number ATX headers are processed
|
|||
|
// separately
|
|||
|
function makeHeaderFunc(level) {
|
|||
|
return function (match, header) {
|
|||
|
return '\n\n</p>\n<a ' + protect('class="target" name="' + mangle(removeHTMLTags(header)) + '"') +
|
|||
|
'> </a>' + entag('h' + level, header) + '\n<p>\n\n';
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (elementMode === undefined) {
|
|||
|
elementMode = true;
|
|||
|
}
|
|||
|
|
|||
|
if (str.innerHTML !== undefined) {
|
|||
|
str = str.innerHTML;
|
|||
|
}
|
|||
|
|
|||
|
// Prefix a newline so that blocks beginning at the top of the
|
|||
|
// document are processed correctly
|
|||
|
str = '\n\n' + str;
|
|||
|
|
|||
|
// Replace pre-formatted script tags that are used to protect
|
|||
|
// less-than signs, e.g., in std::vector<Value>
|
|||
|
str = str.rp(/<script\s+type\s*=\s*['"]preformatted['"]\s*>([\s\S]*?)<\/script>/gi, '$1');
|
|||
|
|
|||
|
function replaceDiagrams(str) {
|
|||
|
var result = extractDiagram(str);
|
|||
|
if (result.diagramString) {
|
|||
|
var CAPTION_REGEXP = /^\n*[ \t]*\[[^\n]+\][ \t]*(?=\n)/;
|
|||
|
result.afterString = result.afterString.rp(CAPTION_REGEXP, function (caption) {
|
|||
|
// Strip whitespace and enclosing brackets from the caption
|
|||
|
caption = caption.trim();
|
|||
|
caption = caption.ss(1, caption.length - 1);
|
|||
|
|
|||
|
result.caption = entag('center', entag('div', caption, protect('class="imagecaption"')));
|
|||
|
return '';
|
|||
|
});
|
|||
|
|
|||
|
var diagramSVG = diagramToSVG(result.diagramString, result.alignmentHint);
|
|||
|
var captionAbove = option('captionAbove', 'diagram')
|
|||
|
return result.beforeString +
|
|||
|
(result.caption && captionAbove ? result.caption : '') +
|
|||
|
diagramSVG +
|
|||
|
(result.caption && ! captionAbove ? result.caption : '') + '\n' +
|
|||
|
replaceDiagrams(result.afterString);
|
|||
|
} else {
|
|||
|
return str;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// CODE FENCES, with styles. Do this before other
|
|||
|
// processing so that their code is protected from further
|
|||
|
// Markdown processing
|
|||
|
var stylizeFence = function (cssClass, symbol) {
|
|||
|
var pattern = new RegExp('\\n([ \\t]*)' + symbol + '{3,}([ \\t]*\\S*)([ \\t]+.+)?\n([\\s\\S]+?)\n\\1' + symbol + '{3,}\\s*\n([ \t]*\\[.+(?:\n.+){0,3}\\])?', 'g');
|
|||
|
str = str.rp(pattern, function(match, indent, lang, cssSubClass, sourceCode, caption) {
|
|||
|
if (caption) {
|
|||
|
caption = caption.trim();
|
|||
|
caption = '<div ' + protect('class="listingcaption ' + cssClass + '"') + '>' + caption.ss(1, caption.length - 1) + '</div>\n';
|
|||
|
}
|
|||
|
lang = lang ? lang.trim() : lang;
|
|||
|
lang = lang ? [lang] : undefined;
|
|||
|
|
|||
|
// Remove the block's own indentation from each line of sourceCode
|
|||
|
sourceCode = sourceCode.rp(new RegExp('(^|\n)' + indent, 'g'), '$1');
|
|||
|
|
|||
|
var captionAbove = option('captionAbove', 'listing')
|
|||
|
|
|||
|
|
|||
|
var nextSourceCode, nextLang, nextCssSubClass;
|
|||
|
var body = '';
|
|||
|
do {
|
|||
|
nextSourceCode = nextLang = nextCssSubClass = undefined;
|
|||
|
sourceCode = sourceCode.rp(new RegExp('\\n([ \\t]*)' + symbol + '{3,}([ \\t]*\\S+)([ \\t]+.+)?\n([\\s\\S]*)'),
|
|||
|
function (match, indent, lang, cssSubClass, everythingElse) {
|
|||
|
nextLang = [lang];
|
|||
|
nextCssSubClass = cssSubClass;
|
|||
|
nextSourceCode = everythingElse;
|
|||
|
return '';
|
|||
|
});
|
|||
|
|
|||
|
// Highlight and append this block
|
|||
|
var highlighted = hljs.highlightAuto(sourceCode, lang).value;
|
|||
|
if (cssSubClass) {
|
|||
|
highlighted = entag('div', highlighted, 'class="' + cssSubClass + '"');
|
|||
|
}
|
|||
|
body += highlighted;
|
|||
|
|
|||
|
// Advance the next nested block
|
|||
|
sourceCode = nextSourceCode;
|
|||
|
lang = nextLang;
|
|||
|
cssSubClass = nextCssSubClass;
|
|||
|
} while (sourceCode);
|
|||
|
|
|||
|
// Insert paragraph close/open tags, since browsers force them anyway around pre tags
|
|||
|
// We need the indent in case this is a code block inside a list that is indented.
|
|||
|
return '\n' + indent + '</p>' + (caption && captionAbove ? caption : '') +
|
|||
|
protect(entag('pre', entag('code', body), 'class="listing ' + cssClass + '"')) +
|
|||
|
(caption && ! captionAbove ? caption : '') + '<p>\n';
|
|||
|
});
|
|||
|
};
|
|||
|
|
|||
|
stylizeFence('tilde', '~');
|
|||
|
stylizeFence('backtick', '`');
|
|||
|
|
|||
|
// Protect raw <CODE> content
|
|||
|
str = str.rp(/(<code\b.*?<\/code>)/gi, protector);
|
|||
|
|
|||
|
// Remove XML/HTML COMMENTS
|
|||
|
str = str.rp(/<!--\s[\s\S]+?\s-->/g, '');
|
|||
|
|
|||
|
str = replaceDiagrams(str);
|
|||
|
|
|||
|
// Protect SVG blocks (including the ones we just inserted)
|
|||
|
str = str.rp(/<svg( .*?)?>([\s\S]*?)<\/svg>/gi, function (match, attribs, body) {
|
|||
|
return '<svg' + protect(attribs) + '>' + protect(body) + '</svg>';
|
|||
|
});
|
|||
|
|
|||
|
// Protect STYLE blocks
|
|||
|
str = str.rp(/<style>([\s\S]*?)<\/style>/gi, function (match, body) {
|
|||
|
return entag('style', protect(body));
|
|||
|
});
|
|||
|
|
|||
|
// Protect the very special case of img tags with newlines and
|
|||
|
// breaks in them AND mismatched angle brackets. This happens for
|
|||
|
// gravizo graphs.
|
|||
|
str = str.rp(/<img\s+src=(["'])[\s\S]*?\1\s*>/gi, function (match, quote) {
|
|||
|
// Strip the "<img " and ">", and then protect:
|
|||
|
return "<img " + protect(match.ss(5, match.length - 1)) + ">";
|
|||
|
});
|
|||
|
|
|||
|
// INLINE CODE: Surrounded in back ticks on a single line. Do this before any other
|
|||
|
// processing to protect code blocks from further interference. Don't process back ticks
|
|||
|
// inside of code fences. Allow a single newline, but not wrapping further because that
|
|||
|
// might just pick up quotes used as other punctuation across lines. Explicitly exclude
|
|||
|
// cases where the second quote immediately preceeds a number, e.g., "the old `97"
|
|||
|
str = str.rp(/(`)(.+?(?:\n.+?)?)`(?!\d)/g, entag('code', '$2'));
|
|||
|
|
|||
|
// CODE: Escape angle brackets inside code blocks (including the ones we just introduced),
|
|||
|
// and then protect the blocks themselves
|
|||
|
str = str.rp(/(<code(?: .*?)?>)([\s\S]*?)<\/code>/gi, function (match, open, inlineCode) {
|
|||
|
return protect(open + escapeHTMLEntities(inlineCode) + '</code>');
|
|||
|
});
|
|||
|
|
|||
|
// PRE: Protect pre blocks
|
|||
|
str = str.rp(/(<pre\b[\s\S]*?<\/pre>)/gi, protector);
|
|||
|
|
|||
|
// Protect raw HTML attributes from processing
|
|||
|
str = str.rp(/(<\w[^ \n<>]*?[ \t]+)(.*?)(?=\/?>)/g, protectorWithPrefix);
|
|||
|
|
|||
|
// End of processing literal blocks
|
|||
|
/////////////////////////////////////////////////////////////////////////////
|
|||
|
|
|||
|
// Temporarily hide $$ MathJax LaTeX blocks from Markdown processing (this must
|
|||
|
// come before single $ block detection below)
|
|||
|
str = str.rp(/(\$\$[\s\S]+?\$\$)/g, protector);
|
|||
|
|
|||
|
// Convert LaTeX $ ... $ to MathJax, but verify that this
|
|||
|
// actually looks like math and not just dollar
|
|||
|
// signs. Don't rp double-dollar signs. Do this only
|
|||
|
// outside of protected blocks.
|
|||
|
|
|||
|
// Also allow LaTeX of the form $...$ if the close tag is not US$ or Can$
|
|||
|
// and there are spaces outside of the dollar signs.
|
|||
|
//
|
|||
|
// Test: " $3 or US$2 and 3$, $x$ $y + \n 2x$ or ($z$) $k$. or $2 or $2".match(pattern) =
|
|||
|
// ["$x$", "$y + 2x$", "$z$", "$k$"];
|
|||
|
str = str.rp(/((?:[^\w\d]))\$(\S(?:[^\$]*?\S(?!US|Can))??)\$(?![\w\d])/g, '$1\\($2\\)');
|
|||
|
|
|||
|
//
|
|||
|
// Literally: find a non-dollar sign, non-number followed
|
|||
|
// by a dollar sign and a space. Then, find any number of
|
|||
|
// characters until the same pattern reversed, allowing
|
|||
|
// one punctuation character before the final space. We're
|
|||
|
// trying to exclude things like Canadian 1$ and US $1
|
|||
|
// triggering math mode.
|
|||
|
|
|||
|
str = str.rp(/((?:[^\w\d]))\$([ \t][^\$]+?[ \t])\$(?![\w\d])/g, '$1\\($2\\)');
|
|||
|
|
|||
|
// Temporarily hide MathJax LaTeX blocks from Markdown processing
|
|||
|
str = str.rp(/(\\\([\s\S]+?\\\))/g, protector);
|
|||
|
str = str.rp(/(\\begin\{equation\}[\s\S]*?\\end\{equation\})/g, protector);
|
|||
|
str = str.rp(/(\\begin\{eqnarray\}[\s\S]*?\\end\{eqnarray\})/g, protector);
|
|||
|
str = str.rp(/(\\begin\{equation\*\}[\s\S]*?\\end\{equation\*\})/g, protector);
|
|||
|
|
|||
|
// For headers, we consume leading and trailing whitespace to avoid creating an
|
|||
|
// extra paragraph tag around the header itself.
|
|||
|
|
|||
|
// Setext-style H1: Text with ======== right under it
|
|||
|
str = str.rp(/(?:^|\s*\n)(.+?)\n[ \t]*={3,}[ \t]*\n/g, makeHeaderFunc(1));
|
|||
|
|
|||
|
// Setext-style H2: Text with -------- right under it
|
|||
|
str = str.rp(/(?:^|\s*\n)(.+?)\n[ \t]*-{3,}[ \t]*\n/g, makeHeaderFunc(2));
|
|||
|
|
|||
|
// ATX-style headers:
|
|||
|
for (var i = 6; i > 0; --i) {
|
|||
|
str = str.rp(new RegExp(/^\s*/.source + '#{' + i + ',' + i +'}(?:[ \t])([^\n#]+)#*[ \t]*\n', 'gm'),
|
|||
|
makeHeaderFunc(i));
|
|||
|
|
|||
|
// No-number headers
|
|||
|
str = str.rp(new RegExp(/^\s*/.source + '\\(#{' + i + ',' + i +'}\\)(?:[ \t])([^\n#]+)\\(?#*\\)?\\n[ \t]*\n', 'gm'),
|
|||
|
'\n</p>\n' + entag('div', '$1', protect('class="nonumberh' + i + '"')) + '\n<p>\n\n');
|
|||
|
}
|
|||
|
|
|||
|
// HORIZONTAL RULE: * * *, - - -, _ _ _
|
|||
|
str = str.rp(/\n[ \t]*((\*|-|_)[ \t]*){3,}[ \t]*\n/g, '\n<hr/>\n');
|
|||
|
|
|||
|
// PAGE BREAK or HORIZONTAL RULE: +++++
|
|||
|
str = str.rp(/\n[ \t]*\+{5,}[ \t]*\n/g, '\n<hr ' + protect('class="pagebreak"') + '/>\n');
|
|||
|
|
|||
|
// ADMONITION: !!! (class) (title)\n body
|
|||
|
str = str.rp(/^!!![ \t]*([^\s"'><&\:]*)\:?(.*)\n([ \t]{3,}.*\s*\n)*/gm, function (match, cssClass, title) {
|
|||
|
// Have to extract the body by splitting match because the regex doesn't capture the body correctly in the multi-line case
|
|||
|
match = match.trim();
|
|||
|
return '\n\n' + entag('div', ((title ? entag('div', title, protect('class="admonition-title"')) + '\n' : '') + match.ss(match.indexOf('\n'))).trim(), protect('class="admonition ' + cssClass.toLowerCase().trim() + '"')) + '\n\n';
|
|||
|
});
|
|||
|
|
|||
|
// FANCY QUOTE in a blockquote:
|
|||
|
// > " .... "
|
|||
|
// > -- Foo
|
|||
|
|
|||
|
var FANCY_QUOTE = protect('class="fancyquote"');
|
|||
|
str = str.rp(/\n>[ \t]*"(.*(?:\n>.*)*)"[ \t]*(?:\n>[ \t]*)?(\n>[ \t]{2,}\S.*)?\n/g,
|
|||
|
function (match, quote, author) {
|
|||
|
return entag('blockquote',
|
|||
|
entag('span',
|
|||
|
quote.rp(/\n>/g, '\n'),
|
|||
|
FANCY_QUOTE) +
|
|||
|
(author ? entag('span',
|
|||
|
author.rp(/\n>/g, '\n'),
|
|||
|
protect('class="author"')) : ''),
|
|||
|
FANCY_QUOTE);
|
|||
|
});
|
|||
|
|
|||
|
// BLOCKQUOTE: > in front of a series of lines
|
|||
|
// Process iteratively to support nested blockquotes
|
|||
|
var foundBlockquote = false;
|
|||
|
do {
|
|||
|
foundBlockquote = false;
|
|||
|
str = str.rp(/(?:\n>.*){2,}/g, function (match) {
|
|||
|
// Strip the leading '>'
|
|||
|
foundBlockquote = true;
|
|||
|
return entag('blockquote', match.rp(/\n>/g, '\n'));
|
|||
|
});
|
|||
|
} while (foundBlockquote);
|
|||
|
|
|||
|
|
|||
|
// FOOTNOTES/ENDNOTES: [^symbolic name]. Disallow spaces in footnote names to
|
|||
|
// make parsing unambiguous
|
|||
|
str = str.rp(/\s*\[\^(\S+)\](?!:)/g, function (match, symbolicName) {
|
|||
|
symbolicName = symbolicName.toLowerCase().trim();
|
|||
|
|
|||
|
if (! (symbolicName in endNoteTable)) {
|
|||
|
++endNoteCount;
|
|||
|
endNoteTable[symbolicName] = endNoteCount;
|
|||
|
}
|
|||
|
|
|||
|
return '<sup><a ' + protect('href="#endnote-' + symbolicName + '"') +
|
|||
|
'>' + endNoteTable[symbolicName] + '</a></sup>';
|
|||
|
});
|
|||
|
|
|||
|
// CITATIONS: [#symbolicname]
|
|||
|
// The reference: (don't use \S+ because it can grab trailing punctuation)
|
|||
|
str = str.rp(/\[#([^\)\(\[\]\.#\s]+)\](?!:)/g, function (match, symbolicName) {
|
|||
|
symbolicName = symbolicName.trim();
|
|||
|
return '[<a ' + protect('href="#citation-' + symbolicName.toLowerCase() + '"') +
|
|||
|
'>' + symbolicName + '</a>]';
|
|||
|
});
|
|||
|
|
|||
|
// The bibliography entry:
|
|||
|
str = str.rp(/\n\[#(\S+)\]:[ \t]+((?:[ \t]*\S[^\n]*\n?)*)/g, function (match, symbolicName, entry) {
|
|||
|
symbolicName = symbolicName.trim();
|
|||
|
return '<div ' + protect('class="bib"') + '>[<a ' + protect('class="target" name="citation-' + symbolicName.toLowerCase() + '"') +
|
|||
|
'> </a><b>' + symbolicName + '</b>] ' + entry + '</div>';
|
|||
|
});
|
|||
|
|
|||
|
// TABLES: line with | over line containing only | and -
|
|||
|
// (process before reference links to avoid ambiguity on the captions)
|
|||
|
str = replaceTables(str, protect);
|
|||
|
|
|||
|
// REFERENCE-LINKS: [foo][] or [bar][foo] + [foo]: http://foo.com
|
|||
|
str = str.rp(/^\[([^\^#].*?)\]:(.*?)$/gm, function (match, symbolicName, url) {
|
|||
|
referenceLinkTable[symbolicName.toLowerCase().trim()] = {link: url.trim(), used: false};
|
|||
|
return '';
|
|||
|
});
|
|||
|
|
|||
|
// E-MAIL ADDRESS: <foo@bar.baz> or foo@bar.baz
|
|||
|
str = str.rp(/(?:<|(?!<)\b)(\S+@(\S+\.)+?\S{3,}?)(?:$|>|(?=<)|(?=\s)(?!>))/g, function (match, addr) {
|
|||
|
return '<a ' + protect('href="mailto:' + addr + '"') + '>' + addr + '</a>';
|
|||
|
});
|
|||
|
|
|||
|
// Common code for formatting images
|
|||
|
var formatImage = function (ignore, url, attribs) {
|
|||
|
attribs = attribs || '';
|
|||
|
var img;
|
|||
|
var hash;
|
|||
|
|
|||
|
// Detect videos
|
|||
|
if (/(.mp4|.m4v|.avi|.mpg|.mov)$/i.test(url)) {
|
|||
|
// This is video. Any attributes provided will override the defaults given here
|
|||
|
img = '<video ' + protect('class="markdeep" src="' + url + '"' + attribs + ' width="480px" controls="true"') + '/>';
|
|||
|
} else if (hash = url.match(/^https:\/\/(?:www\.)?(?:youtube\.com\/\S*?v=|youtu\.be\/)([\w\d-]+)(&.*)?$/i)) {
|
|||
|
// Youtube video
|
|||
|
img = '<iframe ' + protect('class="markdeep" src="https://www.youtube.com/embed/' + hash[1] + '"' + attribs + ' width="480px" height="300px" frameborder="0" allowfullscreen webkitallowfullscreen mozallowfullscreen') + '></iframe>';
|
|||
|
} else if (hash = url.match(/^https:\/\/(?:www\.)?vimeo.com\/\S*?\/([\w\d-]+)$/i)) {
|
|||
|
// Vimeo video
|
|||
|
img = '<iframe ' + protect('class="markdeep" src="https://player.vimeo.com/video/' + hash[1] + '"' + attribs + ' width="480px" height="300px" frameborder="0" allowfullscreen webkitallowfullscreen mozallowfullscreen') + '></iframe>';
|
|||
|
} else {
|
|||
|
// Image (trailing space is needed in case attribs must be quoted by the
|
|||
|
// browser...without the space, the browser will put the closing slash in the
|
|||
|
// quotes.)
|
|||
|
img = '<img ' + protect('class="markdeep" src="' + url + '"' + attribs) + ' />';
|
|||
|
|
|||
|
// Check for width or height (or max-width and max-height). If they exist,
|
|||
|
// link this to the full-size image as well. The current code ALWAYS makes
|
|||
|
// this link.
|
|||
|
//if (/\b(width|height)\b/i.test(attribs)) {
|
|||
|
img = entag('a ', img, protect('href="' + url + '" target="_blank"'));
|
|||
|
//}
|
|||
|
}
|
|||
|
|
|||
|
return img;
|
|||
|
};
|
|||
|
|
|||
|
// Reformat figure links that have subfigure labels in parentheses, to avoid them being
|
|||
|
// processed as links
|
|||
|
str = str.rp(/\b(figure|fig\.|table|tbl\.|listing|lst\.)\s*\[([^\s\]]+)\](?=\()/gi, function (match) {
|
|||
|
return match + '<span/>';
|
|||
|
});
|
|||
|
|
|||
|
|
|||
|
// Process links before images so that captions can contain links
|
|||
|
|
|||
|
// Detect gravizo URLs inside of markdown images and protect them,
|
|||
|
// which will cause them to be parsed sort-of reasonably. This is
|
|||
|
// a really special case needed to handle the newlines and potential
|
|||
|
// nested parentheses. Use the pattern from http://blog.stevenlevithan.com/archives/regex-recursion
|
|||
|
// (could be extended to multiple nested parens if needed)
|
|||
|
str = str.rp(/\(http:\/\/g.gravizo.com\/g\?((?:[^\(\)]|\([^\(\)]*\))*)\)/gi, function(match, url) {
|
|||
|
return "(http://g.gravizo.com/g?" + encodeURIComponent(url) + ")";
|
|||
|
});
|
|||
|
|
|||
|
// HYPERLINKS: [text](url attribs)
|
|||
|
str = str.rp(/(^|[^!])\[([^\[\]]+?)\]\(("?)([^<>\s"]+?)\3(\s+[^\)]*?)?\)/g, function (match, pre, text, maybeQuote, url, attribs) {
|
|||
|
attribs = attribs || '';
|
|||
|
return pre + '<a ' + protect('href="' + url + '"' + attribs) + '>' + text + '</a>' + maybeShowLabel(url);
|
|||
|
});
|
|||
|
|
|||
|
// EMPTY HYPERLINKS: [](url)
|
|||
|
str = str.rp(/(^|[^!])\[[ \t]*?\]\(("?)([^<>\s"]+?)\2\)/g, function (match, pre, maybeQuote, url) {
|
|||
|
return pre + '<a ' + protect('href="' + url + '"') + '>' + url + '</a>';
|
|||
|
});
|
|||
|
|
|||
|
// IMAGE GRID: Rewrite rows and grids of images into a grid
|
|||
|
var imageGridAttribs = protect('width="100%"');
|
|||
|
var imageGridRowAttribs = protect('valign="top"');
|
|||
|
str = str.rp(/(?:\n(?:[ \t]*!\[[^\n]*?\]\(("?)[^<>\s]+?(?:[^\n\)]*?)?\)){2,}[ \t]*)+\n/g, function (match) {
|
|||
|
var table = '';
|
|||
|
|
|||
|
// Break into rows:
|
|||
|
match = match.split('\n');
|
|||
|
|
|||
|
// Parse each row:
|
|||
|
match.forEach(function(row) {
|
|||
|
row = row.trim();
|
|||
|
if (row) {
|
|||
|
// Parse each image
|
|||
|
table += entag('tr', row.rp(/[ \t]*!\[[^\n]*?\]\([^\)\s]+([^\)]*?)?\)/g, function(image, attribs) {
|
|||
|
//if (! /width|height/i.test(attribs) {
|
|||
|
// Add a bogus "width" attribute to force the images to be hyperlinked to their
|
|||
|
// full-resolution versions
|
|||
|
//}
|
|||
|
return entag('td', '\n\n'+ image + '\n\n');
|
|||
|
}), imageGridRowAttribs);
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
return '\n' + entag('table', table, imageGridAttribs) + '\n';
|
|||
|
});
|
|||
|
|
|||
|
// SIMPLE IMAGE: ![](url attribs)
|
|||
|
str = str.rp(/(\s*)!\[\]\(("?)([^"<>\s]+?)\2(\s[^\)]*?)?\)(\s*)/g, function (match, preSpaces, maybeQuote, url, attribs, postSpaces) {
|
|||
|
var img = formatImage(match, url, attribs);
|
|||
|
|
|||
|
if (isolated(preSpaces, postSpaces)) {
|
|||
|
// In a block by itself: center
|
|||
|
img = entag('center', img);
|
|||
|
}
|
|||
|
|
|||
|
return preSpaces + img + postSpaces;
|
|||
|
});
|
|||
|
|
|||
|
// Explicit loop so that the output will be re-processed, preserving spaces between blocks.
|
|||
|
// Note that there is intentionally no global flag on the first regexp since we only want
|
|||
|
// to process the first occurance.
|
|||
|
var loop = true;
|
|||
|
var imageCaptionAbove = option('captionAbove', 'image');
|
|||
|
while (loop) {
|
|||
|
loop = false;
|
|||
|
|
|||
|
// CAPTIONED IMAGE: ![caption](url attribs)
|
|||
|
str = str.rp(/(\s*)!\[([\s\S]+?)?\]\(("?)([^"<>\s]+?)\3(\s[^\)]*?)?\)(\s*)/, function (match, preSpaces, caption, maybeQuote, url, attribs, postSpaces) {
|
|||
|
|
|||
|
loop = true;
|
|||
|
var divStyle = '';
|
|||
|
var iso = isolated(preSpaces, postSpaces);
|
|||
|
|
|||
|
// Only floating images get their size attributes moved to the whole box
|
|||
|
if (attribs && ! iso) {
|
|||
|
// Move any width *attribute* specification to the box itself
|
|||
|
attribs = attribs.rp(/((?:max-)?width)\s*:\s*[^;'"]*/g, function (attribMatch, attrib) {
|
|||
|
divStyle = attribMatch + ';';
|
|||
|
return attrib + ':100%';
|
|||
|
});
|
|||
|
|
|||
|
// Move any width *style* specification to the box itself
|
|||
|
attribs = attribs.rp(/((?:max-)?width)\s*=\s*('\S+?'|"\S+?")/g, function (attribMatch, attrib, expr) {
|
|||
|
// Strip the quotes
|
|||
|
divStyle = attrib + ':' + expr.ss(1, expr.length - 1) + ';';
|
|||
|
return 'style="' + attrib + ':100%" ';
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
var img = formatImage(match, url, attribs);
|
|||
|
|
|||
|
if (iso) {
|
|||
|
// In its own block: center
|
|||
|
preSpaces += '<center>';
|
|||
|
postSpaces = '</center>' + postSpaces;
|
|||
|
} else {
|
|||
|
// Embedded: float
|
|||
|
divStyle += 'float:right;margin:4px 0px 0px 25px;'
|
|||
|
}
|
|||
|
|
|||
|
caption = entag('div', caption + maybeShowLabel(url), protect('class="imagecaption"'));
|
|||
|
return preSpaces +
|
|||
|
entag('div', (imageCaptionAbove ? caption : '') + img + (! imageCaptionAbove ? caption : ''), protect('class="image" style="' + divStyle + '"')) +
|
|||
|
postSpaces;
|
|||
|
});
|
|||
|
} // while replacements made
|
|||
|
|
|||
|
// Process these after links, so that URLs with underscores and tildes are protected.
|
|||
|
|
|||
|
// STRONG: Must run before italic, since they use the
|
|||
|
// same symbols. **b** __b__
|
|||
|
str = replaceMatched(str, /\*\*/, 'strong', protect('class="asterisk"'));
|
|||
|
str = replaceMatched(str, /__/, 'strong', protect('class="underscore"'));
|
|||
|
|
|||
|
// EM (ITALICS): *i* _i_
|
|||
|
str = replaceMatched(str, /\*/, 'em', protect('class="asterisk"'));
|
|||
|
str = replaceMatched(str, /_/, 'em', protect('class="underscore"'));
|
|||
|
|
|||
|
// STRIKETHROUGH: ~~text~~
|
|||
|
str = str.rp(/\~\~([^~].*?)\~\~/g, entag('del', '$1'));
|
|||
|
|
|||
|
// SMART DOUBLE QUOTES: "a -> “ z" -> ”
|
|||
|
// Allow situations such as "foo"==>"bar" and foo:"bar", but not 3' 9"
|
|||
|
str = str.rp(/(^|[ \t->])(")(?=\w)/gm, '$1“');
|
|||
|
str = str.rp(/([A-Za-z\.,:;\?!=<])(")(?=$|\W)/gm, '$1”');
|
|||
|
|
|||
|
// ARROWS:
|
|||
|
str = str.rp(/(\s|^)<==(\s)/g, '$1\u21D0$2');
|
|||
|
str = str.rp(/(\s|^)->(\s)/g, '$1→$2');
|
|||
|
// (this requires having removed HTML comments first)
|
|||
|
str = str.rp(/(\s|^)-->(\s)/g, '$1⟶$2');
|
|||
|
str = str.rp(/(\s|^)==>(\s)/g, '$1\u21D2$2');
|
|||
|
str = str.rp(/(\s|^)<-(\s)/g, '$1←$2');
|
|||
|
str = str.rp(/(\s|^)<--(\s)/g, '$1⟵$2');
|
|||
|
str = str.rp(/(\s|^)<==>(\s)/g, '$1\u21D4$2');
|
|||
|
str = str.rp(/(\s|^)<->(\s)/g, '$1\u2194$2');
|
|||
|
|
|||
|
// EM DASH: ---
|
|||
|
// (exclude things that look like table delimiters!)
|
|||
|
str = str.rp(/([^-!\:\|])---([^->\:\|])/g, '$1—$2');
|
|||
|
|
|||
|
// other EM DASH: -- (we don't support en dash...it is too short and looks like a minus)
|
|||
|
// (exclude things that look like table delimiters!)
|
|||
|
str = str.rp(/([^-!\:\|])--([^->\:\|])/g, '$1—$2');
|
|||
|
|
|||
|
// NUMBER x NUMBER:
|
|||
|
str = str.rp(/(\d+\s?)x(\s?\d+)/g, '$1×$2');
|
|||
|
|
|||
|
// MINUS: -4 or 2 - 1
|
|||
|
str = str.rp(/([\s\(\[<\|])-(\d)/g, '$1−$2');
|
|||
|
str = str.rp(/(\d) - (\d)/g, '$1 − $2');
|
|||
|
|
|||
|
// EXPONENTS: ^1 ^-1 (no decimal places allowed)
|
|||
|
str = str.rp(/\^([-+]?\d+)\b/g, '<sup>$1</sup>');
|
|||
|
|
|||
|
// PAGE BREAK:
|
|||
|
str = str.rp(/\b\\(pagebreak|newpage)\b/gi, protect('<div style="page-break-after:always"> </div>\n'))
|
|||
|
|
|||
|
// SCHEDULE LISTS: date : title followed by indented content
|
|||
|
str = replaceScheduleLists(str, protect);
|
|||
|
|
|||
|
// DEFINITION LISTS: Word followed by a colon list
|
|||
|
// Use <dl><dt>term</dt><dd>definition</dd></dl>
|
|||
|
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dl
|
|||
|
//
|
|||
|
// Process these before lists so that lists within definition lists
|
|||
|
// work correctly
|
|||
|
str = replaceDefinitionLists(str, protect);
|
|||
|
|
|||
|
// LISTS: lines with -, +, *, or number.
|
|||
|
str = replaceLists(str, protect);
|
|||
|
|
|||
|
// DEGREE: ##-degree
|
|||
|
str = str.rp(/(\d+?)[ \t-]degree(?:s?)/g, '$1°');
|
|||
|
|
|||
|
// PARAGRAPH: Newline, any amount of space, newline...as long as there isn't already
|
|||
|
// a paragraph break there.
|
|||
|
str = str.rp(/(?:<p>)?\n\s*\n+(?!<\/p>)/gi,
|
|||
|
function(match) { return (/^<p>/i.test(match)) ? match : '\n\n</p><p>\n\n';});
|
|||
|
|
|||
|
// Remove empty paragraphs (mostly avoided by the above, but some can still occur)
|
|||
|
str = str.rp(/<p>[\s\n]*<\/p>/gi, '');
|
|||
|
|
|||
|
// Reference links
|
|||
|
str = str.rp(/\[(.+?)\]\[(.*?)\]/g, function (match, text, symbolicName) {
|
|||
|
// Empty symbolic name is replaced by the text
|
|||
|
if (! symbolicName.trim()) {
|
|||
|
symbolicName = text;
|
|||
|
}
|
|||
|
|
|||
|
symbolicName = symbolicName.toLowerCase().trim();
|
|||
|
var t = referenceLinkTable[symbolicName];
|
|||
|
if (! t) {
|
|||
|
console.log("Reference link '" + symbolicName + "' never defined");
|
|||
|
return '?';
|
|||
|
} else {
|
|||
|
t.used = true;
|
|||
|
return '<a ' + protect('href="' + t.link + '"') + '>' + text + '</a>';
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
|
|||
|
// FOOTNOTES/ENDNOTES
|
|||
|
str = str.rp(/\n\[\^(\S+)\]: ((?:.+?\n?)*)/g, function (match, symbolicName, note) {
|
|||
|
symbolicName = symbolicName.toLowerCase().trim();
|
|||
|
if (symbolicName in endNoteTable) {
|
|||
|
return '\n<div ' + protect('class="endnote"') + '><a ' +
|
|||
|
protect('class="target" name="endnote-' + symbolicName + '"') +
|
|||
|
'> </a><sup>' + endNoteTable[symbolicName] + '</sup> ' + note + '</div>';
|
|||
|
} else {
|
|||
|
return "\n";
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
|
|||
|
// SECTION LINKS: XXX section, XXX subsection.
|
|||
|
// Do this by rediscovering the headers and then recursively
|
|||
|
// searching for links to them. Process after other
|
|||
|
// forms of links to avoid ambiguity.
|
|||
|
|
|||
|
var allHeaders = str.match(/<h([1-6])>(.*?)<\/h\1>/gi);
|
|||
|
if (allHeaders) {
|
|||
|
allHeaders.forEach(function (header) {
|
|||
|
header = removeHTMLTags(header.ss(4, header.length - 5)).trim();
|
|||
|
var link = '<a ' + protect('href="#' + mangle(header) + '"') + '>';
|
|||
|
// Search for links to this section
|
|||
|
str = str.rp(RegExp("(\\b" + escapeRegExpCharacters(header) + ")(?=\\s" + keyword('subsection') + "|\\s" +
|
|||
|
keyword('section') + ")", 'gi'),
|
|||
|
link + "$1</a>");
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
// TABLE, LISTING, and FIGURE LABEL NUMBERING: Figure [symbol]: Table [symbol]: Listing [symbol]: Diagram [symbols]:
|
|||
|
|
|||
|
// This data structure maps caption types [by localized name] to a count of how many of
|
|||
|
// that type of object exist.
|
|||
|
var refCounter = {};
|
|||
|
|
|||
|
// refTable['type_symbolicName'] = {number: number to link to, used: bool}
|
|||
|
var refTable = {};
|
|||
|
|
|||
|
str = str.rp(RegExp(/($|>)\s*/.source + '(' + keyword('figure') + '|' + keyword('table') + '|' + keyword('listing') + '|' + keyword('diagram') + ')' + /\s+\[(.+?)\]:/.source, 'gim'), function (match, prefix, _type, _ref) {
|
|||
|
var type = _type.toLowerCase();
|
|||
|
// Increment the counter
|
|||
|
var count = refCounter[type] = (refCounter[type] | 0) + 1;
|
|||
|
var ref = type + '_' + mangle(_ref.toLowerCase().trim());
|
|||
|
|
|||
|
// Store the reference number
|
|||
|
refTable[ref] = {number: count, used: false, source: type + ' [' + _ref + ']'};
|
|||
|
|
|||
|
return prefix +
|
|||
|
entag('a', ' ', protect('class="target" name="' + ref + '"')) + entag('b', type[0].toUpperCase() + type.ss(1) + ' ' + count + ':', protect('style="font-style:normal;"')) +
|
|||
|
maybeShowLabel(_ref);
|
|||
|
});
|
|||
|
|
|||
|
// FIGURE, TABLE, and LISTING references:
|
|||
|
// (must come after figure/table/listing processing, obviously)
|
|||
|
str = str.rp(/\b(figure|fig\.|table|tbl\.|listing|lst\.)\s+\[([^\s\]]+)\]/gi, function (match, _type, _ref) {
|
|||
|
// Fix abbreviations
|
|||
|
var type = _type.toLowerCase();
|
|||
|
switch (type) {
|
|||
|
case 'fig.': type = 'figure'; break;
|
|||
|
case 'tbl.': type = 'table'; break;
|
|||
|
case 'lst.': type = 'listing'; break;
|
|||
|
}
|
|||
|
|
|||
|
// Clean up the reference
|
|||
|
var ref = type + '_' + mangle(_ref.toLowerCase().trim());
|
|||
|
var t = refTable[ref];
|
|||
|
|
|||
|
if (t) {
|
|||
|
t.used = true;
|
|||
|
return '<a ' + protect('href="#' + ref + '"') + '>' + _type + ' ' + t.number + maybeShowLabel(_ref) + '</a>';
|
|||
|
} else {
|
|||
|
console.log("Reference to undefined '" + type + " [" + _ref + "]'");
|
|||
|
return _type + ' ?';
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
// URL: <http://baz> or http://baz
|
|||
|
// Must be detected after [link]() processing
|
|||
|
str = str.rp(/(?:<|(?!<)\b)(\w{3,6}:\/\/.+?)(?:$|>|(?=<)|(?=\s|\u00A0)(?!<))/g, function (match, url) {
|
|||
|
var extra = '';
|
|||
|
if (url[url.length - 1] == '.') {
|
|||
|
// Accidentally sucked in a period at the end of a sentence
|
|||
|
url = url.ss(0, url.length - 1);
|
|||
|
extra = '.';
|
|||
|
}
|
|||
|
// svn and perforce URLs are not hyperlinked. All others (http/https/ftp/mailto/tel, etc. are)
|
|||
|
return '<a ' + ((url[0] !== 's' && url[0] !== 'p') ? protect('href="' + url + '" class="url"') : '') + '>' + url + '</a>' + extra;
|
|||
|
});
|
|||
|
|
|||
|
if (! elementMode) {
|
|||
|
var TITLE_PATTERN = /^\s*(?:<\/p><p>)?\s*<strong.*?>([^ \t\*].*?[^ \t\*])<\/strong>(?:<\/p>)?[ \t]*\n/.source;
|
|||
|
|
|||
|
var ALL_SUBTITLES_PATTERN = /([ {4,}\t][ \t]*\S.*\n)*/.source;
|
|||
|
|
|||
|
// Detect a bold first line and make it into a title; detect indented lines
|
|||
|
// below it and make them subtitles
|
|||
|
str = str.rp(
|
|||
|
new RegExp(TITLE_PATTERN + ALL_SUBTITLES_PATTERN, 'g'),
|
|||
|
function (match, title) {
|
|||
|
title = title.trim();
|
|||
|
|
|||
|
// rp + RegExp won't give us the full list of
|
|||
|
// subtitles, only the last one. So, we have to
|
|||
|
// re-process match.
|
|||
|
var subtitles = match.ss(match.indexOf('\n', match.indexOf('</strong>')));
|
|||
|
subtitles = subtitles ? subtitles.rp(/[ \t]*(\S.*?)\n/g, '<div class="subtitle"> $1 </div>\n') : '';
|
|||
|
|
|||
|
// Remove all tags from the title when inside the <TITLE> tag
|
|||
|
return entag('title', removeHTMLTags(title)) + maybeShowLabel(window.location.href, 'center') +
|
|||
|
'<div class="title"> ' + title +
|
|||
|
' </div>\n' + subtitles + '<div class="afterTitles"></div>\n';
|
|||
|
});
|
|||
|
} // if ! noTitles
|
|||
|
|
|||
|
// Remove any bogus leading close-paragraph tag inserted by our extra newlines
|
|||
|
str = str.rp(/^\s*<\/p>/, '');
|
|||
|
|
|||
|
// If not in element mode and not an INSERT child, maybe add a TOC
|
|||
|
if (! elementMode) {// && ! myURLParse[2]) {
|
|||
|
var temp = insertTableOfContents(str, protect);
|
|||
|
str = temp[0];
|
|||
|
var toc = temp[1];
|
|||
|
// SECTION LINKS: Replace sec. [X], section [X], subsection [X]
|
|||
|
str = str.rp(RegExp('\\b(' + keyword('sec') + '\\.|' + keyword('section') + '|' + keyword('subsection') + ')\\s\\[(.+?)\\]', 'gi'),
|
|||
|
function (match, prefix, ref) {
|
|||
|
var link = toc[ref.toLowerCase().trim()];
|
|||
|
if (link) {
|
|||
|
return prefix + ' <a ' + protect('href="#toc' + link + '"') + '>' + link + '</a>';
|
|||
|
} else {
|
|||
|
return prefix + ' ?';
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
// Expose all protected values. We may need to do this
|
|||
|
// recursively, because pre and code blocks can be nested.
|
|||
|
while (str.indexOf(PROTECT_CHARACTER) + 1) {
|
|||
|
str = str.rp(PROTECT_REGEXP, expose);
|
|||
|
}
|
|||
|
|
|||
|
// Warn about unused references
|
|||
|
Object.keys(referenceLinkTable).forEach(function (key) {
|
|||
|
if (! referenceLinkTable[key].used) {
|
|||
|
console.log("Reference link '[" + key + "]' is defined but never used");
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
Object.keys(refTable).forEach(function (key) {
|
|||
|
if (! refTable[key].used) {
|
|||
|
console.log("'" + refTable[key].source + "' is never referenced");
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
return '<span class="md">' + entag('p', str) + '</span>';
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
Adds whitespace at the end of each line of str, so that all lines have equal length in
|
|||
|
unicode characters (which is not the same as JavaScript characters when high-index/escape
|
|||
|
characters are present).
|
|||
|
*/
|
|||
|
function equalizeLineLengths(str) {
|
|||
|
var lineArray = str.split('\n');
|
|||
|
var longest = 0;
|
|||
|
lineArray.forEach(function(line) {
|
|||
|
longest = max(longest, Array.from(line).length);
|
|||
|
});
|
|||
|
|
|||
|
// Worst case spaces needed for equalizing lengths
|
|||
|
// http://stackoverflow.com/questions/1877475/repeat-character-n-times
|
|||
|
var spaces = Array(longest + 1).join(' ');
|
|||
|
|
|||
|
var result = '';
|
|||
|
lineArray.forEach(function(line) {
|
|||
|
// Append the needed number of spaces onto each line, and
|
|||
|
// reconstruct the output with newlines
|
|||
|
result += line + spaces.ss(Array.from(line).length) + '\n';
|
|||
|
});
|
|||
|
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
/** Finds the longest common whitespace prefix of all non-empty lines
|
|||
|
and then removes it */
|
|||
|
function removeLeadingSpace(str) {
|
|||
|
var lineArray = str.split('\n');
|
|||
|
|
|||
|
var minimum = Infinity;
|
|||
|
lineArray.forEach(function (line) {
|
|||
|
if (line.trim() !== '') {
|
|||
|
// This is a non-empty line
|
|||
|
var spaceArray = line.match(/^([ \t]*)/);
|
|||
|
if (spaceArray) {
|
|||
|
minimum = min(minimum, spaceArray[0].length);
|
|||
|
}
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
if (minimum === 0) {
|
|||
|
// No leading space
|
|||
|
return str;
|
|||
|
}
|
|||
|
|
|||
|
var result = '';
|
|||
|
lineArray.forEach(function(line) {
|
|||
|
// Strip the common spaces
|
|||
|
result += line.ss(minimum) + '\n';
|
|||
|
});
|
|||
|
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
/** Returns true if this character is a "letter" under the ASCII definition */
|
|||
|
function isASCIILetter(c) {
|
|||
|
var code = c.charCodeAt(0);
|
|||
|
return ((code >= 65) && (code <= 90)) || ((code >= 97) && (code <= 122));
|
|||
|
}
|
|||
|
|
|||
|
/** Converts diagramString, which is a Markdeep diagram without the surrounding asterisks, to
|
|||
|
SVG (HTML). Lines may have ragged lengths.
|
|||
|
|
|||
|
alignmentHint is the float alignment desired for the SVG tag,
|
|||
|
which can be 'floatleft', 'floatright', or ''
|
|||
|
*/
|
|||
|
function diagramToSVG(diagramString, alignmentHint) {
|
|||
|
// Clean up diagramString if line endings are ragged
|
|||
|
diagramString = equalizeLineLengths(diagramString);
|
|||
|
|
|||
|
// Temporarily replace 'o' that is surrounded by other text
|
|||
|
// with another character to avoid processing it as a point
|
|||
|
// decoration. This will be replaced in the final svg and is
|
|||
|
// faster than checking each neighborhood each time.
|
|||
|
var HIDE_O = '\ue004';
|
|||
|
diagramString = diagramString.rp(/([a-zA-Z]{2})o/g, '$1' + HIDE_O);
|
|||
|
diagramString = diagramString.rp(/o([a-zA-Z]{2})/g, HIDE_O + '$1');
|
|||
|
diagramString = diagramString.rp(/([a-zA-Z\ue004])o([a-zA-Z\ue004])/g, '$1' + HIDE_O + '$2');
|
|||
|
|
|||
|
/** Pixels per character */
|
|||
|
var SCALE = 8;
|
|||
|
|
|||
|
/** Multiply Y coordinates by this when generating the final SVG
|
|||
|
result to account for the aspect ratio of text files. This
|
|||
|
MUST be 2 */
|
|||
|
var ASPECT = 2;
|
|||
|
|
|||
|
var DIAGONAL_ANGLE = Math.atan(1.0 / ASPECT) * 180 / Math.PI;
|
|||
|
|
|||
|
var EPSILON = 1e-6;
|
|||
|
|
|||
|
// The order of the following is based on rotation angles
|
|||
|
// and is used for ArrowSet.toSVG
|
|||
|
var ARROW_HEAD_CHARACTERS = '>v<^';
|
|||
|
var POINT_CHARACTERS = 'o*';
|
|||
|
var JUMP_CHARACTERS = '()';
|
|||
|
var UNDIRECTED_VERTEX_CHARACTERS = "+";
|
|||
|
var VERTEX_CHARACTERS = UNDIRECTED_VERTEX_CHARACTERS + ".'";
|
|||
|
|
|||
|
// GRAY[i] is the Unicode block character for (i+1)/4 level gray
|
|||
|
var GRAY_CHARACTERS = '\u2591\u2592\u2593\u2594\u2589';
|
|||
|
|
|||
|
// TRI[i] is a right-triangle rotated by 90*i
|
|||
|
var TRI_CHARACTERS = '\u25E2\u25E3\u25E4\u25E5';
|
|||
|
|
|||
|
var DECORATION_CHARACTERS = ARROW_HEAD_CHARACTERS + POINT_CHARACTERS + JUMP_CHARACTERS + GRAY_CHARACTERS + TRI_CHARACTERS;
|
|||
|
|
|||
|
function isUndirectedVertex(c) { return UNDIRECTED_VERTEX_CHARACTERS.indexOf(c) + 1; }
|
|||
|
function isVertex(c) { return VERTEX_CHARACTERS.indexOf(c) !== -1; }
|
|||
|
function isTopVertex(c) { return isUndirectedVertex(c) || (c === '.'); }
|
|||
|
function isBottomVertex(c) { return isUndirectedVertex(c) || (c === "'"); }
|
|||
|
function isVertexOrLeftDecoration(c){ return isVertex(c) || (c === '<') || isPoint(c); }
|
|||
|
function isVertexOrRightDecoration(c){return isVertex(c) || (c === '>') || isPoint(c); }
|
|||
|
function isArrowHead(c) { return ARROW_HEAD_CHARACTERS.indexOf(c) + 1; }
|
|||
|
function isGray(c) { return GRAY_CHARACTERS.indexOf(c) + 1; }
|
|||
|
function isTri(c) { return TRI_CHARACTERS.indexOf(c) + 1; }
|
|||
|
|
|||
|
// "D" = Diagonal slash (/), "B" = diagonal Backslash (\)
|
|||
|
// Characters that may appear anywhere on a solid line
|
|||
|
function isSolidHLine(c) { return (c === '-') || isUndirectedVertex(c) || isJump(c); }
|
|||
|
function isSolidVLineOrJumpOrPoint(c) { return isSolidVLine(c) || isJump(c) || isPoint(c); }
|
|||
|
function isSolidVLine(c) { return (c === '|') || isUndirectedVertex(c); }
|
|||
|
function isSolidDLine(c) { return (c === '/') || isUndirectedVertex(c) }
|
|||
|
function isSolidBLine(c) { return (c === '\\') || isUndirectedVertex(c); }
|
|||
|
function isJump(c) { return JUMP_CHARACTERS.indexOf(c) + 1; }
|
|||
|
function isPoint(c) { return POINT_CHARACTERS.indexOf(c) + 1; }
|
|||
|
function isDecoration(c) { return DECORATION_CHARACTERS.indexOf(c) + 1; }
|
|||
|
function isEmpty(c) { return c === ' '; }
|
|||
|
|
|||
|
///////////////////////////////////////////////////////////////////////////////
|
|||
|
// Math library
|
|||
|
|
|||
|
/** Invoke as new Vec2(v) to clone or new Vec2(x, y) to create from coordinates.
|
|||
|
Can also invoke without new for brevity. */
|
|||
|
function Vec2(x, y) {
|
|||
|
// Detect when being run without new
|
|||
|
if (! (this instanceof Vec2)) { return new Vec2(x, y); }
|
|||
|
|
|||
|
if (y === undefined) {
|
|||
|
if (x === undefined) { x = y = 0; }
|
|||
|
else if (x instanceof Vec2) { y = x.y; x = x.x; }
|
|||
|
else { console.error("Vec2 requires one Vec2 or (x, y) as an argument"); }
|
|||
|
}
|
|||
|
this.x = x;
|
|||
|
this.y = y;
|
|||
|
Object.seal(this);
|
|||
|
}
|
|||
|
|
|||
|
/** Returns an SVG representation */
|
|||
|
Vec2.prototype.toString = Vec2.prototype.toSVG =
|
|||
|
function () { return '' + (this.x * SCALE) + ',' + (this.y * SCALE * ASPECT) + ' '; };
|
|||
|
|
|||
|
/** Converts a "rectangular" string defined by newlines into 2D
|
|||
|
array of characters. Grids are immutable. */
|
|||
|
function makeGrid(str) {
|
|||
|
/** Returns ' ' for out of bounds values */
|
|||
|
var grid = function(x, y) {
|
|||
|
if (y === undefined) {
|
|||
|
if (x instanceof Vec2) { y = x.y; x = x.x; }
|
|||
|
else { console.error('grid requires either a Vec2 or (x, y)'); }
|
|||
|
}
|
|||
|
|
|||
|
return ((x >= 0) && (x < grid.width) && (y >= 0) && (y < grid.height)) ?
|
|||
|
str[y * (grid.width + 1) + x] : ' ';
|
|||
|
};
|
|||
|
|
|||
|
// Elements are true when consumed
|
|||
|
grid._used = [];
|
|||
|
|
|||
|
grid.height = str.split('\n').length;
|
|||
|
if (str[str.length - 1] === '\n') { --grid.height; }
|
|||
|
|
|||
|
// Convert the string to an array to better handle greater-than 16-bit unicode
|
|||
|
// characters, which JavaScript does not process correctly with indices. Do this after
|
|||
|
// the above string processing.
|
|||
|
str = Array.from(str);
|
|||
|
grid.width = str.indexOf('\n');
|
|||
|
|
|||
|
/** Mark this location. Takes a Vec2 or (x, y) */
|
|||
|
grid.setUsed = function (x, y) {
|
|||
|
if (y === undefined) {
|
|||
|
if (x instanceof Vec2) { y = x.y; x = x.x; }
|
|||
|
else { console.error('grid requires either a Vec2 or (x, y)'); }
|
|||
|
}
|
|||
|
if ((x >= 0) && (x < grid.width) && (y >= 0) && (y < grid.height)) {
|
|||
|
// Match the source string indexing
|
|||
|
grid._used[y * (grid.width + 1) + x] = true;
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
grid.isUsed = function (x, y) {
|
|||
|
if (y === undefined) {
|
|||
|
if (x instanceof Vec2) { y = x.y; x = x.x; }
|
|||
|
else { console.error('grid requires either a Vec2 or (x, y)'); }
|
|||
|
}
|
|||
|
return (this._used[y * (this.width + 1) + x] === true);
|
|||
|
};
|
|||
|
|
|||
|
/** Returns true if there is a solid vertical line passing through (x, y) */
|
|||
|
grid.isSolidVLineAt = function (x, y) {
|
|||
|
if (y === undefined) { y = x.x; x = x.x; }
|
|||
|
|
|||
|
var up = grid(x, y - 1);
|
|||
|
var c = grid(x, y);
|
|||
|
var dn = grid(x, y + 1);
|
|||
|
|
|||
|
var uprt = grid(x + 1, y - 1);
|
|||
|
var uplt = grid(x - 1, y - 1);
|
|||
|
|
|||
|
if (isSolidVLine(c)) {
|
|||
|
// Looks like a vertical line...does it continue?
|
|||
|
return (isTopVertex(up) || (up === '^') || isSolidVLine(up) || isJump(up) ||
|
|||
|
isBottomVertex(dn) || (dn === 'v') || isSolidVLine(dn) || isJump(dn) ||
|
|||
|
isPoint(up) || isPoint(dn) || (grid(x, y - 1) === '_') || (uplt === '_') ||
|
|||
|
(uprt === '_') ||
|
|||
|
|
|||
|
// Special case of 1-high vertical on two curved corners
|
|||
|
((isTopVertex(uplt) || isTopVertex(uprt)) &&
|
|||
|
(isBottomVertex(grid(x - 1, y + 1)) || isBottomVertex(grid(x + 1, y + 1)))));
|
|||
|
|
|||
|
} else if (isTopVertex(c) || (c === '^')) {
|
|||
|
// May be the top of a vertical line
|
|||
|
return isSolidVLine(dn) || (isJump(dn) && (c !== '.'));
|
|||
|
} else if (isBottomVertex(c) || (c === 'v')) {
|
|||
|
return isSolidVLine(up) || (isJump(up) && (c !== "'"));
|
|||
|
} else if (isPoint(c)) {
|
|||
|
return isSolidVLine(up) || isSolidVLine(dn);
|
|||
|
}
|
|||
|
|
|||
|
return false;
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
/** Returns true if there is a solid middle (---) horizontal line
|
|||
|
passing through (x, y). Ignores underscores. */
|
|||
|
grid.isSolidHLineAt = function (x, y) {
|
|||
|
if (y === undefined) { y = x.x; x = x.x; }
|
|||
|
|
|||
|
var ltlt = grid(x - 2, y);
|
|||
|
var lt = grid(x - 1, y);
|
|||
|
var c = grid(x + 0, y);
|
|||
|
var rt = grid(x + 1, y);
|
|||
|
var rtrt = grid(x + 2, y);
|
|||
|
|
|||
|
if (isSolidHLine(c) || (isSolidHLine(lt) && isJump(c))) {
|
|||
|
// Looks like a horizontal line...does it continue? We need three in a row.
|
|||
|
if (isSolidHLine(lt)) {
|
|||
|
return isSolidHLine(rt) || isVertexOrRightDecoration(rt) ||
|
|||
|
isSolidHLine(ltlt) || isVertexOrLeftDecoration(ltlt);
|
|||
|
} else if (isVertexOrLeftDecoration(lt)) {
|
|||
|
return isSolidHLine(rt);
|
|||
|
} else {
|
|||
|
return isSolidHLine(rt) && (isSolidHLine(rtrt) || isVertexOrRightDecoration(rtrt));
|
|||
|
}
|
|||
|
|
|||
|
} else if (c === '<') {
|
|||
|
return isSolidHLine(rt) && isSolidHLine(rtrt)
|
|||
|
|
|||
|
} else if (c === '>') {
|
|||
|
return isSolidHLine(lt) && isSolidHLine(ltlt);
|
|||
|
|
|||
|
} else if (isVertex(c)) {
|
|||
|
return ((isSolidHLine(lt) && isSolidHLine(ltlt)) ||
|
|||
|
(isSolidHLine(rt) && isSolidHLine(rtrt)));
|
|||
|
}
|
|||
|
|
|||
|
return false;
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
/** Returns true if there is a solid backslash line passing through (x, y) */
|
|||
|
grid.isSolidBLineAt = function (x, y) {
|
|||
|
if (y === undefined) { y = x.x; x = x.x; }
|
|||
|
var c = grid(x, y);
|
|||
|
var lt = grid(x - 1, y - 1);
|
|||
|
var rt = grid(x + 1, y + 1);
|
|||
|
|
|||
|
if (c === '\\') {
|
|||
|
// Looks like a diagonal line...does it continue? We need two in a row.
|
|||
|
return (isSolidBLine(rt) || isBottomVertex(rt) || isPoint(rt) || (rt === 'v') ||
|
|||
|
isSolidBLine(lt) || isTopVertex(lt) || isPoint(lt) || (lt === '^') ||
|
|||
|
(grid(x, y - 1) === '/') || (grid(x, y + 1) === '/') || (rt === '_') || (lt === '_'));
|
|||
|
} else if (c === '.') {
|
|||
|
return (rt === '\\');
|
|||
|
} else if (c === "'") {
|
|||
|
return (lt === '\\');
|
|||
|
} else if (c === '^') {
|
|||
|
return rt === '\\';
|
|||
|
} else if (c === 'v') {
|
|||
|
return lt === '\\';
|
|||
|
} else if (isVertex(c) || isPoint(c) || (c === '|')) {
|
|||
|
return isSolidBLine(lt) || isSolidBLine(rt);
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
/** Returns true if there is a solid diagonal line passing through (x, y) */
|
|||
|
grid.isSolidDLineAt = function (x, y) {
|
|||
|
if (y === undefined) { y = x.x; x = x.x; }
|
|||
|
|
|||
|
var c = grid(x, y);
|
|||
|
var lt = grid(x - 1, y + 1);
|
|||
|
var rt = grid(x + 1, y - 1);
|
|||
|
|
|||
|
if (c === '/' && ((grid(x, y - 1) === '\\') || (grid(x, y + 1) === '\\'))) {
|
|||
|
// Special case of tiny hexagon corner
|
|||
|
return true;
|
|||
|
} else if (isSolidDLine(c)) {
|
|||
|
// Looks like a diagonal line...does it continue? We need two in a row.
|
|||
|
return (isSolidDLine(rt) || isTopVertex(rt) || isPoint(rt) || (rt === '^') || (rt === '_') ||
|
|||
|
isSolidDLine(lt) || isBottomVertex(lt) || isPoint(lt) || (lt === 'v') || (lt === '_'));
|
|||
|
} else if (c === '.') {
|
|||
|
return (lt === '/');
|
|||
|
} else if (c === "'") {
|
|||
|
return (rt === '/');
|
|||
|
} else if (c === '^') {
|
|||
|
return lt === '/';
|
|||
|
} else if (c === 'v') {
|
|||
|
return rt === '/';
|
|||
|
} else if (isVertex(c) || isPoint(c) || (c === '|')) {
|
|||
|
return isSolidDLine(lt) || isSolidDLine(rt);
|
|||
|
}
|
|||
|
return false;
|
|||
|
};
|
|||
|
|
|||
|
grid.toString = function () { return str; };
|
|||
|
|
|||
|
return Object.freeze(grid);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/** A 1D curve. If C is specified, the result is a bezier with
|
|||
|
that as the tangent control point */
|
|||
|
function Path(A, B, C, D, dashed) {
|
|||
|
if (! ((A instanceof Vec2) && (B instanceof Vec2))) {
|
|||
|
console.error('Path constructor requires at least two Vec2s');
|
|||
|
}
|
|||
|
this.A = A;
|
|||
|
this.B = B;
|
|||
|
if (C) {
|
|||
|
this.C = C;
|
|||
|
if (D) {
|
|||
|
this.D = D;
|
|||
|
} else {
|
|||
|
this.D = C;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
this.dashed = dashed || false;
|
|||
|
|
|||
|
Object.freeze(this);
|
|||
|
}
|
|||
|
|
|||
|
var _ = Path.prototype;
|
|||
|
_.isVertical = function () {
|
|||
|
return this.B.x === this.A.x;
|
|||
|
};
|
|||
|
|
|||
|
_.isHorizontal = function () {
|
|||
|
return this.B.y === this.A.y;
|
|||
|
};
|
|||
|
|
|||
|
/** Diagonal lines look like: / See also backDiagonal */
|
|||
|
_.isDiagonal = function () {
|
|||
|
var dx = this.B.x - this.A.x;
|
|||
|
var dy = this.B.y - this.A.y;
|
|||
|
return (abs(dy + dx) < EPSILON);
|
|||
|
};
|
|||
|
|
|||
|
_.isBackDiagonal = function () {
|
|||
|
var dx = this.B.x - this.A.x;
|
|||
|
var dy = this.B.y - this.A.y;
|
|||
|
return (abs(dy - dx) < EPSILON);
|
|||
|
};
|
|||
|
|
|||
|
_.isCurved = function () {
|
|||
|
return this.C !== undefined;
|
|||
|
};
|
|||
|
|
|||
|
/** Does this path have any end at (x, y) */
|
|||
|
_.endsAt = function (x, y) {
|
|||
|
if (y === undefined) { y = x.y; x = x.x; }
|
|||
|
return ((this.A.x === x) && (this.A.y === y)) ||
|
|||
|
((this.B.x === x) && (this.B.y === y));
|
|||
|
};
|
|||
|
|
|||
|
/** Does this path have an up end at (x, y) */
|
|||
|
_.upEndsAt = function (x, y) {
|
|||
|
if (y === undefined) { y = x.y; x = x.x; }
|
|||
|
return this.isVertical() && (this.A.x === x) && (min(this.A.y, this.B.y) === y);
|
|||
|
};
|
|||
|
|
|||
|
/** Does this path have an up end at (x, y) */
|
|||
|
_.diagonalUpEndsAt = function (x, y) {
|
|||
|
if (! this.isDiagonal()) { return false; }
|
|||
|
if (y === undefined) { y = x.y; x = x.x; }
|
|||
|
if (this.A.y < this.B.y) {
|
|||
|
return (this.A.x === x) && (this.A.y === y);
|
|||
|
} else {
|
|||
|
return (this.B.x === x) && (this.B.y === y);
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/** Does this path have a down end at (x, y) */
|
|||
|
_.diagonalDownEndsAt = function (x, y) {
|
|||
|
if (! this.isDiagonal()) { return false; }
|
|||
|
if (y === undefined) { y = x.y; x = x.x; }
|
|||
|
if (this.B.y < this.A.y) {
|
|||
|
return (this.A.x === x) && (this.A.y === y);
|
|||
|
} else {
|
|||
|
return (this.B.x === x) && (this.B.y === y);
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/** Does this path have an up end at (x, y) */
|
|||
|
_.backDiagonalUpEndsAt = function (x, y) {
|
|||
|
if (! this.isBackDiagonal()) { return false; }
|
|||
|
if (y === undefined) { y = x.y; x = x.x; }
|
|||
|
if (this.A.y < this.B.y) {
|
|||
|
return (this.A.x === x) && (this.A.y === y);
|
|||
|
} else {
|
|||
|
return (this.B.x === x) && (this.B.y === y);
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/** Does this path have a down end at (x, y) */
|
|||
|
_.backDiagonalDownEndsAt = function (x, y) {
|
|||
|
if (! this.isBackDiagonal()) { return false; }
|
|||
|
if (y === undefined) { y = x.y; x = x.x; }
|
|||
|
if (this.B.y < this.A.y) {
|
|||
|
return (this.A.x === x) && (this.A.y === y);
|
|||
|
} else {
|
|||
|
return (this.B.x === x) && (this.B.y === y);
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/** Does this path have a down end at (x, y) */
|
|||
|
_.downEndsAt = function (x, y) {
|
|||
|
if (y === undefined) { y = x.y; x = x.x; }
|
|||
|
return this.isVertical() && (this.A.x === x) && (max(this.A.y, this.B.y) === y);
|
|||
|
};
|
|||
|
|
|||
|
/** Does this path have a left end at (x, y) */
|
|||
|
_.leftEndsAt = function (x, y) {
|
|||
|
if (y === undefined) { y = x.y; x = x.x; }
|
|||
|
return this.isHorizontal() && (this.A.y === y) && (min(this.A.x, this.B.x) === x);
|
|||
|
};
|
|||
|
|
|||
|
/** Does this path have a right end at (x, y) */
|
|||
|
_.rightEndsAt = function (x, y) {
|
|||
|
if (y === undefined) { y = x.y; x = x.x; }
|
|||
|
return this.isHorizontal() && (this.A.y === y) && (max(this.A.x, this.B.x) === x);
|
|||
|
};
|
|||
|
|
|||
|
_.verticalPassesThrough = function (x, y) {
|
|||
|
if (y === undefined) { y = x.y; x = x.x; }
|
|||
|
return this.isVertical() &&
|
|||
|
(this.A.x === x) &&
|
|||
|
(min(this.A.y, this.B.y) <= y) &&
|
|||
|
(max(this.A.y, this.B.y) >= y);
|
|||
|
}
|
|||
|
|
|||
|
_.horizontalPassesThrough = function (x, y) {
|
|||
|
if (y === undefined) { y = x.y; x = x.x; }
|
|||
|
return this.isHorizontal() &&
|
|||
|
(this.A.y === y) &&
|
|||
|
(min(this.A.x, this.B.x) <= x) &&
|
|||
|
(max(this.A.x, this.B.x) >= x);
|
|||
|
}
|
|||
|
|
|||
|
/** Returns a string suitable for inclusion in an SVG tag */
|
|||
|
_.toSVG = function () {
|
|||
|
var svg = '<path d="M ' + this.A;
|
|||
|
|
|||
|
if (this.isCurved()) {
|
|||
|
svg += 'C ' + this.C + this.D + this.B;
|
|||
|
} else {
|
|||
|
svg += 'L ' + this.B;
|
|||
|
}
|
|||
|
svg += '" style="fill:none;"';
|
|||
|
if (this.dashed) {
|
|||
|
svg += ' stroke-dasharray="3,6"';
|
|||
|
}
|
|||
|
svg += '/>';
|
|||
|
return svg;
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
/** A group of 1D curves. This was designed so that all of the
|
|||
|
methods can later be implemented in O(1) time, but it
|
|||
|
currently uses O(n) implementations for source code
|
|||
|
simplicity. */
|
|||
|
function PathSet() {
|
|||
|
this._pathArray = [];
|
|||
|
}
|
|||
|
|
|||
|
var PS = PathSet.prototype;
|
|||
|
PS.insert = function (path) {
|
|||
|
this._pathArray.push(path);
|
|||
|
};
|
|||
|
|
|||
|
/** Returns a new method that returns true if method(x, y)
|
|||
|
returns true on any element of _pathAray */
|
|||
|
function makeFilterAny(method) {
|
|||
|
return function(x, y) {
|
|||
|
for (var i = 0; i < this._pathArray.length; ++i) {
|
|||
|
if (method.call(this._pathArray[i], x, y)) { return true; }
|
|||
|
}
|
|||
|
// Fall through: return undefined == false
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// True if an up line ends at these coordinates. Recall that the
|
|||
|
// variable _ is bound to the Path prototype still.
|
|||
|
PS.upEndsAt = makeFilterAny(_.upEndsAt);
|
|||
|
PS.diagonalUpEndsAt = makeFilterAny(_.diagonalUpEndsAt);
|
|||
|
PS.backDiagonalUpEndsAt = makeFilterAny(_.backDiagonalUpEndsAt);
|
|||
|
PS.diagonalDownEndsAt = makeFilterAny(_.diagonalDownEndsAt);
|
|||
|
PS.backDiagonalDownEndsAt = makeFilterAny(_.backDiagonalDownEndsAt);
|
|||
|
PS.downEndsAt = makeFilterAny(_.downEndsAt);
|
|||
|
PS.leftEndsAt = makeFilterAny(_.leftEndsAt);
|
|||
|
PS.rightEndsAt = makeFilterAny(_.rightEndsAt);
|
|||
|
PS.endsAt = makeFilterAny(_.endsAt);
|
|||
|
PS.verticalPassesThrough = makeFilterAny(_.verticalPassesThrough);
|
|||
|
PS.horizontalPassesThrough = makeFilterAny(_.horizontalPassesThrough);
|
|||
|
|
|||
|
/** Returns an SVG string */
|
|||
|
PS.toSVG = function () {
|
|||
|
var svg = '';
|
|||
|
for (var i = 0; i < this._pathArray.length; ++i) {
|
|||
|
svg += this._pathArray[i].toSVG() + '\n';
|
|||
|
}
|
|||
|
return svg;
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
function DecorationSet() {
|
|||
|
this._decorationArray = [];
|
|||
|
}
|
|||
|
|
|||
|
var DS = DecorationSet.prototype;
|
|||
|
|
|||
|
/** insert(x, y, type, <angle>)
|
|||
|
insert(vec, type, <angle>)
|
|||
|
|
|||
|
angle is the angle in degrees to rotate the result */
|
|||
|
DS.insert = function(x, y, type, angle) {
|
|||
|
if (type === undefined) { type = y; y = x.y; x = x.x; }
|
|||
|
|
|||
|
if (! isDecoration(type)) {
|
|||
|
console.error('Illegal decoration character: ' + type);
|
|||
|
}
|
|||
|
var d = {C: Vec2(x, y), type: type, angle:angle || 0};
|
|||
|
|
|||
|
// Put arrows at the front and points at the back so that
|
|||
|
// arrows always draw under points
|
|||
|
|
|||
|
if (isPoint(type)) {
|
|||
|
this._decorationArray.push(d);
|
|||
|
} else {
|
|||
|
this._decorationArray.unshift(d);
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
DS.toSVG = function () {
|
|||
|
var svg = '';
|
|||
|
for (var i = 0; i < this._decorationArray.length; ++i) {
|
|||
|
var decoration = this._decorationArray[i];
|
|||
|
var C = decoration.C;
|
|||
|
|
|||
|
if (isJump(decoration.type)) {
|
|||
|
// Slide jumps
|
|||
|
var dx = (decoration.type === ')') ? +0.75 : -0.75;
|
|||
|
var up = Vec2(C.x, C.y - 0.5);
|
|||
|
var dn = Vec2(C.x, C.y + 0.5);
|
|||
|
var cup = Vec2(C.x + dx, C.y - 0.5);
|
|||
|
var cdn = Vec2(C.x + dx, C.y + 0.5);
|
|||
|
|
|||
|
svg += '<path d="M ' + dn + ' C ' + cdn + cup + up + '" style="fill:none;"/>';
|
|||
|
|
|||
|
} else if (isPoint(decoration.type)) {
|
|||
|
|
|||
|
svg += '<circle cx="' + (C.x * SCALE) + '" cy="' + (C.y * SCALE * ASPECT) +
|
|||
|
'" r="' + (SCALE - STROKE_WIDTH) + '" class="' + ((decoration.type === '*') ? 'closed' : 'open') + 'dot"/>';
|
|||
|
} else if (isGray(decoration.type)) {
|
|||
|
|
|||
|
var shade = Math.round((3 - GRAY_CHARACTERS.indexOf(decoration.type)) * 63.75);
|
|||
|
svg += '<rect x="' + ((C.x - 0.5) * SCALE) + '" y="' + ((C.y - 0.5) * SCALE * ASPECT) + '" width="' + SCALE + '" height="' + (SCALE * ASPECT) + '" stroke=none fill="rgb(' + shade + ',' + shade + ',' + shade +')"/>';
|
|||
|
|
|||
|
} else if (isTri(decoration.type)) {
|
|||
|
// 30-60-90 triangle
|
|||
|
var index = TRI_CHARACTERS.indexOf(decoration.type);
|
|||
|
var xs = 0.5 - (index & 1);
|
|||
|
var ys = 0.5 - (index >> 1);
|
|||
|
xs *= sign(ys);
|
|||
|
var tip = Vec2(C.x + xs, C.y - ys);
|
|||
|
var up = Vec2(C.x + xs, C.y + ys);
|
|||
|
var dn = Vec2(C.x - xs, C.y + ys);
|
|||
|
svg += '<polygon points="' + tip + up + dn + '" style="stroke:none"/>\n';
|
|||
|
} else { // Arrow head
|
|||
|
var tip = Vec2(C.x + 1, C.y);
|
|||
|
var up = Vec2(C.x - 0.5, C.y - 0.35);
|
|||
|
var dn = Vec2(C.x - 0.5, C.y + 0.35);
|
|||
|
svg += '<polygon points="' + tip + up + dn +
|
|||
|
'" style="stroke:none" transform="rotate(' + decoration.angle + ',' + C + ')"/>\n';
|
|||
|
}
|
|||
|
}
|
|||
|
return svg;
|
|||
|
};
|
|||
|
|
|||
|
////////////////////////////////////////////////////////////////////////////
|
|||
|
|
|||
|
function findPaths(grid, pathSet) {
|
|||
|
// Does the line from A to B contain at least one c?
|
|||
|
function lineContains(A, B, c) {
|
|||
|
var dx = sign(B.x - A.x);
|
|||
|
var dy = sign(B.y - A.y);
|
|||
|
var x, y;
|
|||
|
|
|||
|
for (x = A.x, y = A.y; (x !== B.x) || (y !== B.y); x += dx, y += dy) {
|
|||
|
if (grid(x, y) === c) { return true; }
|
|||
|
}
|
|||
|
|
|||
|
// Last point
|
|||
|
return (grid(x, y) === c);
|
|||
|
}
|
|||
|
|
|||
|
// Find all solid vertical lines. Iterate horizontally
|
|||
|
// so that we never hit the same line twice
|
|||
|
for (var x = 0; x < grid.width; ++x) {
|
|||
|
for (var y = 0; y < grid.height; ++y) {
|
|||
|
if (grid.isSolidVLineAt(x, y)) {
|
|||
|
// This character begins a vertical line...now, find the end
|
|||
|
var A = Vec2(x, y);
|
|||
|
do { grid.setUsed(x, y); ++y; } while (grid.isSolidVLineAt(x, y));
|
|||
|
var B = Vec2(x, y - 1);
|
|||
|
|
|||
|
var up = grid(A);
|
|||
|
var upup = grid(A.x, A.y - 1);
|
|||
|
|
|||
|
if (! isVertex(up) && ((upup === '-') || (upup === '_') || (grid(A.x - 1, A.y - 1) === '_') ||
|
|||
|
(grid(A.x + 1, A.y - 1) === '_') ||
|
|||
|
isBottomVertex(upup)) || isJump(upup)) {
|
|||
|
// Stretch up to almost reach the line above (if there is a decoration,
|
|||
|
// it will finish the gap)
|
|||
|
A.y -= 0.5;
|
|||
|
}
|
|||
|
|
|||
|
var dn = grid(B);
|
|||
|
var dndn = grid(B.x, B.y + 1);
|
|||
|
if (! isVertex(dn) && ((dndn === '-') || isTopVertex(dndn)) || isJump(dndn) ||
|
|||
|
(grid(B.x - 1, B.y) === '_') || (grid(B.x + 1, B.y) === '_')) {
|
|||
|
// Stretch down to almost reach the line below
|
|||
|
B.y += 0.5;
|
|||
|
}
|
|||
|
|
|||
|
// Don't insert degenerate lines
|
|||
|
if ((A.x !== B.x) || (A.y !== B.y)) {
|
|||
|
pathSet.insert(new Path(A, B));
|
|||
|
}
|
|||
|
|
|||
|
// Continue the search from the end value y+1
|
|||
|
}
|
|||
|
|
|||
|
// Some very special patterns for the short lines needed on
|
|||
|
// circuit diagrams. Only invoke these if not also on a curve
|
|||
|
// _ _
|
|||
|
// -' '-
|
|||
|
else if ((grid(x, y) === "'") &&
|
|||
|
(((grid(x - 1, y) === '-') && (grid(x + 1, y - 1) === '_') &&
|
|||
|
! isSolidVLineOrJumpOrPoint(grid(x - 1, y - 1))) ||
|
|||
|
((grid(x - 1, y - 1) === '_') && (grid(x + 1, y) === '-') &&
|
|||
|
! isSolidVLineOrJumpOrPoint(grid(x + 1, y - 1))))) {
|
|||
|
pathSet.insert(new Path(Vec2(x, y - 0.5), Vec2(x, y)));
|
|||
|
}
|
|||
|
|
|||
|
// _.- -._
|
|||
|
else if ((grid(x, y) === '.') &&
|
|||
|
(((grid(x - 1, y) === '_') && (grid(x + 1, y) === '-') &&
|
|||
|
! isSolidVLineOrJumpOrPoint(grid(x + 1, y + 1))) ||
|
|||
|
((grid(x - 1, y) === '-') && (grid(x + 1, y) === '_') &&
|
|||
|
! isSolidVLineOrJumpOrPoint(grid(x - 1, y + 1))))) {
|
|||
|
pathSet.insert(new Path(Vec2(x, y), Vec2(x, y + 0.5)));
|
|||
|
}
|
|||
|
|
|||
|
} // y
|
|||
|
} // x
|
|||
|
|
|||
|
// Find all solid horizontal lines
|
|||
|
for (var y = 0; y < grid.height; ++y) {
|
|||
|
for (var x = 0; x < grid.width; ++x) {
|
|||
|
if (grid.isSolidHLineAt(x, y)) {
|
|||
|
// Begins a line...find the end
|
|||
|
var A = Vec2(x, y);
|
|||
|
do { grid.setUsed(x, y); ++x; } while (grid.isSolidHLineAt(x, y));
|
|||
|
var B = Vec2(x - 1, y);
|
|||
|
|
|||
|
// Detect curves and shorten the edge
|
|||
|
if ( ! isVertex(grid(A.x - 1, A.y)) &&
|
|||
|
((isTopVertex(grid(A)) && isSolidVLineOrJumpOrPoint(grid(A.x - 1, A.y + 1))) ||
|
|||
|
(isBottomVertex(grid(A)) && isSolidVLineOrJumpOrPoint(grid(A.x - 1, A.y - 1))))) {
|
|||
|
++A.x;
|
|||
|
}
|
|||
|
|
|||
|
if ( ! isVertex(grid(B.x + 1, B.y)) &&
|
|||
|
((isTopVertex(grid(B)) && isSolidVLineOrJumpOrPoint(grid(B.x + 1, B.y + 1))) ||
|
|||
|
(isBottomVertex(grid(B)) && isSolidVLineOrJumpOrPoint(grid(B.x + 1, B.y - 1))))) {
|
|||
|
--B.x;
|
|||
|
}
|
|||
|
|
|||
|
// Don't insert degenerate lines
|
|||
|
if ((A.x !== B.x) || (A.y !== B.y)) {
|
|||
|
pathSet.insert(new Path(A, B));
|
|||
|
}
|
|||
|
// Continue the search from the end x+1
|
|||
|
}
|
|||
|
}
|
|||
|
} // y
|
|||
|
|
|||
|
// Find all solid left-to-right downward diagonal lines (BACK DIAGONAL)
|
|||
|
for (var i = -grid.height; i < grid.width; ++i) {
|
|||
|
for (var x = i, y = 0; y < grid.height; ++y, ++x) {
|
|||
|
if (grid.isSolidBLineAt(x, y)) {
|
|||
|
// Begins a line...find the end
|
|||
|
var A = Vec2(x, y);
|
|||
|
do { ++x; ++y; } while (grid.isSolidBLineAt(x, y));
|
|||
|
var B = Vec2(x - 1, y - 1);
|
|||
|
|
|||
|
// Ensure that the entire line wasn't just vertices
|
|||
|
if (lineContains(A, B, '\\')) {
|
|||
|
for (var j = A.x; j <= B.x; ++j) {
|
|||
|
grid.setUsed(j, A.y + (j - A.x));
|
|||
|
}
|
|||
|
|
|||
|
var top = grid(A);
|
|||
|
var up = grid(A.x, A.y - 1);
|
|||
|
var uplt = grid(A.x - 1, A.y - 1);
|
|||
|
if ((up === '/') || (uplt === '_') || (up === '_') ||
|
|||
|
(! isVertex(top) &&
|
|||
|
(isSolidHLine(uplt) || isSolidVLine(uplt)))) {
|
|||
|
// Continue half a cell more to connect for:
|
|||
|
// ___ ___
|
|||
|
// \ \ / ---- |
|
|||
|
// \ \ \ ^ |^
|
|||
|
A.x -= 0.5; A.y -= 0.5;
|
|||
|
} else if (isPoint(uplt)) {
|
|||
|
// Continue 1/4 cell more to connect for:
|
|||
|
//
|
|||
|
// o
|
|||
|
// ^
|
|||
|
// \
|
|||
|
A.x -= 0.25; A.y -= 0.25;
|
|||
|
}
|
|||
|
|
|||
|
var bottom = grid(B);
|
|||
|
var dnrt = grid(B.x + 1, B.y + 1);
|
|||
|
if ((grid(B.x, B.y + 1) === '/') || (grid(B.x + 1, B.y) === '_') ||
|
|||
|
(grid(B.x - 1, B.y) === '_') ||
|
|||
|
(! isVertex(grid(B)) &&
|
|||
|
(isSolidHLine(dnrt) || isSolidVLine(dnrt)))) {
|
|||
|
// Continue half a cell more to connect for:
|
|||
|
// \ \ |
|
|||
|
// \ \ \ v v|
|
|||
|
// \__ __\ / ---- |
|
|||
|
|
|||
|
B.x += 0.5; B.y += 0.5;
|
|||
|
} else if (isPoint(dnrt)) {
|
|||
|
// Continue 1/4 cell more to connect for:
|
|||
|
//
|
|||
|
// \
|
|||
|
// v
|
|||
|
// o
|
|||
|
|
|||
|
B.x += 0.25; B.y += 0.25;
|
|||
|
}
|
|||
|
|
|||
|
pathSet.insert(new Path(A, B));
|
|||
|
// Continue the search from the end x+1,y+1
|
|||
|
} // lineContains
|
|||
|
}
|
|||
|
}
|
|||
|
} // i
|
|||
|
|
|||
|
|
|||
|
// Find all solid left-to-right upward diagonal lines (DIAGONAL)
|
|||
|
for (var i = -grid.height; i < grid.width; ++i) {
|
|||
|
for (var x = i, y = grid.height - 1; y >= 0; --y, ++x) {
|
|||
|
if (grid.isSolidDLineAt(x, y)) {
|
|||
|
// Begins a line...find the end
|
|||
|
var A = Vec2(x, y);
|
|||
|
do { ++x; --y; } while (grid.isSolidDLineAt(x, y));
|
|||
|
var B = Vec2(x - 1, y + 1);
|
|||
|
|
|||
|
if (lineContains(A, B, '/')) {
|
|||
|
// This is definitely a line. Commit the characters on it
|
|||
|
for (var j = A.x; j <= B.x; ++j) {
|
|||
|
grid.setUsed(j, A.y - (j - A.x));
|
|||
|
}
|
|||
|
|
|||
|
var up = grid(B.x, B.y - 1);
|
|||
|
var uprt = grid(B.x + 1, B.y - 1);
|
|||
|
var bottom = grid(B);
|
|||
|
if ((up === '\\') || (up === '_') || (uprt === '_') ||
|
|||
|
(! isVertex(grid(B)) &&
|
|||
|
(isSolidHLine(uprt) || isSolidVLine(uprt)))) {
|
|||
|
|
|||
|
// Continue half a cell more to connect at:
|
|||
|
// __ __ --- |
|
|||
|
// / / ^ ^|
|
|||
|
// / / / / |
|
|||
|
|
|||
|
B.x += 0.5; B.y -= 0.5;
|
|||
|
} else if (isPoint(uprt)) {
|
|||
|
|
|||
|
// Continue 1/4 cell more to connect at:
|
|||
|
//
|
|||
|
// o
|
|||
|
// ^
|
|||
|
// /
|
|||
|
|
|||
|
B.x += 0.25; B.y -= 0.25;
|
|||
|
}
|
|||
|
|
|||
|
var dnlt = grid(A.x - 1, A.y + 1);
|
|||
|
var top = grid(A);
|
|||
|
if ((grid(A.x, A.y + 1) === '\\') || (grid(A.x - 1, A.y) === '_') || (grid(A.x + 1, A.y) === '_') ||
|
|||
|
(! isVertex(grid(A)) &&
|
|||
|
(isSolidHLine(dnlt) || isSolidVLine(dnlt)))) {
|
|||
|
|
|||
|
// Continue half a cell more to connect at:
|
|||
|
// / \ |
|
|||
|
// / / v v|
|
|||
|
// __/ /__ ---- |
|
|||
|
|
|||
|
A.x -= 0.5; A.y += 0.5;
|
|||
|
} else if (isPoint(dnlt)) {
|
|||
|
|
|||
|
// Continue 1/4 cell more to connect at:
|
|||
|
//
|
|||
|
// /
|
|||
|
// v
|
|||
|
// o
|
|||
|
|
|||
|
A.x -= 0.25; A.y += 0.25;
|
|||
|
}
|
|||
|
pathSet.insert(new Path(A, B));
|
|||
|
|
|||
|
// Continue the search from the end x+1,y-1
|
|||
|
} // lineContains
|
|||
|
}
|
|||
|
}
|
|||
|
} // y
|
|||
|
|
|||
|
|
|||
|
// Now look for curved corners. The syntax constraints require
|
|||
|
// that these can always be identified by looking at three
|
|||
|
// horizontally-adjacent characters.
|
|||
|
for (var y = 0; y < grid.height; ++y) {
|
|||
|
for (var x = 0; x < grid.width; ++x) {
|
|||
|
var c = grid(x, y);
|
|||
|
|
|||
|
// Note that because of undirected vertices, the
|
|||
|
// following cases are not exclusive
|
|||
|
if (isTopVertex(c)) {
|
|||
|
// -.
|
|||
|
// |
|
|||
|
if (isSolidHLine(grid(x - 1, y)) && isSolidVLine(grid(x + 1, y + 1))) {
|
|||
|
grid.setUsed(x - 1, y); grid.setUsed(x, y); grid.setUsed(x + 1, y + 1);
|
|||
|
pathSet.insert(new Path(Vec2(x - 1, y), Vec2(x + 1, y + 1),
|
|||
|
Vec2(x + 1.1, y), Vec2(x + 1, y + 1)));
|
|||
|
}
|
|||
|
|
|||
|
// .-
|
|||
|
// |
|
|||
|
if (isSolidHLine(grid(x + 1, y)) && isSolidVLine(grid(x - 1, y + 1))) {
|
|||
|
grid.setUsed(x - 1, y + 1); grid.setUsed(x, y); grid.setUsed(x + 1, y);
|
|||
|
pathSet.insert(new Path(Vec2(x + 1, y), Vec2(x - 1, y + 1),
|
|||
|
Vec2(x - 1.1, y), Vec2(x - 1, y + 1)));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// Special case patterns:
|
|||
|
// . . . .
|
|||
|
// ( o ) o
|
|||
|
// ' . ' '
|
|||
|
if (((c === ')') || isPoint(c)) && (grid(x - 1, y - 1) === '.') && (grid(x - 1, y + 1) === "\'")) {
|
|||
|
grid.setUsed(x, y); grid.setUsed(x - 1, y - 1); grid.setUsed(x - 1, y + 1);
|
|||
|
pathSet.insert(new Path(Vec2(x - 2, y - 1), Vec2(x - 2, y + 1),
|
|||
|
Vec2(x + 0.6, y - 1), Vec2(x + 0.6, y + 1)));
|
|||
|
}
|
|||
|
|
|||
|
if (((c === '(') || isPoint(c)) && (grid(x + 1, y - 1) === '.') && (grid(x + 1, y + 1) === "\'")) {
|
|||
|
grid.setUsed(x, y); grid.setUsed(x + 1, y - 1); grid.setUsed(x + 1, y + 1);
|
|||
|
pathSet.insert(new Path(Vec2(x + 2, y - 1), Vec2(x + 2, y + 1),
|
|||
|
Vec2(x - 0.6, y - 1), Vec2(x - 0.6, y + 1)));
|
|||
|
}
|
|||
|
|
|||
|
if (isBottomVertex(c)) {
|
|||
|
// |
|
|||
|
// -'
|
|||
|
if (isSolidHLine(grid(x - 1, y)) && isSolidVLine(grid(x + 1, y - 1))) {
|
|||
|
grid.setUsed(x - 1, y); grid.setUsed(x, y); grid.setUsed(x + 1, y - 1);
|
|||
|
pathSet.insert(new Path(Vec2(x - 1, y), Vec2(x + 1, y - 1),
|
|||
|
Vec2(x + 1.1, y), Vec2(x + 1, y - 1)));
|
|||
|
}
|
|||
|
|
|||
|
// |
|
|||
|
// '-
|
|||
|
if (isSolidHLine(grid(x + 1, y)) && isSolidVLine(grid(x - 1, y - 1))) {
|
|||
|
grid.setUsed(x - 1, y - 1); grid.setUsed(x, y); grid.setUsed(x + 1, y);
|
|||
|
pathSet.insert(new Path(Vec2(x + 1, y), Vec2(x - 1, y - 1),
|
|||
|
Vec2(x - 1.1, y), Vec2(x - 1, y - 1)));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
} // for x
|
|||
|
} // for y
|
|||
|
|
|||
|
// Find low horizontal lines marked with underscores. These
|
|||
|
// are so simple compared to the other cases that we process
|
|||
|
// them directly here without a helper function. Process these
|
|||
|
// from top to bottom and left to right so that we can read
|
|||
|
// them in a single sweep.
|
|||
|
//
|
|||
|
// Exclude the special case of double underscores going right
|
|||
|
// into an ASCII character, which could be a source code
|
|||
|
// identifier such as __FILE__ embedded in the diagram.
|
|||
|
for (var y = 0; y < grid.height; ++y) {
|
|||
|
for (var x = 0; x < grid.width - 2; ++x) {
|
|||
|
var lt = grid(x - 1, y);
|
|||
|
|
|||
|
if ((grid(x, y) === '_') && (grid(x + 1, y) === '_') &&
|
|||
|
(! isASCIILetter(grid(x + 2, y)) || (lt === '_')) &&
|
|||
|
(! isASCIILetter(lt) || (grid(x + 2, y) === '_'))) {
|
|||
|
|
|||
|
var ltlt = grid(x - 2, y);
|
|||
|
var A = Vec2(x - 0.5, y + 0.5);
|
|||
|
|
|||
|
if ((lt === '|') || (grid(x - 1, y + 1) === '|') ||
|
|||
|
(lt === '.') || (grid(x - 1, y + 1) === "'")) {
|
|||
|
// Extend to meet adjacent vertical
|
|||
|
A.x -= 0.5;
|
|||
|
|
|||
|
// Very special case of overrunning into the side of a curve,
|
|||
|
// needed for logic gate diagrams
|
|||
|
if ((lt === '.') &&
|
|||
|
((ltlt === '-') ||
|
|||
|
(ltlt === '.')) &&
|
|||
|
(grid(x - 2, y + 1) === '(')) {
|
|||
|
A.x -= 0.5;
|
|||
|
}
|
|||
|
} else if (lt === '/') {
|
|||
|
A.x -= 1.0;
|
|||
|
}
|
|||
|
|
|||
|
// Detect overrun of a tight double curve
|
|||
|
if ((lt === '(') && (ltlt === '(') &&
|
|||
|
(grid(x, y + 1) === "'") && (grid(x, y - 1) === '.')) {
|
|||
|
A.x += 0.5;
|
|||
|
}
|
|||
|
lt = ltlt = undefined;
|
|||
|
|
|||
|
do { grid.setUsed(x, y); ++x; } while (grid(x, y) === '_');
|
|||
|
|
|||
|
var B = Vec2(x - 0.5, y + 0.5);
|
|||
|
var c = grid(x, y);
|
|||
|
var rt = grid(x + 1, y);
|
|||
|
var dn = grid(x, y + 1);
|
|||
|
|
|||
|
if ((c === '|') || (dn === '|') || (c === '.') || (dn === "'")) {
|
|||
|
// Extend to meet adjacent vertical
|
|||
|
B.x += 0.5;
|
|||
|
|
|||
|
// Very special case of overrunning into the side of a curve,
|
|||
|
// needed for logic gate diagrams
|
|||
|
if ((c === '.') &&
|
|||
|
((rt === '-') || (rt === '.')) &&
|
|||
|
(grid(x + 1, y + 1) === ')')) {
|
|||
|
B.x += 0.5;
|
|||
|
}
|
|||
|
} else if ((c === '\\')) {
|
|||
|
B.x += 1.0;
|
|||
|
}
|
|||
|
|
|||
|
// Detect overrun of a tight double curve
|
|||
|
if ((c === ')') && (rt === ')') && (grid(x - 1, y + 1) === "'") && (grid(x - 1, y - 1) === '.')) {
|
|||
|
B.x += -0.5;
|
|||
|
}
|
|||
|
|
|||
|
pathSet.insert(new Path(A, B));
|
|||
|
}
|
|||
|
} // for x
|
|||
|
} // for y
|
|||
|
} // findPaths
|
|||
|
|
|||
|
|
|||
|
function findDecorations(grid, pathSet, decorationSet) {
|
|||
|
function isEmptyOrVertex(c) { return (c === ' ') || /[^a-zA-Z0-9]|[ov]/.test(c); }
|
|||
|
function isLetter(c) { var x = c.toUpperCase().charCodeAt(0); return (x > 64) && (x < 91); }
|
|||
|
|
|||
|
/** Is the point in the center of these values on a line? Allow points that are vertically
|
|||
|
adjacent but not horizontally--they wouldn't fit anyway, and might be text. */
|
|||
|
function onLine(up, dn, lt, rt) {
|
|||
|
return ((isEmptyOrVertex(dn) || isPoint(dn)) &&
|
|||
|
(isEmptyOrVertex(up) || isPoint(up)) &&
|
|||
|
isEmptyOrVertex(rt) &&
|
|||
|
isEmptyOrVertex(lt));
|
|||
|
}
|
|||
|
|
|||
|
for (var x = 0; x < grid.width; ++x) {
|
|||
|
for (var j = 0; j < grid.height; ++j) {
|
|||
|
var c = grid(x, j);
|
|||
|
var y = j;
|
|||
|
|
|||
|
if (isJump(c)) {
|
|||
|
|
|||
|
// Ensure that this is really a jump and not a stray character
|
|||
|
if (pathSet.downEndsAt(x, y - 0.5) &&
|
|||
|
pathSet.upEndsAt(x, y + 0.5)) {
|
|||
|
decorationSet.insert(x, y, c);
|
|||
|
grid.setUsed(x, y);
|
|||
|
}
|
|||
|
|
|||
|
} else if (isPoint(c)) {
|
|||
|
var up = grid(x, y - 1);
|
|||
|
var dn = grid(x, y + 1);
|
|||
|
var lt = grid(x - 1, y);
|
|||
|
var rt = grid(x + 1, y);
|
|||
|
var llt = grid(x - 2, y);
|
|||
|
var rrt = grid(x + 2, y);
|
|||
|
|
|||
|
if (pathSet.rightEndsAt(x - 1, y) || // Must be at the end of a line...
|
|||
|
pathSet.leftEndsAt(x + 1, y) || // or completely isolated NSEW
|
|||
|
pathSet.downEndsAt(x, y - 1) ||
|
|||
|
pathSet.upEndsAt(x, y + 1) ||
|
|||
|
|
|||
|
pathSet.upEndsAt(x, y) || // For points on vertical lines
|
|||
|
pathSet.downEndsAt(x, y) || // that are surrounded by other characters
|
|||
|
|
|||
|
onLine(up, dn, lt, rt)) {
|
|||
|
|
|||
|
decorationSet.insert(x, y, c);
|
|||
|
grid.setUsed(x, y);
|
|||
|
}
|
|||
|
} else if (isGray(c)) {
|
|||
|
decorationSet.insert(x, y, c);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (isTri(c)) {
|
|||
|
decorationSet.insert(x, y, c);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else { // Arrow heads
|
|||
|
|
|||
|
// If we find one, ensure that it is really an
|
|||
|
// arrow head and not a stray character by looking
|
|||
|
// for a connecting line.
|
|||
|
var dx = 0;
|
|||
|
if ((c === '>') && (pathSet.rightEndsAt(x, y) ||
|
|||
|
pathSet.horizontalPassesThrough(x, y))) {
|
|||
|
if (isPoint(grid(x + 1, y))) {
|
|||
|
// Back up if connecting to a point so as to not
|
|||
|
// overlap it
|
|||
|
dx = -0.5;
|
|||
|
}
|
|||
|
decorationSet.insert(x + dx, y, '>', 0);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if ((c === '<') && (pathSet.leftEndsAt(x, y) ||
|
|||
|
pathSet.horizontalPassesThrough(x, y))) {
|
|||
|
if (isPoint(grid(x - 1, y))) {
|
|||
|
// Back up if connecting to a point so as to not
|
|||
|
// overlap it
|
|||
|
dx = 0.5;
|
|||
|
}
|
|||
|
decorationSet.insert(x + dx, y, '>', 180);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (c === '^') {
|
|||
|
// Because of the aspect ratio, we need to look
|
|||
|
// in two slots for the end of the previous line
|
|||
|
if (pathSet.upEndsAt(x, y - 0.5)) {
|
|||
|
decorationSet.insert(x, y - 0.5, '>', 270);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.upEndsAt(x, y)) {
|
|||
|
decorationSet.insert(x, y, '>', 270);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.diagonalUpEndsAt(x + 0.5, y - 0.5)) {
|
|||
|
decorationSet.insert(x + 0.5, y - 0.5, '>', 270 + DIAGONAL_ANGLE);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.diagonalUpEndsAt(x + 0.25, y - 0.25)) {
|
|||
|
decorationSet.insert(x + 0.25, y - 0.25, '>', 270 + DIAGONAL_ANGLE);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.diagonalUpEndsAt(x, y)) {
|
|||
|
decorationSet.insert(x, y, '>', 270 + DIAGONAL_ANGLE);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.backDiagonalUpEndsAt(x, y)) {
|
|||
|
decorationSet.insert(x, y, c, 270 - DIAGONAL_ANGLE);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.backDiagonalUpEndsAt(x - 0.5, y - 0.5)) {
|
|||
|
decorationSet.insert(x - 0.5, y - 0.5, c, 270 - DIAGONAL_ANGLE);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.backDiagonalUpEndsAt(x - 0.25, y - 0.25)) {
|
|||
|
decorationSet.insert(x - 0.25, y - 0.25, c, 270 - DIAGONAL_ANGLE);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.verticalPassesThrough(x, y)) {
|
|||
|
// Only try this if all others failed
|
|||
|
decorationSet.insert(x, y - 0.5, '>', 270);
|
|||
|
grid.setUsed(x, y);
|
|||
|
}
|
|||
|
} else if (c === 'v') {
|
|||
|
if (pathSet.downEndsAt(x, y + 0.5)) {
|
|||
|
decorationSet.insert(x, y + 0.5, '>', 90);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.downEndsAt(x, y)) {
|
|||
|
decorationSet.insert(x, y, '>', 90);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.diagonalDownEndsAt(x, y)) {
|
|||
|
decorationSet.insert(x, y, '>', 90 + DIAGONAL_ANGLE);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.diagonalDownEndsAt(x - 0.5, y + 0.5)) {
|
|||
|
decorationSet.insert(x - 0.5, y + 0.5, '>', 90 + DIAGONAL_ANGLE);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.diagonalDownEndsAt(x - 0.25, y + 0.25)) {
|
|||
|
decorationSet.insert(x - 0.25, y + 0.25, '>', 90 + DIAGONAL_ANGLE);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.backDiagonalDownEndsAt(x, y)) {
|
|||
|
decorationSet.insert(x, y, '>', 90 - DIAGONAL_ANGLE);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.backDiagonalDownEndsAt(x + 0.5, y + 0.5)) {
|
|||
|
decorationSet.insert(x + 0.5, y + 0.5, '>', 90 - DIAGONAL_ANGLE);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.backDiagonalDownEndsAt(x + 0.25, y + 0.25)) {
|
|||
|
decorationSet.insert(x + 0.25, y + 0.25, '>', 90 - DIAGONAL_ANGLE);
|
|||
|
grid.setUsed(x, y);
|
|||
|
} else if (pathSet.verticalPassesThrough(x, y)) {
|
|||
|
// Only try this if all others failed
|
|||
|
decorationSet.insert(x, y + 0.5, '>', 90);
|
|||
|
grid.setUsed(x, y);
|
|||
|
}
|
|||
|
} // arrow heads
|
|||
|
} // decoration type
|
|||
|
} // y
|
|||
|
} // x
|
|||
|
} // findArrowHeads
|
|||
|
|
|||
|
var grid = makeGrid(diagramString);
|
|||
|
|
|||
|
var pathSet = new PathSet();
|
|||
|
var decorationSet = new DecorationSet();
|
|||
|
|
|||
|
findPaths(grid, pathSet);
|
|||
|
findDecorations(grid, pathSet, decorationSet);
|
|||
|
|
|||
|
var svg = '<svg class="diagram" xmlns="http://www.w3.org/2000/svg" version="1.1" height="' +
|
|||
|
((grid.height + 1) * SCALE * ASPECT) + '" width="' + ((grid.width + 1) * SCALE) + '"';
|
|||
|
|
|||
|
if (alignmentHint === 'floatleft') {
|
|||
|
svg += ' style="float:left;margin:15px 30px 15px 0;"';
|
|||
|
} else if (alignmentHint === 'floatright') {
|
|||
|
svg += ' style="float:right;margin:15px 0 15px 30px;"';
|
|||
|
} else if (alignmentHint === 'center') {
|
|||
|
svg += ' style="margin:0 auto 0 auto;"';
|
|||
|
}
|
|||
|
|
|||
|
svg += '><g transform="translate(' + Vec2(1, 1) + ')">\n';
|
|||
|
|
|||
|
if (DEBUG_SHOW_GRID) {
|
|||
|
svg += '<g style="opacity:0.1">\n';
|
|||
|
for (var x = 0; x < grid.width; ++x) {
|
|||
|
for (var y = 0; y < grid.height; ++y) {
|
|||
|
svg += '<rect x="' + ((x - 0.5) * SCALE + 1) + '" + y="' + ((y - 0.5) * SCALE * ASPECT + 2) + '" width="' + (SCALE - 2) + '" height="' + (SCALE * ASPECT - 2) + '" style="fill:';
|
|||
|
if (grid.isUsed(x, y)) {
|
|||
|
svg += 'red;';
|
|||
|
} else if (grid(x, y) === ' ') {
|
|||
|
svg += 'gray;opacity:0.05';
|
|||
|
} else {
|
|||
|
svg += 'blue;';
|
|||
|
}
|
|||
|
svg += '"/>\n';
|
|||
|
}
|
|||
|
}
|
|||
|
svg += '</g>\n';
|
|||
|
}
|
|||
|
|
|||
|
svg += pathSet.toSVG();
|
|||
|
svg += decorationSet.toSVG();
|
|||
|
|
|||
|
// Convert any remaining characters
|
|||
|
if (! DEBUG_HIDE_PASSTHROUGH) {
|
|||
|
svg += '<g transform="translate(0,0)">';
|
|||
|
for (var y = 0; y < grid.height; ++y) {
|
|||
|
for (var x = 0; x < grid.width; ++x) {
|
|||
|
var c = grid(x, y);
|
|||
|
if (/[\u2B22\u2B21]/.test(c)) {
|
|||
|
// Enlarge hexagons so that they fill a grid
|
|||
|
svg += '<text text-anchor="middle" x="' + (x * SCALE) + '" y="' + (4 + y * SCALE * ASPECT) + '" style="font-size:20.5px">' + escapeHTMLEntities(c) + '</text>';
|
|||
|
} else if ((c !== ' ') && ! grid.isUsed(x, y)) {
|
|||
|
svg += '<text text-anchor="middle" x="' + (x * SCALE) + '" y="' + (4 + y * SCALE * ASPECT) + '">' + escapeHTMLEntities(c) + '</text>';
|
|||
|
} // if
|
|||
|
} // y
|
|||
|
} // x
|
|||
|
svg += '</g>';
|
|||
|
}
|
|||
|
|
|||
|
if (DEBUG_SHOW_SOURCE) {
|
|||
|
// Offset the characters a little for easier viewing
|
|||
|
svg += '<g transform="translate(2,2)">\n';
|
|||
|
for (var x = 0; x < grid.width; ++x) {
|
|||
|
for (var y = 0; y < grid.height; ++y) {
|
|||
|
var c = grid(x, y);
|
|||
|
if (c !== ' ') {
|
|||
|
svg += '<text text-anchor="middle" x="' + (x * SCALE) + '" y="' + (4 + y * SCALE * ASPECT) + '" style="fill:#F00;font-family:Menlo,monospace;font-size:12px;text-align:center">' + escapeHTMLEntities(c) + '</text>';
|
|||
|
} // if
|
|||
|
} // y
|
|||
|
} // x
|
|||
|
svg += '</g>';
|
|||
|
} // if
|
|||
|
|
|||
|
svg += '</g></svg>';
|
|||
|
|
|||
|
svg = svg.rp(new RegExp(HIDE_O, 'g'), 'o');
|
|||
|
|
|||
|
|
|||
|
return svg;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/* xcode.min.js modified */
|
|||
|
var HIGHLIGHT_STYLESHEET =
|
|||
|
"<style>.hljs{display:block;overflow-x:auto;padding:0.5em;background:#fff;color:#000;-webkit-text-size-adjust:none}"+
|
|||
|
".hljs-comment{color:#006a00}" +
|
|||
|
".hljs-keyword{color:#02E}" +
|
|||
|
".hljs-literal,.nginx .hljs-title{color:#aa0d91}" +
|
|||
|
".method,.hljs-list .hljs-title,.hljs-tag .hljs-title,.setting .hljs-value,.hljs-winutils,.tex .hljs-command,.http .hljs-title,.hljs-request,.hljs-status,.hljs-name{color:#008}" +
|
|||
|
".hljs-envvar,.tex .hljs-special{color:#660}" +
|
|||
|
".hljs-string{color:#c41a16}" +
|
|||
|
".hljs-tag .hljs-value,.hljs-cdata,.hljs-filter .hljs-argument,.hljs-attr_selector,.apache .hljs-cbracket,.hljs-date,.hljs-regexp{color:#080}" +
|
|||
|
".hljs-sub .hljs-identifier,.hljs-pi,.hljs-tag,.hljs-tag .hljs-keyword,.hljs-decorator,.ini .hljs-title,.hljs-shebang,.hljs-prompt,.hljs-hexcolor,.hljs-rule .hljs-value,.hljs-symbol,.hljs-symbol .hljs-string,.hljs-number,.css .hljs-function,.hljs-function .hljs-title,.coffeescript .hljs-attribute{color:#A0C}" +
|
|||
|
".hljs-function .hljs-title{font-weight:bold;color:#000}" +
|
|||
|
".hljs-class .hljs-title,.smalltalk .hljs-class,.hljs-type,.hljs-typename,.hljs-tag .hljs-attribute,.hljs-doctype,.hljs-class .hljs-id,.hljs-built_in,.setting,.hljs-params,.clojure .hljs-attribute{color:#5c2699}" +
|
|||
|
".hljs-variable{color:#3f6e74}" +
|
|||
|
".css .hljs-tag,.hljs-rule .hljs-property,.hljs-pseudo,.hljs-subst{color:#000}" +
|
|||
|
".css .hljs-class,.css .hljs-id{color:#9b703f}" +
|
|||
|
".hljs-value .hljs-important{color:#ff7700;font-weight:bold}" +
|
|||
|
".hljs-rule .hljs-keyword{color:#c5af75}" +
|
|||
|
".hljs-annotation,.apache .hljs-sqbracket,.nginx .hljs-built_in{color:#9b859d}" +
|
|||
|
".hljs-preprocessor,.hljs-preprocessor *,.hljs-pragma{color:#643820}" +
|
|||
|
".tex .hljs-formula{background-color:#eee;font-style:italic}" +
|
|||
|
".diff .hljs-header,.hljs-chunk{color:#808080;font-weight:bold}" +
|
|||
|
".diff .hljs-change{background-color:#bccff9}" +
|
|||
|
".hljs-addition{background-color:#baeeba}" +
|
|||
|
".hljs-deletion{background-color:#ffc8bd}" +
|
|||
|
".hljs-comment .hljs-doctag{font-weight:bold}" +
|
|||
|
".method .hljs-id{color:#000}</style>";
|
|||
|
|
|||
|
function isMarkdeepScriptName(str) { return str.search(/markdeep\S*?\.js$/i) !== -1; }
|
|||
|
function toArray(list) { return Array.prototype.slice.call(list); }
|
|||
|
|
|||
|
// Intentionally uninitialized global variable used to detect
|
|||
|
// recursive invocations
|
|||
|
if (! window.alreadyProcessedMarkdeep) {
|
|||
|
window.alreadyProcessedMarkdeep = true;
|
|||
|
|
|||
|
// Detect the noformat argument to the URL
|
|||
|
var noformat = (window.location.href.search(/\?.*noformat.*/i) !== -1);
|
|||
|
|
|||
|
// Export relevant methods
|
|||
|
window.markdeep = Object.freeze({
|
|||
|
format: markdeepToHTML,
|
|||
|
formatDiagram: diagramToSVG,
|
|||
|
stylesheet: function() {
|
|||
|
return STYLESHEET + sectionNumberingStylesheet() + HIGHLIGHT_STYLESHEET;
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
var MATHJAX_CONFIG ='<script type="text/x-mathjax-config">MathJax.Hub.Config({ TeX: { equationNumbers: {autoNumber: "AMS"} } });</script>' +
|
|||
|
'<span style="display:none">' +
|
|||
|
// Custom definitions (NC == \newcommand)
|
|||
|
'$$NC{\\n}{\\hat{n}}NC{\\w}{\\hat{\\omega}}NC{\\wi}{\\w_\\mathrm{i}}NC{\\wo}{\\w_\\mathrm{o}}NC{\\wh}{\\w_\\mathrm{h}}NC{\\Li}{L_\\mathrm{i}}NC{\\Lo}{L_\\mathrm{o}}NC{\\Le}{L_\\mathrm{e}}NC{\\Lr}{L_\\mathrm{r}}NC{\\Lt}{L_\\mathrm{t}}NC{\\O}{\\mathrm{O}}NC{\\degrees}{{^{\\large\\circ}}}NC{\\T}{\\mathsf{T}}NC{\\mathset}[1]{\\mathbb{#1}}NC{\\Real}{\\mathset{R}}NC{\\Integer}{\\mathset{Z}}NC{\\Boolean}{\\mathset{B}}NC{\\Complex}{\\mathset{C}}NC{\\un}[1]{\\,\\mathrm{#1}}$$\n'.rp(/NC/g, '\\newcommand') +
|
|||
|
'</span>\n'
|
|||
|
var MATHJAX_URL = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
|
|||
|
|
|||
|
function loadMathJax() {
|
|||
|
// Dynamically load mathjax
|
|||
|
var script = document.createElement("script");
|
|||
|
script.type = "text/javascript";
|
|||
|
script.src = MATHJAX_URL;
|
|||
|
document.getElementsByTagName("head")[0].appendChild(script);
|
|||
|
}
|
|||
|
|
|||
|
function needsMathJax(html) {
|
|||
|
// Need MathJax if $$ ... $$, \( ... \), or \begin{
|
|||
|
return option('detectMath') &&
|
|||
|
((html.search(/(?:\$\$[\s\S]+\$\$)|(?:\\begin{)/m) !== -1) ||
|
|||
|
(html.search(/\\\(.*\\\)/) !== -1));
|
|||
|
}
|
|||
|
|
|||
|
var mode = option('mode');
|
|||
|
switch (mode) {
|
|||
|
case 'script':
|
|||
|
// Nothing to do
|
|||
|
return;
|
|||
|
|
|||
|
case 'html':
|
|||
|
case 'doxygen':
|
|||
|
toArray(document.getElementsByClassName('diagram')).concat(toArray(document.getElementsByTagName('diagram'))).forEach(
|
|||
|
function (element) {
|
|||
|
var src = unescapeHTMLEntities(element.innerHTML);
|
|||
|
// Remove the first and last string (which probably
|
|||
|
// had the pre or diagram tag as part of them) if they are
|
|||
|
// empty except for whitespace.
|
|||
|
src = src.rp(/(:?^[ \t]*\n)|(:?\n[ \t]*)$/g, '');
|
|||
|
|
|||
|
if (mode === 'doxygen') {
|
|||
|
// Undo Doxygen's &ndash and &mdash, which are impossible to
|
|||
|
// detect once the browser has parsed the document
|
|||
|
src = src.rp(new RegExp('\u2013', 'g'), '--');
|
|||
|
src = src.rp(new RegExp('\u2014', 'g'), '---');
|
|||
|
|
|||
|
// Undo Doxygen's links within the diagram because they throw off spacing
|
|||
|
src = src.rp(/<a class="el" .*>(.*)<\/a>/g, '$1');
|
|||
|
}
|
|||
|
element.outerHTML = '<center class="md">' + diagramToSVG(removeLeadingSpace(src), '') + '</center>';
|
|||
|
});
|
|||
|
|
|||
|
var anyNeedsMathJax = false;
|
|||
|
toArray(document.getElementsByClassName('markdeep')).concat(toArray(document.getElementsByTagName('markdeep'))).forEach(
|
|||
|
function (src) {
|
|||
|
var dst = document.createElement('div');
|
|||
|
var html = markdeepToHTML(removeLeadingSpace(unescapeHTMLEntities(src.innerHTML)), true);
|
|||
|
anyNeedsMathJax = anyNeedsMathJax || needsMathJax(html);
|
|||
|
dst.innerHTML = html;
|
|||
|
src.parentNode.replaceChild(dst, src);
|
|||
|
});
|
|||
|
|
|||
|
// Include our stylesheet even if there are no MARKDEEP tags, but do not include the BODY_STYLESHEET.
|
|||
|
document.head.innerHTML = window.markdeep.stylesheet() + document.head.innerHTML + (anyNeedsMathJax ? MATHJAX_CONFIG : '');
|
|||
|
loadMathJax();
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
// The following is Morgan's massive hack for allowing browsers to
|
|||
|
// directly parse Markdown from what appears to be a text file, but is
|
|||
|
// actually an intentionally malformed HTML file.
|
|||
|
|
|||
|
// In order to be able to show what source files look like, the
|
|||
|
// noformat argument may be supplied.
|
|||
|
|
|||
|
if (! noformat) {
|
|||
|
// Remove any recursive references to this script so that we don't trigger the cost of
|
|||
|
// recursive *loading*. (The alreadyProcessedMarkdeep variable will prevent recursive
|
|||
|
// *execution*.) We allow other scripts to pass through.
|
|||
|
toArray(document.getElementsByTagName('script')).forEach(function(node) {
|
|||
|
if (isMarkdeepScriptName(node.src)) {
|
|||
|
node.parentNode.removeChild(node);
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
// Hide the body while formatting
|
|||
|
document.body.style.visibility = 'hidden';
|
|||
|
}
|
|||
|
|
|||
|
var source = nodeToMarkdeepSource(document.body);
|
|||
|
|
|||
|
if (noformat) {
|
|||
|
// Abort processing.
|
|||
|
source = source.rp(/<!-- Markdeep:.+$/gm, '') + MARKDEEP_LINE;
|
|||
|
|
|||
|
// Escape the <> (not ampersand) that we just added
|
|||
|
source = source.rp(/</g, '<').rp(/>/g, '>');
|
|||
|
|
|||
|
// Replace the Markdeep line itself so that ?noformat examples have a valid line to copy
|
|||
|
document.body.innerHTML = entag('pre', source);
|
|||
|
|
|||
|
var fallbackNodes = document.getElementsByClassName('fallback');
|
|||
|
for (var i = 0; i < fallbackNodes.length; ++i) {
|
|||
|
fallbackNodes[i].remove();
|
|||
|
}
|
|||
|
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
var markdeepProcessor = function() {
|
|||
|
// Recompute the source text from the current version of the document
|
|||
|
var source = nodeToMarkdeepSource(document.body);
|
|||
|
|
|||
|
source = unescapeHTMLEntities(source);
|
|||
|
var markdeepHTML = markdeepToHTML(source, false);
|
|||
|
|
|||
|
// console.log(markdeepHTML); // Final processed source
|
|||
|
|
|||
|
var needMathJax = needsMathJax(markdeepHTML);
|
|||
|
if (needMathJax) {
|
|||
|
markdeepHTML = MATHJAX_CONFIG + markdeepHTML;
|
|||
|
}
|
|||
|
|
|||
|
markdeepHTML += MARKDEEP_FOOTER;
|
|||
|
|
|||
|
// Replace the document. If using MathJax, include the custom Markdeep definitions
|
|||
|
var longDocument = source.length > 1000;
|
|||
|
var head = BODY_STYLESHEET + STYLESHEET + sectionNumberingStylesheet() + HIGHLIGHT_STYLESHEET;
|
|||
|
if (longDocument) {
|
|||
|
// Add more spacing before the title in a long document
|
|||
|
head += entag('style', 'div.title { padding-top: 40px; } div.afterTitles { height: 15px; }');
|
|||
|
}
|
|||
|
|
|||
|
if (window.location.href.search(/\?.*export.*/i) !== -1) {
|
|||
|
// Export mode
|
|||
|
var text = '<meta charset="UTF-8"><meta http-equiv="content-type" content="text/html;charset=UTF-8">' + head + document.head.innerHTML + markdeepHTML;
|
|||
|
if (needMathJax) {
|
|||
|
// Dynamically load mathjax
|
|||
|
text += '<script src="' + MATHJAX_URL +'"></script>';
|
|||
|
}
|
|||
|
document.body.innerHTML = entag('code', escapeHTMLEntities(text));
|
|||
|
} else {
|
|||
|
document.head.innerHTML = '<meta charset="UTF-8"><meta http-equiv="content-type" content="text/html;charset=UTF-8">' + head + document.head.innerHTML;
|
|||
|
document.body.innerHTML = markdeepHTML;
|
|||
|
if (needMathJax) { loadMathJax(); }
|
|||
|
}
|
|||
|
|
|||
|
document.body.style.visibility = 'visible';
|
|||
|
};
|
|||
|
|
|||
|
///////////// INSERT command processing
|
|||
|
// Helper function for use by children
|
|||
|
function sendContentsToMyParent() {
|
|||
|
// console.log(location.pathname + " sent message to parent");
|
|||
|
// Send the document contents after the childFrame replaced itself
|
|||
|
// (not the source variable captured when this function was defined!)
|
|||
|
parent.postMessage(myID + '=' + document.body.innerHTML, '*');
|
|||
|
}
|
|||
|
|
|||
|
// Strip the filename from the url, if there is one (and it is a string)
|
|||
|
function removeFilename(url) {
|
|||
|
return url && url.ss(0, url.lastIndexOf('/') + 1);
|
|||
|
}
|
|||
|
|
|||
|
var myURLParse = /([^?]+)(?:\?id=(inc\d+)&p=([^&]+))?/.exec(location.href);
|
|||
|
|
|||
|
var myBase = removeFilename(myURLParse[1]);
|
|||
|
var myID = myURLParse[2];
|
|||
|
var parentBase = removeFilename(myURLParse[3] && decodeURIComponent(myURLParse[3]));
|
|||
|
var childFrameStyle = 'display:none';
|
|||
|
var includeCounter = 0;
|
|||
|
var IAmAChild = myID; // !== undefined
|
|||
|
var IAmAParent = false;
|
|||
|
var numIncludeChildrenLeft = 0;
|
|||
|
|
|||
|
var messageCallback = function (event) {
|
|||
|
// Parse the message. Ensure that it is for the Markdeep/include.js system.
|
|||
|
var childID = false;
|
|||
|
var childBody = event.data.substring && event.data.replace(/^(inc\d+)=/, function (match, a) {
|
|||
|
childID = a;
|
|||
|
return '';
|
|||
|
});
|
|||
|
|
|||
|
if (childID) {
|
|||
|
// This message event was for the Markdeep/include.js system
|
|||
|
|
|||
|
//console.log(location.href + ' received a message from child ' + childID);
|
|||
|
|
|||
|
// Replace the corresponding node's contents
|
|||
|
var childFrame = document.getElementById(childID);
|
|||
|
childFrame.outerHTML = '\n' + childBody + '\n';
|
|||
|
|
|||
|
--numIncludeChildrenLeft;
|
|||
|
|
|||
|
// console.log(window.location.pathname, 'numIncludeChildrenLeft = ' + numIncludeChildrenLeft);
|
|||
|
|
|||
|
if (numIncludeChildrenLeft <= 0) {
|
|||
|
if (IAmAChild) {
|
|||
|
//console.log("Intermediate node " + location.pathname + " sending to parent");
|
|||
|
sendContentsToMyParent();
|
|||
|
} else {
|
|||
|
// The entire document is complete, so run the markdeep processor
|
|||
|
// as soon as the document has recovered from our replacements
|
|||
|
setTimeout(markdeepProcessor, 0);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
source = source.rp(/(?:^|\s)\(insert[ \t]+(\S+\.\S*)[ \t]+here\)\s/g, function(match, src) {
|
|||
|
if (numIncludeChildrenLeft === 0) {
|
|||
|
// This is the first child observed. Prepare to receive messages from the
|
|||
|
// embedded children.
|
|||
|
IAmAParent = true;
|
|||
|
addEventListener("message", messageCallback);
|
|||
|
}
|
|||
|
|
|||
|
++numIncludeChildrenLeft;
|
|||
|
// console.log(window.location.pathname, 'numIncludeChildrenLeft = ' + numIncludeChildrenLeft);
|
|||
|
|
|||
|
// Replace this tag with a frame that loads the document. Once loaded, it will
|
|||
|
// send a message with its contents for use as a replacement.
|
|||
|
var childID = 'inc' + (++includeCounter);
|
|||
|
return '<iframe src="' + src + '?id=' + childID + '&p=' + encodeURIComponent(myBase) +
|
|||
|
'"id="' + childID + '"style="' + childFrameStyle + '" content="text/html;charset=UTF-8"></iframe>';
|
|||
|
});
|
|||
|
|
|||
|
// console.log("after insert: "+ source);
|
|||
|
|
|||
|
if (IAmAParent) {
|
|||
|
// I'm waiting on children, so don't run the full processor yet,
|
|||
|
// but do substitute the iframe code so that it can launch.
|
|||
|
document.body.innerHTML = source;
|
|||
|
} else if (IAmAChild) {
|
|||
|
// I'm a child and not a parent, so trigger the send now
|
|||
|
// console.log("Leaf node " + location.pathname + " sending to parent");
|
|||
|
sendContentsToMyParent();
|
|||
|
} else {
|
|||
|
// No includes. Run markdeep processing after the rest of this file parses
|
|||
|
// console.log("non-parent, non-child Parent scheduling markdeepProcessor");
|
|||
|
setTimeout(markdeepProcessor, 0);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
})();
|
|||
|
|