/usr/share/pyshared/twisted/lore/slides.py is in python-twisted-lore 12.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | # -*- test-case-name: twisted.lore.test.test_slides -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Rudimentary slide support for Lore.
TODO:
- Complete mgp output target
- syntax highlighting
- saner font handling
- probably lots more
- Add HTML output targets
- one slides per page (with navigation links)
- all in one page
Example input file::
<html>
<head><title>Title of talk</title></head>
<body>
<h1>Title of talk</h1>
<h2>First Slide</h2>
<ul>
<li>Bullet point</li>
<li>Look ma, I'm <strong>bold</strong>!</li>
<li>... etc ...</li>
</ul>
<h2>Second Slide</h2>
<pre class="python">
# Sample code sample.
print "Hello, World!"
</pre>
</body>
</html>
"""
from xml.dom import minidom as dom
import os.path, re
from cStringIO import StringIO
from twisted.lore import default
from twisted.web import domhelpers
from twisted.python import text
# These should be factored out
from twisted.lore.latex import BaseLatexSpitter, LatexSpitter, processFile
from twisted.lore.latex import getLatexText, HeadingLatexSpitter
from twisted.lore.tree import getHeaders
from twisted.lore.tree import removeH1, fixAPI, fontifyPython
from twisted.lore.tree import addPyListings, addHTMLListings, setTitle
hacked_entities = { 'amp': ' &', 'gt': ' >', 'lt': ' <', 'quot': ' "',
'copy': ' (c)'}
entities = { 'amp': '&', 'gt': '>', 'lt': '<', 'quot': '"',
'copy': '(c)'}
class MagicpointOutput(BaseLatexSpitter):
bulletDepth = 0
def writeNodeData(self, node):
buf = StringIO()
getLatexText(node, buf.write, entities=hacked_entities)
data = buf.getvalue().rstrip().replace('\n', ' ')
self.writer(re.sub(' +', ' ', data))
def visitNode_title(self, node):
self.title = domhelpers.getNodeText(node)
def visitNode_body(self, node):
# Adapted from tree.generateToC
self.fontStack = [('standard', None)]
# Title slide
self.writer(self.start_h2)
self.writer(self.title)
self.writer(self.end_h2)
self.writer('%center\n\n\n\n\n')
for authorNode in domhelpers.findElementsWithAttribute(node, 'class', 'author'):
getLatexText(authorNode, self.writer, entities=entities)
self.writer('\n')
# Table of contents
self.writer(self.start_h2)
self.writer(self.title)
self.writer(self.end_h2)
for element in getHeaders(node):
level = int(element.tagName[1])-1
self.writer(level * '\t')
self.writer(domhelpers.getNodeText(element))
self.writer('\n')
self.visitNodeDefault(node)
def visitNode_div_author(self, node):
# Skip this node; it's already been used by visitNode_body
pass
def visitNode_div_pause(self, node):
self.writer('%pause\n')
def visitNode_pre(self, node):
# TODO: Syntax highlighting
buf = StringIO()
getLatexText(node, buf.write, entities=entities)
data = buf.getvalue()
data = text.removeLeadingTrailingBlanks(data)
lines = data.split('\n')
self.fontStack.append(('typewriter', 4))
self.writer('%' + self.fontName() + '\n')
for line in lines:
self.writer(' ' + line + '\n')
del self.fontStack[-1]
self.writer('%' + self.fontName() + '\n')
def visitNode_ul(self, node):
if self.bulletDepth > 0:
self.writer(self._start_ul)
self.bulletDepth += 1
self.start_li = self._start_li * self.bulletDepth
self.visitNodeDefault(node)
self.bulletDepth -= 1
self.start_li = self._start_li * self.bulletDepth
def visitNode_strong(self, node):
self.doFont(node, 'bold')
def visitNode_em(self, node):
self.doFont(node, 'italic')
def visitNode_code(self, node):
self.doFont(node, 'typewriter')
def doFont(self, node, style):
self.fontStack.append((style, None))
self.writer(' \n%cont, ' + self.fontName() + '\n')
self.visitNodeDefault(node)
del self.fontStack[-1]
self.writer('\n%cont, ' + self.fontName() + '\n')
def fontName(self):
names = [x[0] for x in self.fontStack]
if 'typewriter' in names:
name = 'typewriter'
else:
name = ''
if 'bold' in names:
name += 'bold'
if 'italic' in names:
name += 'italic'
if name == '':
name = 'standard'
sizes = [x[1] for x in self.fontStack]
sizes.reverse()
for size in sizes:
if size:
return 'font "%s", size %d' % (name, size)
return 'font "%s"' % name
start_h2 = "%page\n\n"
end_h2 = '\n\n\n'
_start_ul = '\n'
_start_li = "\t"
end_li = "\n"
def convertFile(filename, outputter, template, ext=".mgp"):
fout = open(os.path.splitext(filename)[0]+ext, 'w')
fout.write(open(template).read())
spitter = outputter(fout.write, os.path.dirname(filename), filename)
fin = open(filename)
processFile(spitter, fin)
fin.close()
fout.close()
# HTML DOM tree stuff
def splitIntoSlides(document):
body = domhelpers.findNodesNamed(document, 'body')[0]
slides = []
slide = []
title = '(unset)'
for child in body.childNodes:
if isinstance(child, dom.Element) and child.tagName == 'h2':
if slide:
slides.append((title, slide))
slide = []
title = domhelpers.getNodeText(child)
else:
slide.append(child)
slides.append((title, slide))
return slides
def insertPrevNextLinks(slides, filename, ext):
for slide in slides:
for name, offset in (("previous", -1), ("next", +1)):
if (slide.pos > 0 and name == "previous") or \
(slide.pos < len(slides)-1 and name == "next"):
for node in domhelpers.findElementsWithAttribute(slide.dom, "class", name):
if node.tagName == 'a':
node.setAttribute('href', '%s-%d%s'
% (filename[0], slide.pos+offset, ext))
else:
text = dom.Text()
text.data = slides[slide.pos+offset].title
node.appendChild(text)
else:
for node in domhelpers.findElementsWithAttribute(slide.dom, "class", name):
pos = 0
for child in node.parentNode.childNodes:
if child is node:
del node.parentNode.childNodes[pos]
break
pos += 1
class HTMLSlide:
def __init__(self, dom, title, pos):
self.dom = dom
self.title = title
self.pos = pos
def munge(document, template, linkrel, d, fullpath, ext, url, config):
# FIXME: This has *way* to much duplicated crap in common with tree.munge
#fixRelativeLinks(template, linkrel)
removeH1(document)
fixAPI(document, url)
fontifyPython(document)
addPyListings(document, d)
addHTMLListings(document, d)
#fixLinks(document, ext)
#putInToC(template, generateToC(document))
template = template.cloneNode(1)
# Insert the slides into the template
slides = []
pos = 0
for title, slide in splitIntoSlides(document):
t = template.cloneNode(1)
text = dom.Text()
text.data = title
setTitle(t, [text])
tmplbody = domhelpers.findElementsWithAttribute(t, "class", "body")[0]
tmplbody.childNodes = slide
tmplbody.setAttribute("class", "content")
# FIXME: Next/Prev links
# FIXME: Perhaps there should be a "Template" class? (setTitle/setBody
# could be methods...)
slides.append(HTMLSlide(t, title, pos))
pos += 1
insertPrevNextLinks(slides, os.path.splitext(os.path.basename(fullpath)), ext)
return slides
from tree import makeSureDirectoryExists
def getOutputFileName(originalFileName, outputExtension, index):
return os.path.splitext(originalFileName)[0]+'-'+str(index) + outputExtension
def doFile(filename, linkrel, ext, url, templ, options={}, outfileGenerator=getOutputFileName):
from tree import parseFileAndReport
doc = parseFileAndReport(filename)
slides = munge(doc, templ, linkrel, os.path.dirname(filename), filename, ext, url, options)
for slide, index in zip(slides, range(len(slides))):
newFilename = outfileGenerator(filename, ext, index)
makeSureDirectoryExists(newFilename)
f = open(newFilename, 'wb')
slide.dom.writexml(f)
f.close()
# Prosper output
class ProsperSlides(LatexSpitter):
firstSlide = 1
start_html = '\\documentclass[ps]{prosper}\n'
start_body = '\\begin{document}\n'
start_div_author = '\\author{'
end_div_author = '}'
def visitNode_h2(self, node):
if self.firstSlide:
self.firstSlide = 0
self.end_body = '\\end{slide}\n\n' + self.end_body
else:
self.writer('\\end{slide}\n\n')
self.writer('\\begin{slide}{')
spitter = HeadingLatexSpitter(self.writer, self.currDir, self.filename)
spitter.visitNodeDefault(node)
self.writer('}')
def _write_img(self, target):
self.writer('\\begin{center}\\includegraphics[%%\nwidth=1.0\n\\textwidth,'
'height=1.0\\textheight,\nkeepaspectratio]{%s}\\end{center}\n' % target)
class PagebreakLatex(LatexSpitter):
everyN = 1
currentN = 0
seenH2 = 0
start_html = LatexSpitter.start_html+"\\date{}\n"
start_body = '\\begin{document}\n\n'
def visitNode_h2(self, node):
if not self.seenH2:
self.currentN = 0
self.seenH2 = 1
else:
self.currentN += 1
self.currentN %= self.everyN
if not self.currentN:
self.writer('\\clearpage\n')
level = (int(node.tagName[1])-2)+self.baseLevel
self.writer('\n\n\\'+level*'sub'+'section*{')
spitter = HeadingLatexSpitter(self.writer, self.currDir, self.filename)
spitter.visitNodeDefault(node)
self.writer('}\n')
class TwoPagebreakLatex(PagebreakLatex):
everyN = 2
class SlidesProcessingFunctionFactory(default.ProcessingFunctionFactory):
latexSpitters = default.ProcessingFunctionFactory.latexSpitters.copy()
latexSpitters['prosper'] = ProsperSlides
latexSpitters['page'] = PagebreakLatex
latexSpitters['twopage'] = TwoPagebreakLatex
def getDoFile(self):
return doFile
def generate_mgp(self, d, fileNameGenerator=None):
template = d.get('template', 'template.mgp')
df = lambda file, linkrel: convertFile(file, MagicpointOutput, template, ext=".mgp")
return df
factory=SlidesProcessingFunctionFactory()
|