📄 __init__.py
字号:
# Stack of section counters so that we don't have to use_latex_toc. # This will grow and shrink as processing occurs. # Initialized for potential first-level sections. self._section_number = [0] # The current stack of enumerations so that we can expand # them into a compound enumeration self._enumeration_counters = [] self._bibitems = [] # docinfo. self.docinfo = None # inside literal block: no quote mangling. self.literal_block = 0 self.literal_block_stack = [] self.literal = 0 # true when encoding in math mode self.mathmode = 0 def to_latex_encoding(self,docutils_encoding): """ Translate docutils encoding name into latex's. Default fallback method is remove "-" and "_" chars from docutils_encoding. """ tr = { "iso-8859-1": "latin1", # west european "iso-8859-2": "latin2", # east european "iso-8859-3": "latin3", # esperanto, maltese "iso-8859-4": "latin4", # north european,scandinavian, baltic "iso-8859-5": "iso88595", # cyrillic (ISO) "iso-8859-9": "latin5", # turkish "iso-8859-15": "latin9", # latin9, update to latin1. "mac_cyrillic": "maccyr", # cyrillic (on Mac) "windows-1251": "cp1251", # cyrillic (on Windows) "koi8-r": "koi8-r", # cyrillic (Russian) "koi8-u": "koi8-u", # cyrillic (Ukrainian) "windows-1250": "cp1250", # "windows-1252": "cp1252", # "us-ascii": "ascii", # ASCII (US) # unmatched encodings #"": "applemac", #"": "ansinew", # windows 3.1 ansi #"": "ascii", # ASCII encoding for the range 32--127. #"": "cp437", # dos latine us #"": "cp850", # dos latin 1 #"": "cp852", # dos latin 2 #"": "decmulti", #"": "latin10", #"iso-8859-6": "" # arabic #"iso-8859-7": "" # greek #"iso-8859-8": "" # hebrew #"iso-8859-10": "" # latin6, more complete iso-8859-4 } if tr.has_key(docutils_encoding.lower()): return tr[docutils_encoding.lower()] return docutils_encoding.translate(string.maketrans("",""),"_-").lower() def language_label(self, docutil_label): return self.language.labels[docutil_label] latex_equivalents = { u'\u00A0' : '~', u'\u2013' : '{--}', u'\u2014' : '{---}', u'\u2018' : '`', u'\u2019' : '\'', u'\u201A' : ',', u'\u201C' : '``', u'\u201D' : '\'\'', u'\u201E' : ',,', u'\u2020' : '{\\dag}', u'\u2021' : '{\\ddag}', u'\u2026' : '{\\dots}', u'\u2122' : '{\\texttrademark}', u'\u21d4' : '{$\\Leftrightarrow$}', } def unicode_to_latex(self,text): # see LaTeX codec # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252124 # Only some special chracters are translated, for documents with many # utf-8 chars one should use the LaTeX unicode package. for uchar in self.latex_equivalents.keys(): text = text.replace(uchar,self.latex_equivalents[uchar]) return text def encode(self, text): """ Encode special characters (``# $ % & ~ _ ^ \ { }``) in `text` & return """ # Escaping with a backslash does not help with backslashes, ~ and ^. # < > are only available in math-mode or tt font. (really ?) # $ starts math- mode. # AND quotes if self.verbatim: return text # compile the regexps once. do it here so one can see them. # # first the braces. if not self.__dict__.has_key('encode_re_braces'): self.encode_re_braces = re.compile(r'([{}])') text = self.encode_re_braces.sub(r'{\\\1}',text) if not self.__dict__.has_key('encode_re_bslash'): # find backslash: except in the form '{\{}' or '{\}}'. self.encode_re_bslash = re.compile(r'(?<!{)(\\)(?![{}]})') # then the backslash: except in the form from line above: # either '{\{}' or '{\}}'. text = self.encode_re_bslash.sub(r'{\\textbackslash}', text) # then dollar text = text.replace("$", '{\\$}') if not ( self.literal_block or self.literal or self.mathmode ): # the vertical bar: in mathmode |,\vert or \mid # in textmode \textbar text = text.replace("|", '{\\textbar}') text = text.replace("<", '{\\textless}') text = text.replace(">", '{\\textgreater}') # then text = text.replace("&", '{\\&}') # the ^: # * verb|^| does not work in mbox. # * mathmode has wedge. hat{~} would also work. # text = text.replace("^", '{\\ensuremath{^\\wedge}}') text = text.replace("^", '{\\textasciicircum}') text = text.replace("%", '{\\%}') text = text.replace("#", '{\\#}') text = text.replace("~", '{\\textasciitilde}') # Separate compound characters, e.g. "--" to "-{}-". (The # actual separation is done later; see below.) separate_chars = '-' if self.literal_block or self.literal: # In monospace-font, we also separate ",,", "``" and "''" # and some other characters which can't occur in # non-literal text. separate_chars += ',`\'"<>' # pdflatex does not produce doublequotes for ngerman. text = self.babel.double_quotes_in_tt(text) if self.font_encoding == 'OT1': # We're using OT1 font-encoding and have to replace # underscore by underlined blank, because this has # correct width. text = text.replace('_', '{\\underline{ }}') # And the tt-backslash doesn't work in OT1, so we use # a mirrored slash. text = text.replace('\\textbackslash', '\\reflectbox{/}') else: text = text.replace('_', '{\\_}') else: text = self.babel.quote_quotes(text) text = text.replace("_", '{\\_}') for char in separate_chars * 2: # Do it twice ("* 2") becaues otherwise we would replace # "---" by "-{}--". text = text.replace(char + char, char + '{}' + char) if self.insert_newline or self.literal_block: # Insert a blank before the newline, to avoid # ! LaTeX Error: There's no line here to end. text = text.replace("\n", '~\\\\\n') elif self.mbox_newline: if self.literal_block: closings = "}" * len(self.literal_block_stack) openings = "".join(self.literal_block_stack) else: closings = "" openings = "" text = text.replace("\n", "%s}\\\\\n\\mbox{%s" % (closings,openings)) text = text.replace('[', '{[}').replace(']', '{]}') if self.insert_none_breaking_blanks: text = text.replace(' ', '~') if self.latex_encoding != 'utf8': text = self.unicode_to_latex(text) return text def attval(self, text, whitespace=re.compile('[\n\r\t\v\f]')): """Cleanse, encode, and return attribute value text.""" return self.encode(whitespace.sub(' ', text)) def astext(self): if self.pdfinfo is not None: if self.pdfauthor: self.pdfinfo.append('pdfauthor={%s}' % self.pdfauthor) if self.pdfinfo: pdfinfo = '\\hypersetup{\n' + ',\n'.join(self.pdfinfo) + '\n}\n' else: pdfinfo = '' head = '\\title{%s}\n\\author{%s}\n\\date{%s}\n' % \ (self.title, ' \\and\n'.join(['~\\\\\n'.join(author_lines) for author_lines in self.author_stack]), self.date) return ''.join(self.head_prefix + [head] + self.head + [pdfinfo] + self.body_prefix + self.body + self.body_suffix) def visit_Text(self, node): self.body.append(self.encode(node.astext())) def depart_Text(self, node): pass def visit_address(self, node): self.visit_docinfo_item(node, 'address') def depart_address(self, node): self.depart_docinfo_item(node) def visit_admonition(self, node, name=''): self.body.append('\\begin{center}\\begin{sffamily}\n') self.body.append('\\fbox{\\parbox{\\admonitionwidth}{\n') if name: self.body.append('\\textbf{\\large '+ self.language.labels[name] + '}\n'); self.body.append('\\vspace{2mm}\n') def depart_admonition(self, node=None): self.body.append('}}\n') # end parbox fbox self.body.append('\\end{sffamily}\n\\end{center}\n'); def visit_attention(self, node): self.visit_admonition(node, 'attention') def depart_attention(self, node): self.depart_admonition() def visit_author(self, node): self.visit_docinfo_item(node, 'author') def depart_author(self, node): self.depart_docinfo_item(node) def visit_authors(self, node): # not used: visit_author is called anyway for each author. pass def depart_authors(self, node): pass def visit_block_quote(self, node): self.body.append( '\\begin{quote}\n') def depart_block_quote(self, node): self.body.append( '\\end{quote}\n') def visit_bullet_list(self, node): if 'contents' in self.topic_classes: if not self.use_latex_toc: self.body.append( '\\begin{list}{}{}\n' ) else: self.body.append( '\\begin{itemize}\n' ) def depart_bullet_list(self, node): if 'contents' in self.topic_classes: if not self.use_latex_toc: self.body.append( '\\end{list}\n' ) else: self.body.append( '\\end{itemize}\n' ) # Imperfect superscript/subscript handling: mathmode italicizes # all letters by default. def visit_superscript(self, node): self.body.append('$^{') self.mathmode = 1 def depart_superscript(self, node): self.body.append('}$') self.mathmode = 0 def visit_subscript(self, node): self.body.append('$_{') self.mathmode = 1 def depart_subscript(self, node): self.body.append('}$') self.mathmode = 0 def visit_caption(self, node): self.body.append( '\\caption{' ) def depart_caption(self, node): self.body.append('}') def visit_caution(self, node): self.visit_admonition(node, 'caution') def depart_caution(self, node): self.depart_admonition() def visit_title_reference(self, node): self.body.append( '\\titlereference{' ) def depart_title_reference(self, node): self.body.append( '}' ) def visit_citation(self, node): # TODO maybe use cite bibitems if self._use_latex_citations: self.context.append(len(self.body)) else: self.body.append('\\begin{figure}[b]') for id in node['ids']: self.body.append('\\hypertarget{%s}' % id) def depart_citation(self, node): if self._use_latex_citations: size = self.context.pop() label = self.body[size] text = ''.join(self.body[size+1:]) del self.body[size:] self._bibitems.append([label, text]) else: self.body.append('\\end{figure}\n') def visit_citation_reference(self, node): if self._use_latex_citations: self.body.append('\\cite{') else: href = '' if node.has_key('refid'): href = node['refid'] elif node.has_key('refname'): href = self.document.nameids[node['refname']] self.body.append('[\\hyperlink{%s}{' % href) def depart_citation_reference(self, node): if self._use_latex_citations: self.body.append('}') else: self.body.append('}]') def visit_classifier(self, node): self.body.append( '(\\textbf{' ) def depart_classifier(self, node): self.body.append( '})\n' ) def visit_colspec(self, node): self.active_table.visit_colspec(node) def depart_colspec(self, node): pass def visit_comment(self, node): # Escape end of line by a new comment start in comment text. self.body.append('%% %s \n' % node.astext().replace('\n', '\n% ')) raise nodes.SkipNode
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -