def __init__(self, element): if type(element) in (str, unicode): tree = ET.fromstring('<html_example>' + element + "</html_example>") self.el = tree else: self.el = element from ist.utils.texlist2 import TexList self.tex = TexList()
def href_id(self): if self.parent: parent_id = str(self.parent.href_id) if FormatMode.format_mode == FormatMode.LATEX_MODE: if parent_id.startswith('IT::'): return '{}::{}'.format(parent_id[4:], TexList.name_mode(self.name)) return '{}::{}'.format(parent_id, TexList.name_mode(self.name)) return '{self.parent.href_id}-{self.name}'.format(self=self) return self.name
def href_id(self): if self.parent: parent_id = str(self.parent.href_id) if FormatMode.format_mode == FormatMode.LATEX_MODE: if parent_id.startswith('IT::'): return '{}::{}'.format(parent_id[4:], TexList.name_mode(self.name)) return '{}::{}'.format(parent_id, TexList.name_mode(self.name)) return '{self.parent.href_id}:{self.name}'.format(self=self) return self.name
def format(items): tex = TexList() #Logger.instance().info('Processing items...') for item in items: # do no format certain objects #Logger.instance().info('processing: %s (%s)' % (item, item.href_id)) if not item.include_in_format(): #Logger.instance().info(' - skipped') continue #Logger.instance().info(' +++ formatting +++') fmt = LatexFormatter.get_formatter_for(item) if fmt is not None: fmt.format(item) tex.extend(fmt) return tex
def parse2latex(self, md_text): from ist.utils.texlist2 import TexList reduce_tag = 'div' secured_markdown = self._replace_math(md_text) latex_secured = TexList.prepare_plain(secured_markdown) # apply markdown html_secured = markdown.markdown(latex_secured, extensions=[ 'markdown.extensions.sane_lists', 'markdown.extensions.nl2br', 'ist.formatters.extensions.md_links', 'ist.formatters.extensions.md_strike']) html_secured = TexList.finish_plain(html_secured) html = self._md_latex.finish(html_secured) try: return ET.fromstring(html) except Exception as e: return ET.fromstring('<' + reduce_tag + '>' + html + '</' + reduce_tag + '>')
def format(items): tex = TexList() Logger.instance().info('Processing items...') for item in items: # do no format certain objects if not item.include_in_format(): Logger.instance().info(' - item skipped: %s' % str(item)) continue Logger.instance().info(' - formatting item: %s' % str(item)) # l = LatexRecord() # l.format(item) # print l # exit() fmt = LatexFormatter.get_formatter_for(item) if fmt is not None: fmt.format(item) tex.extend(fmt) return tex
def format(items): tex = texlist() Logger.instance().info('Processing items...') for item in items: # do no format certain objects if not item.include_in_format(): Logger.instance().info(' - item skipped: %s' % str(item)) continue Logger.instance().info(' - formatting item: %s' % str(item)) a = TexList() a.macro_record_type(item) print a exit() fmt = LatexFormatter.get_formatter_for(item) if fmt is not None: tex.extend(fmt.format(item)) tex.newline() tex.newline() return tex
def href_id(self): """ Method will return unique id of this item if link_name was specified in attributes, it will be used :return: """ if getattr(self, 'attributes', None) and self.attributes.link_name: value = self.attributes.link_name else: value = self.get('unique_name', 'name', 'id') if FormatMode.format_mode == FormatMode.LATEX_MODE: return 'IT::{}'.format(TexList.name_mode(value)) return htmltree.secure(value)
class Html2Latex(object): """ Class which based on given element (html/string) can produce latex format """ list_types = { 'ul': 'itemize', 'ol': 'enumerate' } def __init__(self, element): if type(element) in (str, unicode): tree = ET.fromstring('<html_example>' + element + "</html_example>") self.el = tree else: self.el = element from ist.utils.texlist2 import TexList self.tex = TexList() def extend_children(self): """ Recursive children call """ for child in self.el: self.tex.extend(Html2Latex(child).to_latex()) def tag_is(self, *tags): """ whether current tag is in given tags """ return self.el.tag in tags def text(self): """ return current text """ return self.el.text if self.el.text else '' def tail(self): """ return current tail """ return self.el.tail if self.el.tail else '' def add_text(self): if str(self.text).strip(): with self.tex: self.tex.append(self.text()) def add_tail(self): """ Adds current tail if exists """ if self.tail(): if self.tail() == '\n': self.tex.append(self.tail()) else: with self.tex: self.tex.append(self.tail()) def get_list_type(self): """ helper method for getting list type""" return self.list_types.get(self.el.tag, 'itemize') # # def tag (self): # return self.el.tag def to_latex(self): """ Method converts this object to latex with recursive calls """ if self.tag_is('p'): with self.tex: with self.tex: self.tex.append(self.text()) self.extend_children() self.add_tail() # self.tex.newline () elif self.tag_is('br'): self.tex.append(self.text()) self.tex.append(r'\\') self.extend_children() self.add_tail() elif self.tag_is('h1'): self.tex.append('\\section') with self.tex: self.tex.append(self.text()) self.extend_children() self.add_tail() elif self.tag_is('a'): self.tex.extend(LatexHref(self.el).to_latex()) elif self.tag_is('em'): self.tex.append('\\textit') with self.tex: self.tex.append(self.text()) self.extend_children() self.add_tail() elif self.tag_is('strong'): self.tex.append('\\textbf') with self.tex: self.tex.append(self.text()) self.extend_children() self.add_tail() elif self.tag_is('ul', 'ol'): self.tex.begin(self.get_list_type()) self.tex.append(self.text()) self.extend_children() self.tex.end(self.get_list_type()) self.add_tail() elif self.tag_is('li'): # with self.tex: self.tex.append('\\item ') self.add_text() self.extend_children() self.add_tail() # so far, code tag will be monospaced only elif self.tag_is('code'): # self.tex.open_element ('lstlisting') # self.tex.newline () self.tex.slash('ttfamily ') self.tex.append(self.text().replace('\$', '\$')) # self.extend_children () # self.tex.newline () # self.tex.close_element ('lstlisting') # self.add_tail () elif self.tag_is('span'): self.tex.append(self.text()) self.extend_children() self.add_tail() else: with self.tex: self.tex.append(self.text()) self.extend_children() self.add_tail() return self.tex
class Html2Latex(object): """ Class Html2Latex which based on given element (html/string) can produce latex format """ list_types = { 'ul': 'itemize', 'ol': 'enumerate' } def __init__(self, element): if type(element) in (str, unicode): tree = ET.fromstring('<html_example>' + element + "</html_example>") self.el = tree else: self.el = element from ist.utils.texlist2 import TexList self.tex = TexList() def extend_children(self): """ Recursive children call """ for child in self.el: self.tex.extend(Html2Latex(child).to_latex()) def tag_is(self, *tags): """ whether current tag is in given tags """ return self.el.tag in tags def text(self): """ return current text """ return self.el.text if self.el.text else '' def tail(self): """ return current tail """ return self.el.tail if self.el.tail else '' def add_text(self): if str(self.text).strip(): with self.tex: self.tex.append(self.text()) def add_tail(self): """ Adds current tail if exists """ if self.tail(): if self.tail() == '\n': self.tex.append(self.tail()) else: with self.tex: self.tex.append(self.tail()) def get_list_type(self): """ helper method for getting list type""" return self.list_types.get(self.el.tag, 'itemize') # # def tag (self): # return self.el.tag def to_latex(self): """ Method converts this object to latex with recursive calls """ if self.tag_is('p'): with self.tex: with self.tex: self.tex.append(self.text()) self.extend_children() self.add_tail() # self.tex.newline () elif self.tag_is('br'): self.tex.append(self.text()) self.tex.append(r'\\') self.extend_children() self.add_tail() elif self.tag_is('h1'): self.tex.append('\\section') with self.tex: self.tex.append(self.text()) self.extend_children() self.add_tail() elif self.tag_is('a'): self.tex.extend(LatexHref(self.el).to_latex()) elif self.tag_is('em'): self.tex.append('\\textit') with self.tex: self.tex.append(self.text()) self.extend_children() self.add_tail() elif self.tag_is('strong'): self.tex.append('\\textbf') with self.tex: self.tex.append(self.text()) self.extend_children() self.add_tail() elif self.tag_is('ul', 'ol'): self.tex.begin(self.get_list_type()) self.tex.append(self.text().strip()) self.extend_children() self.tex.end(self.get_list_type()) self.add_tail() elif self.tag_is('li'): # with self.tex: self.tex.append('\\item ') self.add_text() self.extend_children() self.add_tail() # so far, code tag will be monospaced only elif self.tag_is('code'): # self.tex.open_element ('lstlisting') # self.tex.newline () self.tex.slash('ttfamily ') self.tex.append(self.text().replace('\$', '\$')) # self.extend_children () # self.tex.newline () # self.tex.close_element ('lstlisting') # self.add_tail () elif self.tag_is('span'): self.tex.append(self.text()) self.extend_children() self.add_tail() else: with self.tex: self.tex.append(self.text()) self.extend_children() self.add_tail() return self.tex