def format(self,page,t): """ Render some Structured Text as HTML, working around some quirks. """ # an initial single word plus period becomes a numeric bullet - # prepend a temporary marker to prevent # XXX use locale/wikichars from Regexps.py instead of A-z # XXX not working ? t = re.sub(r'(?m)^([ \t]*)([A-z]\w*\.)', r'\1<!--NOSTX-->\2', t) # :: quoting fails if there is whitespace after the :: - remove it t = re.sub(r'(?m)::[ \t]+$', r'::', t) # suppress stx footnote handling so we can do it our way later t = re.sub(footnoteexpr,r'<a name="ref\1">![\1]</a>',t) t = re.sub(r'(?m)\[',r'[<!--NOSTX-->',t) # standard structured text can't handle unicode # it may not handle non-ascii either, so this could still fail t = page.toencoded(t) # let STX loose on it; don't let a formatter error break the whole page try: # XXX slow! t = HTMLWithImages(ZwikiDocumentWithImages(structurize(t)), level=2) except: BLATHER('Structured Text formatting failed: %s' \ % (formattedTraceback())) return '<pre>Structured Text formatting failed:\n%s</pre>' \ % (formattedTraceback()) t = page.tounicode(t) # clean up t = re.sub(r'(<|<)!--NOSTX--(>|>)', r'', t) # strip html & body added by some zope versions t = re.sub(r'(?sm)^<html.*<body.*?>\n(.*)</body>\n</html>\n',r'\1',t) return t
def __call__(self, doc): if isinstance(doc, string_types): doc = structurize(doc) doc.setSubparagraphs(self.color_paragraphs(doc.getSubparagraphs())) else: doc = StructuredTextDocument( self.color_paragraphs(doc.getSubparagraphs())) return doc
def testDocumentClass(self): # testing Document # *cough* *cough* this can't be enough... for f in files: doc = Document() raw_text = readFile(regressions, f) text = stng.structurize(raw_text) self.assert_(doc(text))
def _test(self, stxtxt, expected): doc = stng.structurize(stxtxt) doc = DocumentWithImages()(doc) output = HTMLWithImages()(doc, level=1) if not expected in output: print "Text: ", stxtxt.encode('utf-8') print "Converted:", output.encode('utf-8') print "Expected: ", expected.encode('utf-8') self.fail("'%s' not in result" % expected)
def __call__(self, doc): if isinstance(doc, six.string_types): doc = structurize(doc) doc.setSubparagraphs(self.color_paragraphs( doc.getSubparagraphs())) else: doc = StructuredTextDocument(self.color_paragraphs( doc.getSubparagraphs())) return doc
def _test(self, stxtxt, expected, html=HTMLWithImages): doc = stng.structurize(stxtxt) doc = DocumentWithImages()(doc) output = html()(doc, level=1) msg = ("Text: %s\n" % stxtxt + "Converted: %s\n" % output + "Expected: %s\n" % expected) self.assertIn(expected, output, msg)
def testDocumentClass(self): # testing Document # *cough* *cough* this can't be enough... from zope.structuredtext import stng from zope.structuredtext.document import Document for f in files: doc = Document() raw_text = readFile(regressions, f) text = stng.structurize(raw_text) self.assert_(doc(text))
def testDocumentClass(self): # testing Document # *cough* *cough* this can't be enough... from zope.structuredtext import stng from zope.structuredtext.document import Document for f in files: doc = Document() raw_text = readFile(regressions, f) text = stng.structurize(raw_text) self.assertTrue(doc(text))
def _test(self, stxtxt, expected): from zope.structuredtext import stng from zope.structuredtext.document import DocumentWithImages from zope.structuredtext.html import HTMLWithImages doc = stng.structurize(stxtxt) doc = DocumentWithImages()(doc) output = HTMLWithImages()(doc, level=1) if not expected in output: print("Text: ", stxtxt.encode('utf-8')) print("Converted:", output.encode('utf-8')) print("Expected: ", expected.encode('utf-8')) self.fail("'%s' not in result" % expected)
def testRegressionsTests(self): # HTML regression test for f in files: raw_text = readFile(regressions, f) doc = stng.structurize(raw_text) doc = Document()(doc) html = HTML()(doc) reg_fname = f.replace('.stx','.ref') reg_html = readFile(regressions , reg_fname) self.assertEqual(reg_html.strip(), html.strip())
def _test(self, stxtxt, expected): from zope.structuredtext import stng from zope.structuredtext.document import DocumentWithImages from zope.structuredtext.html import HTMLWithImages doc = stng.structurize(stxtxt) doc = DocumentWithImages()(doc) output = HTMLWithImages()(doc, level=1) if not expected in output: print "Text: ", stxtxt.encode('utf-8') print "Converted:", output.encode('utf-8') print "Expected: ", expected.encode('utf-8') self.fail("'%s' not in result" % expected)
def color_paragraphs(self, raw_paragraphs, type=type, sequence_types=(tuple, list), sts=string_types): result = [] for paragraph in raw_paragraphs: if paragraph.getNodeName() != 'StructuredTextParagraph': result.append(paragraph) continue for pt in self.paragraph_types: if isinstance(pt, sts): # grab the corresponding function pt = getattr(self, pt) # evaluate the paragraph new_paragraphs = pt(paragraph) if new_paragraphs: if not isinstance(new_paragraphs, sequence_types): new_paragraphs = (new_paragraphs, ) for paragraph in new_paragraphs: subs = self.color_paragraphs( paragraph.getSubparagraphs() ) paragraph.setSubparagraphs(subs) break else: # copy, retain attributes atts = getattr(paragraph, '_attributes', []) kw = dict([(att, getattr(paragraph, att)) for att in atts]) subs = self.color_paragraphs(paragraph.getSubparagraphs()) new_paragraphs = StructuredTextParagraph( paragraph.getColorizableTexts()[0], subs, **kw), # color the inline StructuredText types # for each StructuredTextParagraph for paragraph in new_paragraphs: if paragraph.getNodeName() == "StructuredTextTable": # cells = paragraph.getColumns() text = paragraph.getColorizableTexts() text = [structurize(t) for t in text] text = [self(t) for t in text] text = [t.getSubparagraphs() for t in text] paragraph.setColorizableTexts(text) paragraph.setColorizableTexts( [self.color_text(t) for t in paragraph.getColorizableTexts()]) result.append(paragraph) return result
def testRegressionsTests(self): # HTML regression test from zope.structuredtext import stng from zope.structuredtext.document import Document from zope.structuredtext.html import HTML for f in files: raw_text = readFile(regressions, f) doc = stng.structurize(raw_text) doc = Document()(doc) html = HTML()(doc) reg_fname = f.replace('.stx', '.ref') reg_html = readFile(regressions, reg_fname) self.assertEqual(reg_html.strip(), html.strip())
def HTML(aStructuredString, level=1, header=1): st = stng.structurize(aStructuredString) doc = document.DocumentWithImages()(st) return NoCodeStructured()(doc, header=header, level=level)
def _test(self, stxtxt, expected): doc = stng.structurize(stxtxt) doc = Document()(doc) output = HTML()(doc, level=1) self.failIf(output.find(expected) == -1)
def readFile(dirname, fname): myfile = open(os.path.join(dirname, fname), "r") lines = myfile.readlines() myfile.close() return ''.join(lines) def writeFile(dirname, fname, data): myfile = open(os.path.join(dirname, fname), "w") myfile.truncate() myfile.write(data) if __name__ == '__main__': files = [ 'index.stx', 'Acquisition.stx', 'ExtensionClass.stx', 'MultiMapping.stx', 'examples.stx', 'Links.stx', 'examples1.stx', 'table.stx', 'InnerLinks.stx' ] dirname = sys.argv[1] for f in files: raw_text = readFile(dirname, f) doc = stng.structurize(raw_text) doc = Document()(doc) html = HTML()(doc) reg_fname = f.replace('.stx', '.ref') reg_html = writeFile(dirname, reg_fname, html)
def stx2html(aStructuredString, level=1, header=1): """A shortcut to produce HTML. """ st = structurize(aStructuredString) doc = DocumentWithImages()(st) return HTMLWithImages()(doc, header=header, level=level)
def stx2html(aStructuredString, level=1, header=1): st = structurize(aStructuredString) doc = DocumentWithImages()(st) return HTMLWithImages()(doc, header=header, level=level)
def test_structurize_empty(self): paragraphs = '' result = stng.structurize(paragraphs) self.assertIsInstance(result, stng.StructuredTextDocument)
def structurizedFile(f): raw_text = readFile(regressions, f) text = stng.structurize(raw_text) return text
import sys import os.path from zope.structuredtext import stng from zope.structuredtext.document import Document from zope.structuredtext.html import HTML def readFile(dirname,fname): myfile = open(os.path.join(dirname, fname), "r") lines = myfile.readlines() myfile.close() return ''.join(lines) def writeFile(dirname,fname, data): myfile = open(os.path.join(dirname, fname), "w") myfile.truncate() myfile.write(data) if __name__ == '__main__': files = ['index.stx','Acquisition.stx','ExtensionClass.stx', 'MultiMapping.stx','examples.stx','Links.stx','examples1.stx', 'table.stx','InnerLinks.stx'] dirname = sys.argv[1] for f in files: raw_text = readFile(dirname, f) doc = stng.structurize(raw_text) doc = Document()(doc) html = HTML()(doc) reg_fname = f.replace('.stx','.ref') reg_html = writeFile(dirname , reg_fname, html)