view 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
line wrap: on
line source

function outstr = __publish_latex_output__ (type, varargin)
  ## Recognized types are:
  ##
  ## * "header" (title_str, intro_str, toc_cstr)
  ## * "footer" ()
  ## * "code" (str)
  ## * "code_output" (str)
  ## * "section" (str)
  ## * "preformatted_code" (str)
  ## * "preformatted_text" (str)
  ## * "bulleted_list" (cstr)
  ## * "numbered_list" (cstr)
  ## * "graphic" (str)
  ## * "html" (str)
  ## * "latex" (str)
  ## * "text" (str)
  ## * "bold" (str)
  ## * "italic" (str)
  ## * "monospaced" (str)
  ## * "link" (url_str, url_str, str)
  ## * "TM" ()
  ## * "R" ()

  outstr = feval (["do_" type], varargin{:});
endfunction

function outstr = do_header (title_str, intro_str, toc_cstr)
  publish_comment = sprintf ("%s\n",
"",
"",
"% This document was generated by the publish-function",
["% from GNU Octave " version()],
"");

  latex_preamble = sprintf ("%s\n",
"",
"",
'\documentclass[10pt]{article}',
'\usepackage{listings}',
'\usepackage{mathtools}',
'\usepackage{amssymb}',
'\usepackage{graphicx}',
'\usepackage{hyperref}',
'\usepackage{xcolor}',
'\usepackage{titlesec}',
'\usepackage[utf8]{inputenc}',
'\usepackage[T1]{fontenc}',
'\usepackage{lmodern}');

  listings_option = sprintf ("%s\n",
"",
"",
'\lstset{',
'language=Octave,',
'numbers=none,',
'frame=single,',
'tabsize=2,',
'showstringspaces=false,',
'breaklines=true}');

  latex_head = sprintf ("%s\n",
"",
"",
'\titleformat*{\section}{\Huge\bfseries}',
'\titleformat*{\subsection}{\large\bfseries}',
'\renewcommand{\contentsname}{\Large\bfseries Contents}',
'\setlength{\parindent}{0pt}',
"",
'\begin{document}',
"",
['{\Huge\section*{' escape_latex(title_str) '}}'],
"",
'\tableofcontents',
'\vspace*{4em}',
"");

  outstr = [publish_comment, latex_preamble, listings_option, latex_head];

endfunction

function outstr = do_footer (m_source_str)
  outstr = ["\n\n" '\end{document}' "\n"];
endfunction

function outstr = do_code (str)
  outstr = ['\begin{lstlisting}' "\n", str, "\n" '\end{lstlisting}' "\n"];
endfunction

function outstr = do_code_output (str)
  outstr = sprintf ("%s\n",
'\begin{lstlisting}[language={},xleftmargin=5pt,frame=none]',
str,
'\end{lstlisting}');
endfunction

function outstr = do_section (str)
  outstr = sprintf ("%s\n",
"",
"",
'\phantomsection',
['\addcontentsline{toc}{section}{' escape_latex(str) '}'],
['\subsection*{' escape_latex(str) '}'],
"");
endfunction

function outstr = do_preformatted_code (str)
  outstr = sprintf ("%s\n",
'\begin{lstlisting}',
str,
'\end{lstlisting}');
endfunction

function outstr = do_preformatted_text (str)
  outstr = sprintf ("%s\n",
'\begin{lstlisting}[language={}]',
str,
'\end{lstlisting}');
endfunction

function outstr = do_bulleted_list (cstr)
  outstr = ["\n" '\begin{itemize}' "\n"];
  for i = 1:numel (cstr)
    outstr = [outstr, '\item ' escape_latex(cstr{i}) "\n"];
  endfor
  outstr = [outstr, '\end{itemize}' "\n"];
endfunction

function outstr = do_numbered_list (cstr)
  outstr = ["\n" '\begin{enumerate}' "\n"];
  for i = 1:numel (cstr)
    outstr = [outstr, '\item ' escape_latex(cstr{i}) "\n"];
  endfor
  outstr = [outstr, "\\end{enumerate}\n"];
endfunction

function outstr = do_graphic (str)
  outstr = sprintf ("%s\n",
'\begin{figure}[!ht]',
['\includegraphics[width=\textwidth]{' str '}'],
'\end{figure}');
endfunction

function outstr = do_html (str)
  outstr = "";
endfunction

function outstr = do_latex (str)
  outstr = str;
endfunction

function outstr = do_link (url_str, str)
  outstr = ['\href{' url_str '}{' str '}'];
endfunction

function outstr = do_text (str)
  outstr = ["\n\n" escape_latex(str) "\n\n"];
endfunction

function outstr = do_bold (str)
  outstr = ['\textbf{' str '}'];
endfunction

function outstr = do_italic (str)
  outstr = ['\textit{' str '}'];
endfunction

function outstr = do_monospaced (str)
  outstr = ['\texttt{' str '}'];
endfunction

function outstr = do_TM ()
  outstr = '\texttrademark ';
endfunction

function outstr = do_R ()
  outstr = '\textregistered ';
endfunction

function str = escape_latex (str)
  ## Escape "&", "%", "#", "_", "~", "^", "<", ">"
  ## FIXME: What about: "\", "{", "}"
  str = regexprep (str, '(?<!\\)(&|%|#|_)', '\\$1');
  str = regexprep (str, '(?<!\\)(~)', "\\ensuremath{\\tilde{\;}}");
  str = regexprep (str, '(?<!\\)(\^)', "\\^{}");
  str = regexprep (str, '(?<!\\)(<)', "\\ensuremath{<}");
  str = regexprep (str, '(?<!\\)(>)', "\\ensuremath{>}");
endfunction