Changeset 854
- Timestamp:
- 09/18/05 06:58:08 (3 years ago)
- Files:
-
- nbdoc/trunk/notabene/docbook.py (modified) (7 diffs)
- nbdoc/trunk/notabene/notebook.py (modified) (4 diffs)
- nbdoc/trunk/notabene/styles.py (modified) (3 diffs)
- nbdoc/trunk/scipy_tutorial/chapter02.pybk (modified) (1 diff)
- nbdoc/trunk/scipy_tutorial/clean.py (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
nbdoc/trunk/notabene/docbook.py
r853 r854 8 8 import tempfile 9 9 import subprocess 10 import shutil 11 import warnings 10 12 11 13 from lxml import etree as ET … … 13 15 from notabene.formatter import Formatter 14 16 from PyFontify import fontify 17 18 def relpath(target, base=os.curdir): 19 """ 20 Return a relative path to the target from either the current dir or an optional base dir. 21 Base can be a directory specified either as absolute or relative to current dir. 22 23 This function is by Richard Barran. 24 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302594 25 """ 26 27 if not os.path.exists(target): 28 raise OSError, 'Target does not exist: '+target 29 30 if not os.path.isdir(base): 31 raise OSError, 'Base is not a directory or does not exist: '+base 32 33 base_list = (os.path.abspath(base)).split(os.sep) 34 target_list = (os.path.abspath(target)).split(os.sep) 35 36 # On the windows platform the target may be on a completely different drive from the base. 37 if os.name in ['nt','dos','os2'] and base_list[0] <> target_list[0]: 38 raise OSError, 'Target is on a different drive to base. Target: '+target_list[0].upper()+', base: '+base_list[0].upper() 39 40 # Starting from the filepath root, work out how much of the filepath is 41 # shared by base and target. 42 for i in range(min(len(base_list), len(target_list))): 43 if base_list[i] <> target_list[i]: break 44 else: 45 # If we broke out of the loop, i is pointing to the first differing path elements. 46 # If we didn't break out of the loop, i is pointing to identical path elements. 47 # Increment i so that in all cases it points to the first differing path elements. 48 i+=1 49 50 rel_list = [os.pardir] * (len(base_list)-i) + target_list[i:] 51 return os.path.join(*rel_list) 52 53 54 15 55 16 56 def dbify(text): … … 120 160 verbatim = equ.get('verb', None) 121 161 if verbatim != '1': 122 tex = r' \begin{equation}%s\end{equation}' % tex162 tex = r'$$%s$$' % tex 123 163 mo = ET.SubElement(dbeq, 'mediaobject') 124 164 to = ET.SubElement(mo, 'textobject') … … 219 259 220 260 @classmethod 221 def prep_fo(cls, tree): 261 def equation_images(cls, tree, name): 262 can_tex = False 263 try: 264 from matplotlib import texmanager 265 except ImportError: 266 warnings.warn('matplotlib not available') 267 try: 268 texmgr = texmanager.TexManager() 269 texmgr.get_dvipng_version() 270 can_tex = True 271 except RuntimeError: 272 warnings.warn('dvipng not up-to-date') 273 if can_tex == False: 274 warnings.warn('Cannot convert equations to images') 275 return 276 else: 277 if name is None: 278 raise ValueError("must provide a name") 279 eqdir = name + '_files' 280 if not os.path.isdir(eqdir): 281 os.mkdir(eqdir) 282 equations = tree.xpath('//informalequation') 283 equations.extend(tree.xpath('//inlineequation')) 284 for eq in equations: 285 mo = eq.find('mediaobject') 286 if mo is None: 287 mo = eq.find('inlinemediaobject') 288 phrase = mo.find('textobject/phrase') 289 latex = phrase.text 290 pngfile = texmgr.make_png(latex, dpi=120) 291 pngbase = os.path.basename(pngfile) 292 newpng = os.path.join(eqdir, pngbase) 293 shutil.copyfile(pngfile, newpng) 294 io = ET.SubElement(mo, 'imageobject') 295 data = ET.SubElement(io, 'imagedata', 296 fileref=relpath(newpng, os.path.split(os.path.abspath(name))[0]), 297 format='PNG') 298 299 @classmethod 300 def prep_fo(cls, tree, name=None): 222 301 pass 223 302 224 303 @classmethod 225 def prep_html(cls, tree ):226 pass304 def prep_html(cls, tree, name=None): 305 cls.equation_images(tree, name) 227 306 228 307 @classmethod 229 def prep_latex(cls, tree ):308 def prep_latex(cls, tree, name=None): 230 309 listings = tree.xpath('//programlisting') 231 310 for listing in listings: … … 236 315 237 316 @classmethod 238 def to_formatted(cls, dbxml, kind='html', style=None):317 def to_formatted(cls, dbxml, kind='html', name=None, style=None): 239 318 """Convert a DocBook document to the final format. 240 319 … … 247 326 if ET.iselement(dbxml): 248 327 dbxml = ET.ElementTree(dbxml) 249 getattr(cls, 'prep_%s' % kind)(dbxml )328 getattr(cls, 'prep_%s' % kind)(dbxml, name) 250 329 newtree = xslt.apply(dbxml) 251 330 return xslt.tostring(newtree) … … 283 362 toPDF = True 284 363 285 doc = cls.to_formatted(dbxml, kind=format ).encode('utf-8')364 doc = cls.to_formatted(dbxml, kind=format, name=base).encode('utf-8') 286 365 287 366 if toPDF: nbdoc/trunk/notabene/notebook.py
r853 r854 3 3 import os 4 4 import warnings 5 import copy 5 6 6 7 from lxml import etree as ET … … 9 10 from notabene import validate 10 11 from notabene.xmlutils import nsmap, rdf, dc, xlink 12 13 def element_copy(element): 14 return ET.XML(ET.tostring(element, encoding='utf-8')) 11 15 12 16 class SubelemWrapper(object): … … 478 482 parent = child2parent[link] 479 483 path, frag = spliturl(basedir, link.get(xlink.href)) 480 print path, frag481 484 nb = Notebook.from_file(path) 482 485 sheet = nb.xpath.evaluate('/notebook/sheet[@id="%s"]' % frag) … … 489 492 dbxml = dbf.transform_sheet(sheet, nodetype=link.tag) 490 493 idx = parent.index(link) 491 parent[idx] = dbxml 494 del parent[idx] 495 parent.insert(idx, dbxml) 492 496 return book 493 497 nbdoc/trunk/notabene/styles.py
r845 r854 127 127 padding: 0; 128 128 } 129 130 div.mediaobject { 131 text-align: center; 132 margin: 1em; 133 } 134 135 span.inlinemediaobject > img { 136 vertical-align: middle; 137 } 138 129 139 """ 130 140 … … 149 159 fvopt = ET.SubElement(sheet, xsl.template, name="latex.fancyvrb.options") 150 160 fvopt.text = r",commandchars=\\\{\}" 161 162 # turn off fancyhdr 163 fancyhdr = ET.SubElement(sheet, xsl.param, name='latex.use.fancyhdr') 164 fancyhdr.text = '0' 151 165 152 166 docclass = ET.SubElement(sheet, xsl.param, … … 198 212 comment = ET.SubElement(css_style, xsl.comment) 199 213 comment.text = self.html_css() 214 ET.SubElement(sheet, xsl.param, name="section.autolabel", select="1") 215 ET.SubElement(sheet, xsl.param, 216 name="section.label.includes.component.label", select="1") 200 217 201 218 return ET.ElementTree(sheet) nbdoc/trunk/scipy_tutorial/chapter02.pybk
r845 r854 34 34 >>> scipy.sqrt(-1) </para> 35 35 </section> 36 36 <section> 37 <title>Alter numeric</title> 38 <para>With the command scipy.alter_numeric() you can now use index and mask arrays inside brackets and the coercion rules of Numeric will be changed so that Python scalars will not be used to determine the type of the output of an expression. 39 </para> 40 </section> 37 41 <section> 38 42 <title>scipy_base routines</title>
