Example #1
0
    def __init__(self, name, root=None):
        self.name = name

        self.logs = {}  #maps log-ids (strings) to logs,
        #which now contain lists of cells.
        #(for fast access)

        if root is None:
            self.root = ET.Element(ip.notebook,
                                   version=self.version,
                                   nsmap=nsmap(ip, db))
            self.head = ET.SubElement(self.root, ip.head)
            self.add_log()
        else:
            #reconstruct object structure from given xml
            self.root = root
            version = root.get('version')
            if version != self.version:
                warnings.warn("Wrong version %s != %s" %
                              (version, self.version))
            self.head = root.find(ip.head)
            logelems = root.findall(ip.log)
            for logelem in logelems:
                logid = logelem.get('id')
                self.logs[logid] = Log(self, logid, logelem)

        self.tree = ET.ElementTree(self.root)
        self.xpath = ET.XPathEvaluator(self.tree,
                                       namespaces=nsmap(ip, db, dc, rdf))
Example #2
0
    def contiguify(self, log, sheets=None):
        """Renumber cells in a log to make them contiguous.

        If sheets are provided, renumber the appropriate elements, too.
        """
        numbers = sorted(
            int(x)
            for x in log.xpath('./ip:cell/@number', namespaces=nsmap(ip)))
        old2new = {}
        for i, oldnum in enumerate(numbers):
            newnum = str(i + 1)
            old2new[oldnum] = newnum
            for elem in log.xpath('.//[@number="%s"]' % oldnum):
                elem.set('number', newnum)

        if sheets is not None:
            logid = log.get('id')
            ns = nsmap(ip)
            for sheet in sheets:
                cells = sheet.xpath('.//ip:block[@logid="%s"]//' % logid,
                                    namespaces=ns)
                figs = sheet.xpath('.//ip:figure[@logid="%s"]' % logid,
                                   namespaces=ns)
                elems = cells + figs
                for x in elem:
                    x.set('number', old2new.get(x.get('number')))
Example #3
0
 def vacuum(self, nb):
     """Remove all unreferenced cells from logs.
     """
     sheets = nb.xpath.evaluate('./ip:sheet')
     keepers = {}
     for sheet in sheets:
         # get all unique logids from the relevant elements in the sheet
         logids = set(sheet.xpath('.//@logid'))
         for logid in logids:
             # get the log
             log = nb.get_log(logid)
             # get all ipython-cell elements from ipython-blocks with this logid
             cells = sheet.xpath('.//ip:block[@logid="%s"]/ip:cell' % logid,
                                 namespaces=nsmap(ip))
             # get all ipython-figure elements with this logid
             figs = sheet.xpath('.//ip:figure[@logid="%s"]' % logid)
             logkeepers = keepers.get(logid, set())
             logkeepers.update(
                 (x.get('type'), x.get('number')) for x in cells)
             logkeepers.update(('figure', x.get('number')) for x in figs)
             keepers[logid] = logkeepers
     for logid in logids:
         log = nb.get_log(logid)
         logkeepers = keepers[logid]
         cells = list(log)
         for cell in cells:
             num = cell.get('number')
             for subcell in list(cell):
                 if (subcell.tag, num) not in logkeepers:
                     cell.remove(subcell)
             if len(cell) == 0:
                 log.remove(cell)
Example #4
0
 def prep_latex(cls, tree, name=None):
     listings = tree.xpath('//db:programlisting', namespaces=nsmap(db))
     for listing in listings:
         listing.text = cls.escape_latex(listing.text)
         for sub in listing.getiterator():
             sub.text = cls.escape_latex(sub.text)
             sub.tail = cls.escape_latex(sub.tail)
