# HG changeset patch # User Kai T. Ohlhus # Date 1503328948 -7200 # Node ID 58b76c741c3d536c7c614b0a7ba3c4a1b79831eb # Parent d64985eaf56dc1df5073a9cea51b0c4b8b43a521 publish: improve detecion of block markup (bug #51782) * scripts/general/publish.m: improve detecion of block markup by using a recursive strategy with regular expressions, rather than parsing line by line. There was a problem, because such a or block was never able to span an empty line in the previous strategy. * scripts/general/private/__publish_html_output__.m (do_html), scripts/general/private/__publish_latex_output__.m (do_latex): sourround output by newlines. * test/publish/test_script.m: New test cases for block markup with empty lines. diff -r d64985eaf56d -r 58b76c741c3d scripts/general/private/__publish_html_output__.m --- a/scripts/general/private/__publish_html_output__.m Sun Aug 20 21:09:27 2017 -0700 +++ b/scripts/general/private/__publish_html_output__.m Mon Aug 21 17:22:28 2017 +0200 @@ -257,7 +257,7 @@ endfunction function outstr = do_html (str) - outstr = str; + outstr = ["\n" str "\n"]; endfunction function outstr = do_latex (str) diff -r d64985eaf56d -r 58b76c741c3d scripts/general/private/__publish_latex_output__.m --- a/scripts/general/private/__publish_latex_output__.m Sun Aug 20 21:09:27 2017 -0700 +++ b/scripts/general/private/__publish_latex_output__.m Mon Aug 21 17:22:28 2017 +0200 @@ -225,7 +225,7 @@ endfunction function outstr = do_latex (str) - outstr = str; + outstr = ["\n" str "\n"]; endfunction function outstr = do_link (url_str, str) diff -r d64985eaf56d -r 58b76c741c3d scripts/general/publish.m --- a/scripts/general/publish.m Sun Aug 20 21:09:27 2017 -0700 +++ b/scripts/general/publish.m Mon Aug 21 17:22:28 2017 +0200 @@ -587,8 +587,34 @@ return; endif + ## Extract and blocks recursively. + content_str = strjoin (content, "\n"); + tags = {"html", "latex"}; + for i = 1:length(tags) + tok = regexp (content_str, ... + ['(.*?)(^|\n\n)(<', tags{i}, '>)\n(.*?)\n(<\/', ... + tags{i}, '>)($|\n\n)(.*)'], "tokens", "once"); + if (! isempty (tok)) + ## If there was some text before that block --> recursion + if (! strcmpi (tok{1}, ["<", tags{i}, ">"])) + p_content = parse_paragraph_content (strsplit (tok{1}, "\n")); + tok(1:2) = []; + endif + ## Extract the block content + p_content{end+1}.type = tags{i}; + p_content{end}.content = tok{2}; + ## If there was some text after that block --> recursion + if (length (tok) == 5) + p_content = [p_content, ... + parse_paragraph_content(strsplit (tok{5}, "\n"))]; + endif + return; + endif + endfor + ## Split into blocks separated by empty lines idx = [0, find(cellfun (@isempty, content)), length(content) + 1]; + ## For each block for i = find (diff (idx) > 1) block = content(idx(i) + 1:idx(i+1) - 1); @@ -652,58 +678,21 @@ continue; endif - ## Parse remaining blocks line by line - j = 1; - while (j <= numel (block)) - ## HTML markup - if (strcmpi (block{j}, "")) - start_html = j + 1; - while (j < numel (block) && ! strcmpi (block{j}, "")) - j++; - endwhile - if (j == numel (block) && ! strcmpi (block{j}, "")) - warning ("publish: no closing found"); - else - j++; # Skip closing tag - endif - if (j > start_html) - p_content{end+1}.type = "html"; - p_content{end}.content = strjoin (block(start_html:j-2), "\n"); - endif - ## LaTeX markup - elseif (strcmpi (block{j}, "")) - start_latex = j + 1; - while (j < numel (block) && ! strcmpi (block{j}, "")) - j++; - endwhile - if (j == numel (block) && ! strcmpi (block{j}, "")) - warning ("publish: no closing found"); - else - j++; # Skip closing tag - endif - if (j > start_latex) - p_content{end+1}.type = "latex"; - p_content{end}.content = strjoin (block(start_latex:j-2), "\n"); - endif - ## Remaining normal text or markups belonging to normal text - ## that are handled while output generation: - ## - ## * Italic "_", bold "*", and monospaced "|" text - ## * Inline "$" and block "$$" LaTeX math - ## * Links - ## * Trademark symbols - else - if (j == 1 || isempty (p_content) - || ! strcmp (p_content{end}.type, "text")) - p_content{end+1}.type = "text"; - p_content{end}.content = block{j}; - else - p_content{end}.content = strjoin ({p_content{end}.content, ... - block{j}}, "\n"); - endif - j++; - endif - endwhile + ## Now it can be only normal text or markups belonging to normal text + ## that are handled while output generation: + ## + ## * Italic "_", bold "*", and monospaced "|" text + ## * Inline "$" and block "$$" LaTeX math + ## * Links + ## * Trademark symbols + block = strjoin (block, "\n"); + if (isempty (p_content) || ! strcmp (p_content{end}.type, "text")) + p_content{end+1}.type = "text"; + p_content{end}.content = block; + else + p_content{end}.content = strjoin ({p_content{end}.content, block}, ... + "\n"); + endif endfor endfunction diff -r d64985eaf56d -r 58b76c741c3d test/publish/test_script.m --- a/test/publish/test_script.m Sun Aug 20 21:09:27 2017 -0700 +++ b/test/publish/test_script.m Mon Aug 21 17:22:28 2017 +0200 @@ -225,6 +225,30 @@ # # +%% HTML Markup with empty lines +% +% +% +% +%
onetwo
+% +% and some text. +% +% + +## HTML Markup with empty lines +# +# +# +# +# +# +#
onetwo
+# +# and some text. +# +# + %% LaTeX Markup % % \begin{equation} @@ -245,6 +269,30 @@ # # +%% LaTeX Markup with empty lines +% +% Some text +% +% \begin{equation} +% \begin{pmatrix} +% 1 & 2 \\ 3 & 4 +% \end{pmatrix} +% \end{equation} +% +% + +## LaTeX Markup with empty lines +# +# Some text +# +# \begin{equation} +# \begin{pmatrix} +# 1 & 2 \\ 3 & 4 +# \end{pmatrix} +# \end{equation} +# +# + %% Long void % %