#coding: utf-8
# PDF (LaTeX) - Peter Hopcroft, February 2014
import clipboard
import console
import editor
import markdown
import re
import os
import workflow
# constants -------------------------------------
# start of a LaTeX document, defines the page design- \\ is an escaped \
LSTART = """
\\documentclass[a4paper,12pt,openany,oneside]{memoir}
\\usepackage[T1]{fontenc}
\\usepackage[utf8]{inputenc}
\\usepackage{utopia}
%\\usepackage{palatino} % this works
%\\raggedright
\\usepackage{graphicx}
\\usepackage{enumitem}
\\usepackage{tabulary}
%\\usepackage{microtype} % does not help with ligature bug
%\\DisableLigatures[f]{encoding=T1, family=*}
% my global lengths
\\newlength{\\myparskip}
\\setlength{\\myparskip}{1.0ex plus 0.2ex minus 0.2ex}
\\newlength{\\myindent}
\\setlength{\\myindent}{3ex}
\\newlength{\\myskipbig} % for headings
\\setlength{\\myskipbig}{5ex plus 1.0ex minus 1.0ex}
\\newlength{\\myskipsmall} % for headings
\\setlength{\\myskipsmall}{1.5ex plus 0.3ex minus 0.3ex}
% page layout
\\pagestyle{plain}
\\setlrmarginsandblock{35mm}{35mm}{*}
\\setulmarginsandblock{25mm}{25mm}{*}
\\setlength{\\footskip}{8mm}
\\checkandfixthelayout
% paragraphs
\\setlength{\\parindent}{0pt}
\\abnormalparskip{\\myparskip}
% the headings
\\setsecnumdepth{chapter} % number chapters but not lesser headings
% h1 - same as chapter heading but no number
\\newcommand{\\mytitle}[1]%
{{\\newpage\\vspace*{-1.2\\topskip}%
\\Large\\mdseries\\raggedright{#1}%
\\vspace{\\myskipbig}}} % is a group
% h2 - chapter heading
\\chapterstyle{section}
\\renewcommand*{\\chapterheadstart}{\\vspace*{-1.4\\topskip}} % space before
\\renewcommand*{\\chapnumfont}{\\Large\\mdseries\\raggedright}
\\renewcommand*{\\afterchapternum}{\\hspace{1.0ex}} % space after number
\\renewcommand*{\\chaptitlefont}{\\Large\\mdseries\\raggedright}
\\setlength{\\afterchapskip}{\\myskipbig} % space after
% h3 - section heading
\\setbeforesecskip{-\\myskipbig} % - for no hdr indent
\\setsecheadstyle{\\large\\mdseries\\raggedright}
\\setaftersecskip{\\myskipsmall}
% h4 - subsection heading
\\setbeforesubsecskip{-\\myskipbig} % - for no hdr indent
\\setsubsecheadstyle{\\itshape\\raggedright}
\\setaftersubsecskip{\\myskipsmall}
% h5 & h6 subsubsection heading - runin
\\setbeforesubsubsecskip{-\\myskipbig} % - for no hdr indent
\\setsubsubsecheadstyle{\\itshape\\raggedright}
\\setaftersubsubsecskip{-1.4ex} % for runin heading
% lists, using enumitem
\\newenvironment{myul}% % unordered list
{\\begin{itemize} [topsep=0pt, partopsep=0pt, itemsep=0pt, %
parsep=\\parskip, leftmargin=\\myindent, labelindent=1.0pt, %
labelsep=*, label={\\rule[0.2ex]{0.7ex}{0.7ex}}]}%
{\\end{itemize} \\vspace{0ex} }
\\newenvironment{myol}% % ordered list
{\\begin{enumerate} [topsep=0pt, partopsep=0pt, itemsep=0pt, %
parsep=\\parskip, leftmargin=\\myindent, labelindent=0.5pt, %
labelsep=*, label=\\arabic*]}%
{\\end{enumerate} \\vspace{0ex} }
% miscellaneous
\\newenvironment{myblockquote}%
{\\leftskip=\\myindent}%
{}
% verbatim: zero the space above and below trivlists, for verbatim
% caution: affects all trivlist environments (eg centering) beneficially
\\zerotrivseps
\\verbatimindent=0ex % for memoir verbatim
\\newenvironment{myverbatim}%
{\\verbatim\\leftskip=\\myindent}%
{\\endverbatim}
\\pfbreakskip=\\myskipbig % hr -> \pfbreak; set the gap
%\\newcommand{\\myhr}% old hr
% {{\\centering\\rule[0.4ex]{10ex}{0.3pt}\\par}} % inner set {} is group
\\begin{document}
"""
# end of a LaTeX document
LEND = """
\\end{document}
"""
# dictionaries -------------------------------------
# html -> latex equivalents - \\ is an escaped \
D = {'<p>':'\n',
'</p>': '',
'<h1>':'\n\\mytitle{',
'</h1>':'}',
'<h2>':'\n\\chapter{',
'</h2>':'}',
'<h3>':'\n\\section{',
'</h3>':'}',
'<h4>':'\n\\subsection{',
'</h4>':'}',
'<h5>':'\n\\subsubsection{',
'</h5>':'}',
'<h6>':'\n\\subsubsection{',
'</h6>':'}',
'<blockquote>':'\n\\begin{myblockquote}',
'</blockquote>':'\n\\end{myblockquote}',
'<ul>':'\n\\begin{myul}',
'</ul>':'\\end{myul}',
'<ol>':'\n\\begin{myol}',
'</ol>': '\\end{myol}',
'<li>':'\\item ',
'</li>':'',
'<pre><code>':'\n\\begin{myverbatim}',
'</code></pre>':'\\end{myverbatim}',
'<hr />':'\n\\pfbreak',
'<em>':'\\emph{',
'</em>':'}',
'<strong>':'\\textbf{',
'</strong>':'}',
'<code>':'\\verb|',
'</code>':'|',
}
# html entities -> LaTeX characters or commands
# texpad has a bug with local typesetting: some ligatures, eg 'ff',
# can typeset wrongly as 'f '; the lines below with \kern insert a
# zero-size gap between two characters so they typeset properly
E = {'&':'&',
'<':'<',
'ff':'f\kern0pt{}f'
}
# functions -------------------------------------
# convert one html img element into a latex img element
def latex_img (html_img):
# extract img file name
mo = re.search('src="(.*?)"', html_img) # src="anychars"
if (not mo) or (mo.group(1) == ''):
f = 'No image file name found' # default
else:
f = mo.group(1)
# extract img width
mo = re.search('alt="(.*?)"', html_img) # alt="anychars"
if (not mo) or (mo.group(1) == ''):
w = '\\textwidth' # default
else:
w = mo.group(1)
li = '\\includegraphics[width=' + w + ']{' + f + '}'
# extract title
mo = re.search('title="(.*?)"', html_img) # get title="anychars"
if (mo) and (mo.group(1) != ''): # put in float
li = '\n\\begin{figure}[!hbtp]\n' + li + '\n\\par{}' + mo.group(1) + '\n\\end{figure}\n'
return li
# convert all html img elements to latex img elements
def do_imgs (txt):
new_txt = ''
els = txt.split('<img') # find img elements, start with <img
for el in els:
if '/>' in el:
i_r = el.split('/>', 1) # split into img element and the rest
new_txt += latex_img(i_r[0]) + i_r[1]
else:
new_txt += el # no img element
return new_txt
# main -------------------------------------
console.clear()
txt = editor.get_text()
if '\\' in txt:
console.alert('Warning:\nThe markdown text\nhas the character:\n\\',
'These can only be in LaTeX commands, not in ordinary text',
'Continue')
if '&' in txt:
console.alert('Warning:\nThe markdown text\nhas the character:\n&',
'These can only be in LaTeX lists, not in ordinary text',
'Continue')
txt = markdown.markdown (txt) # convert to html, no extensions
for k, v in D.iteritems(): # convert most html to LaTeX
txt = txt.replace(k, v)
txt = do_imgs(txt) # convert html imgs to latex imgs
if '<a' in txt and '</a>' in txt: # check for unsupported html
console.alert('Warning:\nThere is a markdown link',
'This workflow does not suport links in LaTeX',
'Continue')
elif '<' in txt or '>' in txt:
console.alert('Warning:\nThe LaTeX file has < or > brackets',
"This could be HTML that hasn't been converted to laTeX",
'Continue')
for k, v in E.iteritems(): # convert minor things to LaTeX
txt = txt.replace(k, v)
txt = LSTART + txt + LEND # add preamble and postamble
clipboard.set(txt.encode('utf_8', 'replace'))
console.alert('The LaTeX is on the clipboard', 'Switch to Texpad, paste into an empty .tex file, tap typeset, then tap the eye button to see the PDF', 'OK')
# -------------------------------------
There are no comments yet.