Example #5
0
 def equation_images(cls, tree, name):
     can_tex = False
     try:
         from matplotlib import texmanager
     except ImportError:
         warnings.warn('matplotlib not available')
     try:
         texmgr = texmanager.TexManager()
         texmgr.get_dvipng_version()
         can_tex = True
     except RuntimeError:
         warnings.warn('dvipng not up-to-date')
     if can_tex == False:
         warnings.warn('Cannot convert equations to images')
         return
     else:
         if name is None:
             raise ValueError("must provide a name")
         eqdir = name + '_files'
         if not os.path.isdir(eqdir):
             os.mkdir(eqdir)
         equations = tree.xpath('//db:informalequation',
                                namespaces=nsmap(db))
         equations.extend(
             tree.xpath('//db:inlineequation', namespaces=nsmap(db)))
         for eq in equations:
             mo = eq.find(db.mediaobject)
             if mo is None:
                 mo = eq.find(db.inlinemediaobject)
             phrase = mo.find("%s/%s" % (db.textobject, phrase))
             latex = phrase.text
             pngfile = texmgr.make_png(latex, dpi=120)
             pngbase = os.path.basename(pngfile)
             newpng = os.path.join(eqdir, pngbase)
             shutil.copyfile(pngfile, newpng)
             io = ET.SubElement(mo, db.imageobject)
             data = ET.SubElement(
                 io,
                 db.imagedata,
                 fileref=relpath(newpng,
                                 os.path.split(os.path.abspath(name))[0]),
                 format='PNG')
Example #6
0
 def __call__(self, nbxml):
     r = nbxml.getroot()
     if r.get('version') != '1':
         raise ValueError('need notebook with version="1"')
     newr = ET.Element(ip.notebook, nsmap=nsmap(ip, db), version='1.1')
     for elem in r:
         newr.append(elem)
     for elem in newr.getiterator():
         newtag = self.tagmap.get(elem.tag, elem.tag)
         elem.tag = newtag
     return ET.ElementTree(newr)
Example #7
0
    def latex_xsl(self):
        sheet = ET.Element(xsl.stylesheet, version="1.0", nsmap=nsmap(xsl))

        # configure this later
        ET.SubElement(sheet, xsl['import'],
                      href=os.path.join(XSLDIR, "latex", "docbook.xsl"))

        # Add template for <code>
        t = ET.SubElement(sheet, xsl.template, match="code")
        ET.SubElement(t, xsl['call-template'], name="inline.monoseq")

        # turn on fancyvrb
        fancyvrb = ET.SubElement(sheet, xsl.param, name="latex.use.fancyvrb")
        fancyvrb.text = "1"
        fvopt = ET.SubElement(sheet, xsl.template, name="latex.fancyvrb.options")
        fvopt.text = r",commandchars=\\\{\}"

        # turn off fancyhdr
        fancyhdr = ET.SubElement(sheet, xsl.param, name='latex.use.fancyhdr')
        fancyhdr.text = '0'

        docclass = ET.SubElement(sheet, xsl.param,
            name="latex.documentclass.article")
        docclass.text = "letterpaper,10pt,twoside"

        amble = ET.SubElement(sheet, xsl.param,
            name="latex.article.preamble.post")
        amble.text = '\n'.join(x.as_latex() for x in self.styles_dict.itervalues())
        amble.text = '\n%s\n' % amble.text

        # and again for book
        amble = ET.SubElement(sheet, xsl.param,
            name="latex.book.preamble.post")
        amble.text = '\n'.join(x.as_latex() for x in self.styles_dict.itervalues())
        amble.text = '\n%s\n' % amble.text

        # change to English and UTF-8
        lang = ET.SubElement(sheet, xsl.variable, name="l10n.gentext.default.language")
        lang.text = "en"
        ET.SubElement(sheet, xsl.variable, name="latex.documentclass.common")
        ET.SubElement(sheet, xsl.variable, name="latex.babel.language")
        encoding = ET.SubElement(sheet, xsl.output, method="text",
            encoding="UTF-8", indent="yes")

        # add the templates for syntax-highlighting
        for textstyle in self.styles_dict.itervalues():
            template = ET.SubElement(sheet, xsl.template,
                match='phrase[@role="%s"]' % textstyle.name,
                mode="latex.verbatim")
            template.text = '\\%s{' % textstyle.cmd_name()
            value = ET.SubElement(template, xsl["value-of"], select="./text()")
            value.tail = '}'

        return ET.ElementTree(sheet)
