comparison scripts/general/private/__publish_latex_output__.m @ 22705:e9a0aa0a49ed

Overhaul publish function and it's private helper functions. * publish.m: Rewrite docstring. Don't use narginchk, nargoutchk. Don't end error messages with a period. Fix input validation to check that PROPERTY_NAME in a propert/value pair is a string, but value does not need to be. Rewrite messages in error() function calls. Use single quotes to reduce excessive backslashing. Don't require TRUE/FALSE options to be boolean, just real. Remove excessive parentheses around tests in if conditionals. Rename doc_struct to just doc for simplicity. Prefer numel() to length(). Correct cuddling of parentheses to indicate indexing versus function call. Use do/until loop rather than while loop to simplify loop setup. Use str2double rather than str2num. __publish_html_output__.m: Use sprintf() to create HERE documents to make definition of large constant blocks of text easier to understand. Prefer numel() over length(). Rename all handle_XXX functions to do_XXX to follow Octave convention. Use single quoted strings to avoid excessive backslashing. Add newlines to lists so that generated HTML is human-readable. Use in-place operators for efficiency. Use sprintf rather than num2str for efficiency. Use direct comparison when looking at a single character rather than strcmp for efficiency. Use single regexprep with alternation rather than for loop with individual patterns for efficiency. * __publish_latex_output__.m: Use sprintf() to create HERE documents to make definition of large constant blocks of text easier to understand. Prefer numel() over length(). Rename all handle_XXX functions to do_XXX to follow Octave convention. Use single quoted strings to avoid excessive backslashing.
author Rik <rik@octave.org>
date Mon, 31 Oct 2016 22:33:00 -0700
parents ac45255ccd2c
children 449a5e84185a
comparison
equal deleted inserted replaced
22704:b5407b1ab11a 22705:e9a0aa0a49ed
1 function outstr = __publish_latex_output__ (varargin) 1 function outstr = __publish_latex_output__ (type, varargin)
2 ## 2 ## Recognized types are:
3 ## Types to handle are:
4 ## 3 ##
5 ## * "header" (title_str, intro_str, toc_cstr) 4 ## * "header" (title_str, intro_str, toc_cstr)
6 ## * "footer" () 5 ## * "footer" ()
7 ## * "code" (str) 6 ## * "code" (str)
8 ## * "code_output" (str) 7 ## * "code_output" (str)
19 ## * "italic" (str) 18 ## * "italic" (str)
20 ## * "monospaced" (str) 19 ## * "monospaced" (str)
21 ## * "link" (url_str, url_str, str) 20 ## * "link" (url_str, url_str, str)
22 ## * "TM" () 21 ## * "TM" ()
23 ## * "R" () 22 ## * "R" ()
24 ## 23
25 eval (["outstr = handle_", varargin{1}, " (varargin{2:end});"]); 24 outstr = feval (["do_" type], varargin{:});
26 endfunction 25 endfunction
27 26
28 function outstr = handle_header (title_str, intro_str, toc_cstr) 27 function outstr = do_header (title_str, intro_str, toc_cstr)
29 publish_comment = ["\n\n", ... 28 publish_comment = sprintf ("%s\n",
30 "% This document was generated by the publish-function\n", ... 29 "",
31 "% from GNU Octave ", version(), "\n\n"]; 30 "",
31 "% This document was generated by the publish-function",
32 ["% from GNU Octave " version()],
33 "");
32 34
33 latex_preamble = ["\n\n", ... 35 latex_preamble = sprintf ("%s\n",
34 "\\documentclass[10pt]{article}\n", ... 36 "",
35 "\\usepackage{listings}\n", ... 37 "",
36 "\\usepackage{mathtools}\n", ... 38 '\documentclass[10pt]{article}',
37 "\\usepackage{amssymb}\n", ... 39 '\usepackage{listings}',
38 "\\usepackage{graphicx}\n", ... 40 '\usepackage{mathtools}',
39 "\\usepackage{hyperref}\n", ... 41 '\usepackage{amssymb}',
40 "\\usepackage{xcolor}\n", ... 42 '\usepackage{graphicx}',
41 "\\usepackage{titlesec}\n", ... 43 '\usepackage{hyperref}',
42 "\\usepackage[utf8]{inputenc}\n", ... 44 '\usepackage{xcolor}',
43 "\\usepackage[T1]{fontenc}\n", ... 45 '\usepackage{titlesec}',
44 "\\usepackage{lmodern}\n"]; 46 '\usepackage[utf8]{inputenc}',
47 '\usepackage[T1]{fontenc}',
48 '\usepackage{lmodern}');
45 49
46 listings_option = ["\n\n", ... 50 listings_option = sprintf ("%s\n",
47 "\\lstset{\n", ... 51 "",
48 "language=Octave,\n", ... 52 "",
49 "numbers=none,\n", ... 53 '\lstset{',
50 "frame=single,\n", ... 54 'language=Octave,',
51 "tabsize=2,\n", ... 55 'numbers=none,',
52 "showstringspaces=false,\n", ... 56 'frame=single,',
53 "breaklines=true}\n"]; 57 'tabsize=2,',
58 'showstringspaces=false,',
59 'breaklines=true}');
54 60
55 latex_head = ["\n\n", ... 61 latex_head = sprintf ("%s\n",
56 "\\titleformat*{\\section}{\\Huge\\bfseries}\n", ... 62 "",
57 "\\titleformat*{\\subsection}{\\large\\bfseries}\n", ... 63 "",
58 "\\renewcommand{\\contentsname}{\\Large\\bfseries Contents}\n", ... 64 '\titleformat*{\section}{\Huge\bfseries}',
59 "\\setlength{\\parindent}{0pt}\n\n",... 65 '\titleformat*{\subsection}{\large\bfseries}',
60 "\\begin{document}\n\n", ... 66 '\renewcommand{\contentsname}{\Large\bfseries Contents}',
61 "{\\Huge\\section*{", escape_latex(title_str),"}}\n\n", ... 67 '\setlength{\parindent}{0pt}',
62 "\\tableofcontents\n", ... 68 "",
63 "\\vspace*{4em}\n\n"]; 69 '\begin{document}',
70 "",
71 ['{\Huge\section*{' escape_latex(title_str) '}}'],
72 "",
73 '\tableofcontents',
74 '\vspace*{4em}',
75 "");
64 76
65 outstr = [publish_comment, latex_preamble, listings_option, latex_head]; 77 outstr = [publish_comment, latex_preamble, listings_option, latex_head];
78
66 endfunction 79 endfunction
67 80
68 function outstr = handle_footer (m_source_str) 81 function outstr = do_footer (m_source_str)
69 outstr = ["\n\n\\end{document}\n"]; 82 outstr = ["\n\n" '\end{document}' "\n"];
70 endfunction 83 endfunction
71 84
72 function outstr = handle_code (str) 85 function outstr = do_code (str)
73 outstr = ["\\begin{lstlisting}\n", str, "\n\\end{lstlisting}\n"]; 86 outstr = ['\begin{lstlisting}' "\n", str, "\n" '\end{lstlisting}' "\n"];
74 endfunction 87 endfunction
75 88
76 function outstr = handle_code_output (str) 89 function outstr = do_code_output (str)
77 outstr = ["\\begin{lstlisting}", ... 90 outstr = sprintf ("%s\n",
78 "[language={},xleftmargin=5pt,frame=none]\n", ... 91 '\begin{lstlisting}[language={},xleftmargin=5pt,frame=none]',
79 str, "\n\\end{lstlisting}\n"]; 92 str,
93 '\end{lstlisting}');
80 endfunction 94 endfunction
81 95
82 function outstr = handle_section (str) 96 function outstr = do_section (str)
83 outstr = ["\n\n\\phantomsection\n", ... 97 outstr = sprintf ("%s\n",
84 "\\addcontentsline{toc}{section}{", escape_latex(str), "}\n", ... 98 "",
85 "\\subsection*{", escape_latex(str), "}\n\n"]; 99 "",
100 '\phantomsection',
101 ['\addcontentsline{toc}{section}{' escape_latex(str) '}'],
102 ['\subsection*{' escape_latex(str) '}'],
103 "");
86 endfunction 104 endfunction
87 105
88 function outstr = handle_preformatted_code (str) 106 function outstr = do_preformatted_code (str)
89 outstr = ["\\begin{lstlisting}\n", str, "\n\\end{lstlisting}\n"]; 107 outstr = sprintf ("%s\n",
108 '\begin{lstlisting}',
109 str,
110 '\end{lstlisting}');
90 endfunction 111 endfunction
91 112
92 function outstr = handle_preformatted_text (str) 113 function outstr = do_preformatted_text (str)
93 outstr = ["\\begin{lstlisting}[language={}]\n", ... 114 outstr = sprintf ("%s\n",
94 str, "\n\\end{lstlisting}\n"]; 115 '\begin{lstlisting}[language={}]',
116 str,
117 '\end{lstlisting}');
95 endfunction 118 endfunction
96 119
97 function outstr = handle_bulleted_list (cstr) 120 function outstr = do_bulleted_list (cstr)
98 outstr = "\n\\begin{itemize}\n"; 121 outstr = ["\n" '\begin{itemize}' "\n"];
99 for i = 1:length(cstr) 122 for i = 1:numel (cstr)
100 outstr = [outstr, "\\item ", escape_latex(cstr{i}), "\n"]; 123 outstr = [outstr, '\item ' escape_latex(cstr{i}) "\n"];
101 endfor 124 endfor
102 outstr = [outstr, "\\end{itemize}\n"]; 125 outstr = [outstr, '\end{itemize}' "\n"];
103 endfunction 126 endfunction
104 127
105 function outstr = handle_numbered_list (cstr) 128 function outstr = do_numbered_list (cstr)
106 outstr = "\n\\begin{enumerate}\n"; 129 outstr = ["\n" '\begin{enumerate}' "\n"];
107 for i = 1:length(cstr) 130 for i = 1:numel (cstr)
108 outstr = [outstr, "\\item ", escape_latex(cstr{i}), "\n"]; 131 outstr = [outstr, '\item ' escape_latex(cstr{i}) "\n"];
109 endfor 132 endfor
110 outstr = [outstr, "\\end{enumerate}\n"]; 133 outstr = [outstr, "\\end{enumerate}\n"];
111 endfunction 134 endfunction
112 135
113 function outstr = handle_graphic (str) 136 function outstr = do_graphic (str)
114 outstr = ["\\begin{figure}[!ht]\n", ... 137 outstr = sprintf ("%s\n",
115 "\\includegraphics[width=\\textwidth]{", str, "}\n", ... 138 '\begin{figure}[!ht]',
116 "\\end{figure}\n"]; 139 ['\includegraphics[width=\textwidth]{' str '}'],
140 '\end{figure}');
117 endfunction 141 endfunction
118 142
119 function outstr = handle_html (str) 143 function outstr = do_html (str)
120 outstr = ""; 144 outstr = "";
121 endfunction 145 endfunction
122 146
123 function outstr = handle_latex (str) 147 function outstr = do_latex (str)
124 outstr = str; 148 outstr = str;
125 endfunction 149 endfunction
126 150
127 function outstr = handle_link (url_str, str) 151 function outstr = do_link (url_str, str)
128 outstr = ["\\href{", url_str,"}{", str, "}"]; 152 outstr = ['\href{' url_str '}{' str '}'];
129 endfunction 153 endfunction
130 154
131 function outstr = handle_text (str) 155 function outstr = do_text (str)
132 outstr = ["\n\n", escape_latex(str), "\n\n"]; 156 outstr = ["\n\n" escape_latex(str) "\n\n"];
133 endfunction 157 endfunction
134 158
135 function outstr = handle_bold (str) 159 function outstr = do_bold (str)
136 outstr = ["\\textbf{", str, "}"]; 160 outstr = ['\textbf{' str '}'];
137 endfunction 161 endfunction
138 162
139 function outstr = handle_italic (str) 163 function outstr = do_italic (str)
140 outstr = ["\\textit{", str, "}"]; 164 outstr = ['\textit{' str '}'];
141 endfunction 165 endfunction
142 166
143 function outstr = handle_monospaced (str) 167 function outstr = do_monospaced (str)
144 outstr = ["\\texttt{", str, "}"]; 168 outstr = ['\texttt{' str '}'];
145 endfunction 169 endfunction
146 170
147 function outstr = handle_TM () 171 function outstr = do_TM ()
148 outstr = "\\texttrademark "; 172 outstr = '\texttrademark ';
149 endfunction 173 endfunction
150 174
151 function outstr = handle_R () 175 function outstr = do_R ()
152 outstr = "\\textregistered "; 176 outstr = '\textregistered ';
153 endfunction 177 endfunction
154 178
155 function str = escape_latex (str) 179 function str = escape_latex (str)
156 ## Escape "&", "%", "#", "_", "~", "^", "<", ">" 180 ## Escape "&", "%", "#", "_", "~", "^", "<", ">"
157 ## TODO: "\", "{", "}" 181 ## FIXME: What about: "\", "{", "}"
158 str = regexprep (str, '(?<!\\)(&)', "\\&"); 182 str = regexprep (str, '(?<!\\)(&|%|#|_)', '\\$1');
159 str = regexprep (str, '(?<!\\)(%)', "\\%");
160 str = regexprep (str, '(?<!\\)(#)', "\\#");
161 str = regexprep (str, '(?<!\\)(_)', "\\_");
162 str = regexprep (str, '(?<!\\)(~)', "\\ensuremath{\\tilde{\;}}"); 183 str = regexprep (str, '(?<!\\)(~)', "\\ensuremath{\\tilde{\;}}");
163 str = regexprep (str, '(?<!\\)(\^)', "\\^{}"); 184 str = regexprep (str, '(?<!\\)(\^)', "\\^{}");
164 str = regexprep (str, '(?<!\\)(<)', "\\ensuremath{<}"); 185 str = regexprep (str, '(?<!\\)(<)', "\\ensuremath{<}");
165 str = regexprep (str, '(?<!\\)(>)', "\\ensuremath{>}"); 186 str = regexprep (str, '(?<!\\)(>)', "\\ensuremath{>}");
166 endfunction 187 endfunction
188