changeset 23923:62dc81691d73

publish: improve detection of inline markup and fix MathJax usage. * scripts/general/publish.m: improve detection of inline markup by using more sophisticated regular expressions compatible to Matlab. Let the formatter decide what to do with inline and block math, as it is not for granted, that all possible output formats accept "$" and "$$" as math delimiters. * scripts/general/private/__publish_latex_output__.m: New formatting functions for inline and block math. Improved documentation. * scripts/general/private/__publish_html_output__.m: New formatting functions for inline and block math. Improved documentation. Use another CDN according to shutdown of MathJaxCDN https://www.mathjax.org/cdn-shutting-down/.
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Wed, 16 Aug 2017 16:34:01 +0200
parents 7d703bca19b8
children 0519eaf0cca6
files scripts/general/private/__publish_html_output__.m scripts/general/private/__publish_latex_output__.m scripts/general/publish.m
diffstat 3 files changed, 77 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/private/__publish_html_output__.m	Wed Aug 16 08:19:16 2017 +0200
+++ b/scripts/general/private/__publish_html_output__.m	Wed Aug 16 16:34:01 2017 +0200
@@ -71,6 +71,12 @@
 ## @samp{text} (str)
 ##
 ## @item
+## @samp{blockmath} (str)
+##
+## @item
+## @samp{inlinemath} (str)
+##
+## @item
 ## @samp{bold} (str)
 ##
 ## @item
@@ -107,7 +113,7 @@
 "});",
 "</script>",
 ["<script type=\"text/javascript\" async ", ...
- "src=\"https://cdn.mathjax.org/mathjax/latest/MathJax.js?", ...
+ "src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?", ...
  "config=TeX-MML-AM_CHTML\"></script>"]);
 
   stylesheet_str = sprintf ("%s\n",
@@ -266,6 +272,14 @@
   outstr = ["\n<p>" str "</p>\n"];
 endfunction
 
+function outstr = do_blockmath (str)
+  outstr = ["$$" str "$$"];
+endfunction
+
+function outstr = do_inlinemath (str)
+  outstr = ["$" str "$"];
+endfunction
+
 function outstr = do_bold (str)
   outstr = ["<b>" str "</b>"];
 endfunction
--- a/scripts/general/private/__publish_latex_output__.m	Wed Aug 16 08:19:16 2017 +0200
+++ b/scripts/general/private/__publish_latex_output__.m	Wed Aug 16 16:34:01 2017 +0200
@@ -71,6 +71,12 @@
 ## @samp{text} (str)
 ##
 ## @item
+## @samp{blockmath} (str)
+##
+## @item
+## @samp{inlinemath} (str)
+##
+## @item
 ## @samp{bold} (str)
 ##
 ## @item
@@ -230,6 +236,14 @@
   outstr = ["\n\n" escape_latex(str) "\n\n"];
 endfunction
 
+function outstr = do_blockmath (str)
+  outstr = ["$$" str "$$"];
+endfunction
+
+function outstr = do_inlinemath (str)
+  outstr = ["$" str "$"];
+endfunction
+
 function outstr = do_bold (str)
   outstr = ['\textbf{' str '}'];
 endfunction
--- a/scripts/general/publish.m	Wed Aug 16 08:19:16 2017 +0200
+++ b/scripts/general/publish.m	Wed Aug 16 16:34:01 2017 +0200
@@ -816,21 +816,51 @@
 
 function str = format_text (str, formatter)
   ## FORMAT_TEXT formats inline formats in strings.
-  ##   These are: links, bold, italic, monospaced, (TM), (R)
+  ##   These are: links, block/inline math, bold, italic, monospaced, (TM), (R)
+
+  ## Helper to clarify the following regular expressions.  It is suitable for
+  ## inline formatting, that is delimited literally at start and end by
+  ## `delim`.  `term` is an indicating character for the end delimiter.
+  ##
+  ## Best explained by example ('^' start and '$' end of input):
+  ##
+  ##  Positive matches:
+  ##
+  ##    ^*bold*$
+  ##    ^*bold*.$
+  ##    ^(*bold*)$
+  ##    ^ *bold* $
+  ##    ^Text *bold* text$
+  ##    ^*bold text*$
+  ##
+  ##  Negative matches:
+  ##
+  ##    ^Text*bold*text$
+  ##    ^*bold *$
+  ##    ^* bold* $
+  ##    ^*bold text *$
+  ##
+  regex_helper = @(delim, term) ['(^|(?<=\s)|(?=\W))', delim, ...
+    '(?!\s)[^', term, ']*(?<!\s)', delim, '($|(?=\s)|(?=\W))'];
 
   ## Regular expressions for the formats:
   ##
-  ## * Links "<http://www.someurl.com>"
-  ## * Links "<octave:Function TEXT>"
-  ## * Links "<http://www.someurl.com TEXT>"
-  ## * inline "$" and block "$$" LaTeX math
+  ## 1) Links "<http://www.someurl.com>"
+  ## 2) Links "<octave:Function TEXT>"
+  ## 3) Links "<http://www.someurl.com TEXT>"
+  ## 4) LaTeX block math "$$x^2$$"
+  ## 5) LaTeX inline math "$x^2$"
+  ## 6) Bold *text*
+  ## 7) Italic _text_
+  ## 8) Monospaced |text|
   regexes = {'<\S{3,}[^\s<>]*>', ...
              '<octave:[^\s<>]* *[^<>$]*>', ...
              '<\S{3,}[^\s<>]* *[^<>$]*>', ...
-             '\${1,2}.*?\${1,2}', ...
-             '\*[^*]*\*', ...  # Bold
-             '_[^_]*_', ...    # Italic
-             '\|[^|]*\|'};     # Monospaced
+             regex_helper('\$\$', '$'), ...
+             regex_helper('\$', '$'), ...
+             regex_helper('\*', '*'), ...
+             regex_helper('_', '_'), ...
+             regex_helper('\|', '|')};
 
   ## Function to escape some special characters for the GNU Octave manual,
   ## see https://www.gnu.org/software/texinfo/manual/texinfo/html_node/HTML-Xref-Node-Name-Expansion.html
@@ -873,16 +903,22 @@
           txt = format_text (txt(idx+1:end-1), formatter);
           cstr{j} = formatter ("link", url, txt);
         case 4
-          ## inline "$" and block "$$" LaTeX math --> do nothing
+          ## LaTeX block math "$$"
+          txt = cstr{j};
+          cstr{j} = formatter ("blockmath", txt(3:end-2));
         case 5
+          ## LaTeX inline math "$"
+          txt = cstr{j};
+          cstr{j} = formatter ("inlinemath", txt(2:end-1));
+        case 6
           ## Bold
           txt = cstr{j};
           cstr{j} = formatter ("bold", format_text (txt(2:end-1), formatter));
-        case 6
+        case 7
           ## Italic
           txt = cstr{j};
           cstr{j} = formatter ("italic", format_text (txt(2:end-1), formatter));
-        case 7
+        case 8
           ## Monospaced
           txt = cstr{j};
           cstr{j} = formatter ("monospaced", format_text (txt(2:end-1), ...