Example #8
0
    def html_xsl(self):
        sheet = ET.Element(xsl.stylesheet, version="1.0", nsmap=nsmap(xsl))

        ET.SubElement(sheet, xsl['import'],
            href=os.path.join(XSLDIR, "xhtml", "docbook.xsl"))

        css = ET.SubElement(sheet, xsl.template, name="user.head.content")
        css_style = ET.SubElement(css, "style", type="text/css")
        comment = ET.SubElement(css_style, xsl.comment)
        comment.text = self.html_css()
        ET.SubElement(sheet, xsl.param, name="section.autolabel", select="1")
        ET.SubElement(sheet, xsl.param, 
            name="section.label.includes.component.label", select="1")

        return ET.ElementTree(sheet)
Example #9
0
 def get_title(self):
     if self.sheet is not None:
         #print "sheet in get_title:", self.sheet, ET.tostring(self.sheet)
         try:
             title = self.sheet.xpath(
                 './db:title',
                 namespaces=nsmap(db))[0]  #single title per file
             #print ".. title:", title.text
             return title.text
         except IndexError:
             #print "no title"
             return None
     else:
         #print "no sheet in:", self, ET.tostring(self.root)
         return None
Example #10
0
 def rerun_since(self, log, number):
     """Rerun all inputs >= number and append them to the log.
     """
     self.add_hooks()
     cells = sorted(
         (Cell(x) for x in oldlog.xpath('./ip:cell[@number>=%s]' % number,
                                        namespaces=nsmap(ip))),
         key=lambda x: x.number)
     curnum = cells[-1].number
     for cell in cells:
         curnum += 1
         newcell = ET.SubElement(log, ip.cell, number=str(curnum))
         input = ET.SubElement(newcell, ip.input)
         input.text = cell.input
         retdict = self.run_one(input.text)
         if retdict is not None:
             for tag, value in retdict.iteritems():
                 elem = ET.SubElement(newcell, ip[tag])
                 elem.text = value
     self.rm_hooks()
Example #11
0
def book2docbook(bookfilename):
    from notabene.docbook import DBFormatter
    book = ET.parse(bookfilename)
    basedir, basename = os.path.split(bookfilename)
    child2parent = dict((c, p) for p in book.getiterator() for c in p)
    for link in book.xpath('//*[@xlink:type="simple"]',
                           namespaces=nsmap(xlink)):
        parent = child2parent[link]
        path, frag = spliturl(basedir, link.get(xlink.href))
        nb = Notebook.from_file(path)
        sheet = nb.xpath.evaluate('/notebook/sheet[@id="%s"]' % frag)
        if sheet == []:
            raise ValueError('could not find <sheet id="%s"> in %s' %
                             (frag, path))
        else:
            sheet = sheet[0]
        dbf = DBFormatter(nb)
        dbxml = dbf.transform_sheet(sheet, nodetype=link.tag)
        idx = parent.index(link)
        del parent[idx]
        parent.insert(idx, dbxml)
    return book
Example #12
0
 def transform_elements(elemtype, func):
     elems = sheet2.xpath('.//ip:%s' % elemtype, namespaces=nsmap(ip))
     for elem in elems:
         parent, idx = parent_and_index(elem)
         transformed = func(elem)
         parent[idx] = transformed
Example #13
0
 def fo_xsl(self):
     sheet = ET.Element(xsl.stylesheet, version="1.0", nsmap=nsmap(xsl))
     ET.SubElement(sheet, xsl['import'],
         href=os.path.join(XSLDIR, "fo", "docbook.xsl"))
     return ET.ElementTree(sheet)
Example #14
0
 def get_contents(self):
     if self.sheet is not None:
         #print "sheet in get_contents:", self.sheet, ET.tostring(self.sheet)
         sections = self.sheet.xpath('./db:section', namespaces=nsmap(db))
         #print "sections:", sections
         return sections