def processHTML(src, depth): src = os.path.normpath(src) prefix = os.path.splitext(src)[0] suffix = os.path.splitext(src)[1][1:] if suffix != DEFAULTLANG: return dst = prefix + '.html' #.' + suffix dst_abs = os.path.normpath(os.path.join(TRGROOT, dst)) src_abs = os.path.normpath(os.path.join(SRCROOT, src)) dst_dir = os.path.dirname(dst_abs) makedir(dst_dir) if newer([src_abs], dst_abs): reportBuilding(src) arguments = [ '--no-generator', '--language=' + suffix, '--no-source-link', '--no-datestamp', '--output-encoding=iso-8859-15', '--target-suffix=html', '--stylesheet=' + '../' * depth + 'aros.css', '--link-stylesheet', src_abs, dst_abs ] publisher = Publisher() publisher.set_reader('standalone', None, 'restructuredtext') publisher.set_writer('html') publisher.publish(argv=arguments) else: reportSkipping(dst)
def _docutils_rest_to(rest, writer): """ Uses docutils to convert a ReST string to HTML. Returns a tuple containg the HTML string and the list of warning nodes that were removed from the HTML. """ # Make sure any Sphinx polution of docutils has been removed. if Sphinx is not None: for key, value in docutils_roles.items(): if value.__module__.startswith('sphinx'): docutils_roles.pop(key) pub = Publisher(source_class=docutils.io.StringInput, destination_class=docutils.io.StringOutput) pub.set_reader('standalone', None, 'restructuredtext') pub.set_writer(writer) pub.writer.default_stylesheet_path='' pub.get_settings() # Get the default settings pub.settings.halt_level = 6 # Don't halt on errors pub.settings.warning_stream = StringIO() pub.set_source(rest) pub.set_destination() pub.document = pub.reader.read(pub.source, pub.parser, pub.settings) pub.apply_transforms() # Walk the node structure of a docutils document and remove 'problematic' # and 'system_message' nodes. Save the system_message nodes. warning_nodes = [] for node in pub.document.traverse(docutils.nodes.problematic): node.parent.replace(node, node.children[0]) for node in pub.document.traverse(docutils.nodes.system_message): warning_nodes.append(node) node.parent.remove(node) return pub.writer.write(pub.document, pub.destination), warning_nodes
def convertWWW(src, language, options=None): if language == 'el': encoding = 'iso-8859-7' elif language == 'pl': encoding = 'iso-8859-2' elif language == 'ru': encoding = 'windows-1251' elif language == 'cs': encoding = 'iso-8859-2' else: encoding = 'iso-8859-15' arguments = [ '--no-generator', '--language=' + language, '--no-source-link', '--no-datestamp', '--input-encoding=' + encoding, '--output-encoding=' + encoding, '--target-suffix=' + 'php', src, '' ] if options: for option in options: arguments.insert(0, option) publisher = Publisher(destination_class=NullOutput) publisher.set_reader('standalone', None, 'restructuredtext') publisher.set_writer('html') publisher.publish(argv=arguments) return ''.join(publisher.writer.body_pre_docinfo + publisher.writer.body).encode(encoding)
def parse_docstring(doc): p = Publisher(source=doc, source_class=io.StringInput) p.set_reader('standalone', p.parser, 'restructuredtext') p.writer = Writer() p.process_programmatic_settings(None, None, None) p.set_source(doc, None) return p.publish()
def inspect(filename, source_path=None): "returns the document object before any transforms)" from docutils.core import Publisher pub = Publisher(source_class=io.FileInput) pub.set_reader("standalone", None, "restructuredtext") pub.process_programmatic_settings(None, None, None) pub.set_source(source_path=source_path) pub.set_io() return pub.reader.read(pub.source, pub.parser, pub.settings)
def inspect( filename, source_path=None, ): "returns the document object before any transforms)" from docutils.core import Publisher pub = Publisher(source_class=io.FileInput, ) pub.set_reader('standalone', None, "restructuredtext") pub.process_programmatic_settings(None, None, None) pub.set_source(source_path=source_path) pub.set_io() return pub.reader.read(pub.source, pub.parser, pub.settings)
def convertRSTfilesToHTML(root_list): """This routine creates .html files from all .rst files in root_list, the list of files that have just been tangled.""" for root in root_list: base,fullname = g.os_path_split(root) name,ext = g.os_path_splitext(fullname) if ext == ".rst": file = g.os_path_join(base,name+".html") #@ << Convert root to corresponding .html file >> #@+node:EKR.20040502194930.3:<< Convert root to corresponding .html file >> # Leo will report the execption if docutils is not installed. from docutils.core import Publisher from docutils.io import FileInput,StringOutput,StringInput # Read .rst file into s. f = open(root,"r") s = f.read() f.close() # Restucture s into output. pub = Publisher() pub.source = StringInput(pub.settings,source=s) pub.destination = StringOutput(pub.settings,encoding="utf-8") pub.set_reader('standalone',None,'restructuredtext') pub.set_writer('html') output = pub.publish() # EKR: 3/7/03: convert output using the present encoding. dict = g.scanDirectives(self.c,p=root) encoding = dict.get("encoding",None) if encoding == None: encoding = g.app.config.default_derived_file_encoding output = g.toEncodedString(output,encoding,reportErrors=True) # Write the corresponding html file. f = open(file,"w") f.write(output) f.close()
properties = controls[control].properties.keys() properties.sort() l(" *Properties*\n%s" % ("\n\n".join([" %s" % prop for prop in properties]),)) methods = controls[control].methods.keys() methods.sort() l("\n *Methods*\n%s" % ("\n\n".join([" %s" % str(controls[control].methods[method]) for method in methods]),)) text = "\n".join(results) # << Convert to HTML >> import StringIO rstFile = StringIO.StringIO(text) from docutils.core import Publisher from docutils.io import StringOutput, StringInput pub = Publisher() # Initialize the publisher pub.source = StringInput(source=text) pub.destination = StringOutput(encoding="utf-8") pub.set_reader('standalone', None, 'restructuredtext') pub.set_writer('html') output = pub.publish() print output # -- end -- << Convert to HTML >>
def build_pegtable(): """ Search all subdirs of working directory for peg files and parses peg metadata from them. Returns the table containing metadata from all the pegs. """ pegtable = [] pegdirs = [d for d in os.listdir(config.working_directory) if os.path.isdir(slashify(config.working_directory)+d) and d != 'CVS'] init_working_directory = config.working_directory for pegdir in pegdirs: dbg('Processing PEG ' + pegdir) config.working_directory = slashify(init_working_directory)+pegdir peg = {'authors': [], 'status': config.pegboard_undefined, 'topic': pegdir, 'stakeholders': [], 'last-modified': '', 'dir': pegdir, 'files': '', 'html': '', 'rst': '', 'rstfiles': [], 'ignore': [] } peg['files'] = [f for f in os.listdir(config.working_directory) \ if os.path.isfile(slashify(config.working_directory)+f) and not f.startswith('.') and '#' not in f and '~' not in f] if peg['files'].count('peg.rst') > 0: peg['rst'] = 'peg.rst' else: for pegfile in peg['files']: if pegfile.endswith('.rst'): peg['rst'] = pegfile rstfiles = [f for f in peg['files'] if f.endswith('.rst')] config.dbg.mute('docutils') config.mp_generate = 0 for rstfile in rstfiles: config.input_filename = rstfile config.output_filename = rstfile[0:len(rstfile)-4]+config.midfix+'.html' pub = Publisher() pub.set_reader('standalone', None, 'restructuredtext') filename = slashify(config.working_directory)+rstfile pub.process_command_line(argv=('--config '+config.docutils_conf+' '+filename+'').split()) #conversion may fail because of bad restructuredtext try: pub.set_io() document = pub.reader.read(pub.source, pub.parser, pub.settings) pub.apply_transforms(document) peg['ignore'].append(config.output_filename) #conversion have succeeded so far, parsing peg's metadata #from its document tree if rstfile == peg['rst']: peg['html'] = rstfile[0:len(rstfile)-4]+config.midfix+'.html' peg['topic'] = getTagValue(document, 'title', always_raw=1) peg['topic'] = peg['topic'] peg['last-modified'] = getFieldTagValue(document, 'last-modified') #we may have got 'rawsource', which needs some tidying if peg['last-modified'].startswith('$Date'): peg['last-modified'] = peg['last-modified'][7:len(peg['last-modified'])-11].replace('/', '-') peg['status'] = getTagValue(document, 'status') or config.pegboard_undefined stakeholders = getFieldTagValue(document, 'stakeholder') if not stakeholders: stakeholders = getFieldTagValue(document, 'stakeholders') peg['stakeholders'] = [s.strip() for s in stakeholders.split(',')] peg['authors'] = getTagValue(document, 'author', all=1) else: status = getTagValue(document, 'status') if status: peg['rstfiles'].append({'filename': rstfile, 'status': status}) except: dbg_fail('PEG %s: Docutil raised an exception while converting %s. ' % (pegdir, rstfile)) dbg_fail('Conversion failed and pegbaord data could not be collected.\n') config.dbg.enable('docutils') config.mp_generate = 1 if not peg['html']: for file in peg['files']: if file[len(file)-5:len(file)] == '.html': peg['html'] = file break elif file[len(file)-4:len(file)] in ('.rst', '.txt'): peg['html'] = file break config.intput_filename = '' #finally adds peg's metadata into pegtable pegtable.append(peg) config.working_directory = init_working_directory return pegtable
def onIconDoubleClick(tag,keywords): v = keywords.get("p") or keywords.get("v") c = keywords.get("c") # g.trace(c) h = v.headString().strip() if g.match_word(h,0,"@text"): fname = h[5:] ext = os.path.splitext(fname)[1].lower() if ext in ('.htm','.html','.tex'): #@ << write rST as HTML/LaTeX >> #@+node:edream.111803100242.4:<< write rST as HTML/LaTeX >> try: import docutils except ImportError: docutils = None g.es('HTML/LaTeX generation requires docutils') if docutils: import StringIO rstFile = StringIO.StringIO() writeTreeAsRst(rstFile, fname, v, c) rstText = rstFile.getvalue() # Set the writer and encoding for the converted file if ext in ('.html','.htm'): writer='html' enc="utf-8" else: writer='latex' enc="iso-8859-1" #@ << convert rST to HTML/LaTeX >> #@+node:edream.111803100242.5:<< convert rST to HTML/LaTeX >> # this code snipped has been taken from code contributed by Paul Paterson 2002-12-05 from docutils.core import Publisher from docutils.io import StringOutput, StringInput pub = Publisher() # Initialize the publisher pub.source = StringInput(source=rstText) pub.destination = StringOutput(pub.settings, encoding=enc) pub.set_reader('standalone', None, 'restructuredtext') pub.set_writer(writer) output = pub.publish(argv=['']) #@nonl #@-node:edream.111803100242.5:<< convert rST to HTML/LaTeX >> #@nl convertedFile = file(fname,'w') convertedFile.write(output) convertedFile.close() rstFile.close() g.es('written: '+str(fname)) #@nonl #@-node:edream.111803100242.4:<< write rST as HTML/LaTeX >> #@nl else: #@ << write rST file >> #@+node:edream.111803100242.6:<< write rST file >> rstFile = file(fname,'w') writeTreeAsRst(rstFile, fname, v, c) rstFile.close() g.es('written: '+str(fname))
SPELL_CHECK = False # Rasta Core Library try: from mainWindow import Ui_Rasta except ImportError: sys.exit(_('Please run "setup.py build" first.')) # Log Model for log view from model import LogTableModel TMPFILE = '/tmp/untitled.rst' # Global Publisher for Docutils PUB = Publisher(source_class=docutils.io.StringInput, destination_class=docutils.io.StringOutput) PUB.set_reader('standalone', None, 'restructuredtext') PUB.set_writer('html') PUB.get_settings() PUB.settings.halt_level = 7 PUB.settings.warning_stream = StringIO() def clear_log(log): ''' Removes not needed lines from log output ''' try: log = unicode(log) return re.findall("line\=\"(.*?)\"", log)[0], re.findall("\<paragraph\>(.*?)\<\/paragraph\>", log)[0] except: return 1, _('Rasta parse error: %s' % log)
def onIconDoubleClick(tag, keywords): v = keywords.get("p") or keywords.get("v") c = keywords.get("c") # g.trace(c) h = v.headString().strip() if g.match_word(h, 0, "@text"): fname = h[5:] ext = os.path.splitext(fname)[1].lower() if ext in ('.htm', '.html', '.tex'): #@ << write rST as HTML/LaTeX >> #@+node:edream.111803100242.4:<< write rST as HTML/LaTeX >> try: import docutils except ImportError: docutils = None g.es('HTML/LaTeX generation requires docutils') if docutils: import StringIO rstFile = StringIO.StringIO() writeTreeAsRst(rstFile, fname, v, c) rstText = rstFile.getvalue() # Set the writer and encoding for the converted file if ext in ('.html', '.htm'): writer = 'html' enc = "utf-8" else: writer = 'latex' enc = "iso-8859-1" #@ << convert rST to HTML/LaTeX >> #@+node:edream.111803100242.5:<< convert rST to HTML/LaTeX >> # this code snipped has been taken from code contributed by Paul Paterson 2002-12-05 from docutils.core import Publisher from docutils.io import StringOutput, StringInput pub = Publisher() # Initialize the publisher pub.source = StringInput(source=rstText) pub.destination = StringOutput(pub.settings, encoding=enc) pub.set_reader('standalone', None, 'restructuredtext') pub.set_writer(writer) output = pub.publish(argv=['']) #@nonl #@-node:edream.111803100242.5:<< convert rST to HTML/LaTeX >> #@nl convertedFile = file(fname, 'w') convertedFile.write(output) convertedFile.close() rstFile.close() g.es('written: ' + str(fname)) #@nonl #@-node:edream.111803100242.4:<< write rST as HTML/LaTeX >> #@nl else: #@ << write rST file >> #@+node:edream.111803100242.6:<< write rST file >> rstFile = file(fname, 'w') writeTreeAsRst(rstFile, fname, v, c) rstFile.close() g.es('written: ' + str(fname))
def onIconDoubleClick(tag,keywords): c = keywords.get("c") p = keywords.get("p") # g.trace(c) if not c or not p: return applyConfiguration(c) config.tag = tag h = p.headString().strip() if g.match_word(h,0,"@rst"): if len(h) > 5: fname = h[5:] ext = os.path.splitext(fname)[1].lower() if ext in ('.htm','.html','.tex'): #@ << write rST as HTML/LaTeX >> #@+node:ekr.20040331071319.4:<< write rST as HTML/LaTeX >> try: import docutils except: g.es('HTML/LaTeX generation requires docutils') return else: import docutils.parsers.rst from docutils.core import Publisher from docutils.io import StringOutput, StringInput, FileOutput,FileInput import StringIO # Set the writer and encoding for the converted file if ext in ('.html','.htm'): writer='html' ; enc="utf-8" else: writer='latex' ; enc="iso-8859-1" syntax = False if writer == 'html': try: import SilverCity #@ << define code-block >> #@+node:ekr.20040331071319.5:<< define code-block >> def code_block(name,arguments,options,content,lineno,content_offset,block_text,state,state_machine): """Create a code-block directive for docutils.""" # See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252170 language = arguments[0] module = getattr(SilverCity,language) generator = getattr(module,language+"HTMLGenerator") io = StringIO.StringIO() generator().generate_html(io, '\n'.join(content)) html = '<div class="code-block">\n%s\n</div>\n'%io.getvalue() raw = docutils.nodes.raw('',html, format='html') #(self, rawsource='', text='', *children, **attributes): return [raw] # These are documented at http://docutils.sourceforge.net/spec/howto/rst-directives.html. code_block.arguments = ( 1, # Number of required arguments. 0, # Number of optional arguments. 0) # True if final argument may contain whitespace. # A mapping from option name to conversion function. code_block.options = { 'language' : docutils.parsers.rst.directives.unchanged # Return the text argument, unchanged } code_block.content = 1 # True if content is allowed. # Register the directive with docutils. docutils.parsers.rst.directives.register_directive('code-block',code_block) config.do_replace_code_blocks = False #@nonl #@-node:ekr.20040331071319.5:<< define code-block >> #@nl syntax = True except ImportError: g.es('SilverCity not present so no syntax highlighting') #@ << define alternate code block implementation>> #@+node:bwmulder.20050326114320: << define alternate code block implementation>> # Don't know what to do here: Can someone make a suggestion? #import docutils.parsers.rst.directives.admonitions #import docutils.parsers.rst.directives.body # docutils.parsers.rst.directives._directives['code-block'] = docutils.parsers.rst.directives.body.block # docutils.parsers.rst.directives.register_directive('code-block', docutils.parsers.rst.directives.admonitions.admonition) #docutils.parsers.rst.directives._directives['code-block'] = docutils.parsers.rst.directives.body.pull_quote # g.es("Registered some alternate implementation for code-block directive") config.do_replace_code_blocks = config.rst2_replace_code_blocks #@-node:bwmulder.20050326114320: << define alternate code block implementation>> #@nl if config.rst2file: rstFileName = os.path.splitext(fname)[0] + ".txt" rstFile = file(rstFileName, "w") g.es("Using %s as rst file" % rstFileName) else: rstFile = StringIO.StringIO() config.current_file = fname writeTreeAsRst(rstFile,fname,p,c,syntax=syntax) if config.rst2file: rstFile.close() else: rstText = rstFile.getvalue() # This code snipped has been taken from code contributed by Paul Paterson 2002-12-05. pub = Publisher() if config.rst2file: pub.source = FileInput(source_path=rstFileName) pub.destination = FileOutput(destination_path=fname, encoding='unicode') else: pub.source = StringInput(source=rstText) pub.destination = StringOutput(pub.settings, encoding=enc) pub.set_reader('standalone', None, 'restructuredtext') pub.set_writer(writer) output = pub.publish(argv=['']) if config.rst2file: pass else: convertedFile = file(fname,'w') convertedFile.write(output) convertedFile.close() rstFile.close() writeFullFileName(fname) return http_support_main(tag, fname) #@-node:ekr.20040331071319.4:<< write rST as HTML/LaTeX >> #@nl else: #@ << write rST file >> #@+node:ekr.20040331071319.6:<< write rST file >> rstFile = file(fname,'w') writeTreeAsRst(rstFile,fname,p,c) rstFile.close() writeFullFileName(fname) #@nonl #@-node:ekr.20040331071319.6:<< write rST file >> #@nl else: # if the headline only contains @rst then open the node and its parent in text editor # this works for me but needs to be generalized and should probably be a component # of the open_with plugin. if 0: c.openWith(("os.startfile", None, ".txt")) c.selectVnode(p.parent()) c.openWith(("os.startfile", None, ".tp"))
def onIconDoubleClick(tag, keywords): c = keywords.get("c") p = keywords.get("p") # g.trace(c) if not c or not p: return applyConfiguration(c) config.tag = tag h = p.headString().strip() if g.match_word(h, 0, "@rst"): if len(h) > 5: fname = h[5:] ext = os.path.splitext(fname)[1].lower() if ext in ('.htm', '.html', '.tex'): #@ << write rST as HTML/LaTeX >> #@+node:ekr.20040331071319.4:<< write rST as HTML/LaTeX >> try: import docutils except: g.es('HTML/LaTeX generation requires docutils') return else: import docutils.parsers.rst from docutils.core import Publisher from docutils.io import StringOutput, StringInput, FileOutput, FileInput import StringIO # Set the writer and encoding for the converted file if ext in ('.html', '.htm'): writer = 'html' enc = "utf-8" else: writer = 'latex' enc = "iso-8859-1" syntax = False if writer == 'html': try: import SilverCity #@ << define code-block >> #@+node:ekr.20040331071319.5:<< define code-block >> def code_block(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): """Create a code-block directive for docutils.""" # See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252170 language = arguments[0] module = getattr(SilverCity, language) generator = getattr(module, language + "HTMLGenerator") io = StringIO.StringIO() generator().generate_html(io, '\n'.join(content)) html = '<div class="code-block">\n%s\n</div>\n' % io.getvalue( ) raw = docutils.nodes.raw( '', html, format='html' ) #(self, rawsource='', text='', *children, **attributes): return [raw] # These are documented at http://docutils.sourceforge.net/spec/howto/rst-directives.html. code_block.arguments = ( 1, # Number of required arguments. 0, # Number of optional arguments. 0 ) # True if final argument may contain whitespace. # A mapping from option name to conversion function. code_block.options = { 'language': docutils.parsers.rst.directives. unchanged # Return the text argument, unchanged } code_block.content = 1 # True if content is allowed. # Register the directive with docutils. docutils.parsers.rst.directives.register_directive( 'code-block', code_block) config.do_replace_code_blocks = False #@nonl #@-node:ekr.20040331071319.5:<< define code-block >> #@nl syntax = True except ImportError: g.es( 'SilverCity not present so no syntax highlighting') #@ << define alternate code block implementation>> #@+node:bwmulder.20050326114320: << define alternate code block implementation>> # Don't know what to do here: Can someone make a suggestion? #import docutils.parsers.rst.directives.admonitions #import docutils.parsers.rst.directives.body # docutils.parsers.rst.directives._directives['code-block'] = docutils.parsers.rst.directives.body.block # docutils.parsers.rst.directives.register_directive('code-block', docutils.parsers.rst.directives.admonitions.admonition) #docutils.parsers.rst.directives._directives['code-block'] = docutils.parsers.rst.directives.body.pull_quote # g.es("Registered some alternate implementation for code-block directive") config.do_replace_code_blocks = config.rst2_replace_code_blocks #@-node:bwmulder.20050326114320: << define alternate code block implementation>> #@nl if config.rst2file: rstFileName = os.path.splitext(fname)[0] + ".txt" rstFile = file(rstFileName, "w") g.es("Using %s as rst file" % rstFileName) else: rstFile = StringIO.StringIO() config.current_file = fname writeTreeAsRst(rstFile, fname, p, c, syntax=syntax) if config.rst2file: rstFile.close() else: rstText = rstFile.getvalue() # This code snipped has been taken from code contributed by Paul Paterson 2002-12-05. pub = Publisher() if config.rst2file: pub.source = FileInput(source_path=rstFileName) pub.destination = FileOutput(destination_path=fname, encoding='unicode') else: pub.source = StringInput(source=rstText) pub.destination = StringOutput(pub.settings, encoding=enc) pub.set_reader('standalone', None, 'restructuredtext') pub.set_writer(writer) output = pub.publish(argv=['']) if config.rst2file: pass else: convertedFile = file(fname, 'w') convertedFile.write(output) convertedFile.close() rstFile.close() writeFullFileName(fname) return http_support_main(tag, fname) #@-node:ekr.20040331071319.4:<< write rST as HTML/LaTeX >> #@nl else: #@ << write rST file >> #@+node:ekr.20040331071319.6:<< write rST file >> rstFile = file(fname, 'w') writeTreeAsRst(rstFile, fname, p, c) rstFile.close() writeFullFileName(fname) #@nonl #@-node:ekr.20040331071319.6:<< write rST file >> #@nl else: # if the headline only contains @rst then open the node and its parent in text editor # this works for me but needs to be generalized and should probably be a component # of the open_with plugin. if 0: c.openWith(("os.startfile", None, ".txt")) c.selectVnode(p.parent()) c.openWith(("os.startfile", None, ".tp"))