def publish_msgstr(app, source, source_path, source_line, config, settings): # type: (Sphinx, unicode, unicode, int, Config, Dict) -> nodes.document """Publish msgstr (single line) into docutils document :param sphinx.application.Sphinx app: sphinx application :param unicode source: source text :param unicode source_path: source path for warning indication :param source_line: source line for warning indication :param sphinx.config.Config config: sphinx config :param docutils.frontend.Values settings: docutils settings :return: document :rtype: docutils.nodes.document """ from sphinx.io import SphinxI18nReader reader = SphinxI18nReader( app=app, parsers=app.registry.get_source_parsers(), parser_name='restructuredtext', # default parser ) reader.set_lineno_for_reporter(source_line) doc = reader.read( source=StringInput(source=source, source_path=source_path), parser=reader.parser, settings=settings, ) try: doc = doc[0] except IndexError: # empty node pass return doc
def run(args): """The main function.""" sources = [] if args.debug: from mjstat.testdata import TEST_INPUT sources.append(StringInput(source=TEST_INPUT)) else: # XXX if isinstance(args.input, (list, tuple)): sources.extend( FileInput(source_path=i, encoding='sjis', mode='r') for i in args.input) else: sources.append( FileInput(source_path=args.input, encoding='sjis', mode='r')) parser = MJScoreParser() reader = MJScoreReader() game_data_list = tuple(reader.read(i, parser, args) for i in sources) game_data = merge_games(game_data_list) apply_transforms(game_data) writer = MJScoreWriter() writer.write(game_data, FileOutput(None))
def publish_msgstr(app, source, source_path, source_line, config, settings): # type: (Sphinx, unicode, unicode, int, Config, Dict) -> nodes.document """Publish msgstr (single line) into docutils document :param sphinx.application.Sphinx app: sphinx application :param unicode source: source text :param unicode source_path: source path for warning indication :param source_line: source line for warning indication :param sphinx.config.Config config: sphinx config :param docutils.frontend.Values settings: docutils settings :return: document :rtype: docutils.nodes.document """ from sphinx.io import SphinxI18nReader reader = SphinxI18nReader(app) parser = app.registry.create_source_parser(app, 'restructuredtext') doc = reader.read( source=StringInput(source=source, source_path="%s:%s:<translated>" % (source_path, source_line)), parser=parser, settings=settings, ) try: doc = doc[0] except IndexError: # empty node pass return doc
def aFunction(string): source = StringInput(string, encoding='utf-8') docname = "fake" __app.env.temp_data['docname'] = docname settings = frontend.OptionParser((Writer,)).get_default_values() settings.tab_width = 8 settings.pep_references = False settings.rfc_references = False settings.env = __app.env reader = DoctreeReader() parser = parsers.get_parser_class("rst")() docu = utils.new_document(source.source_path, settings) parser.parse(source.read(), docu) __app.builder.prepare_writing((docname,)) return __app.builder.write_doc(docname, docu).destination
def __set__(self, instance, value): if isinstance(value, str): if os.path.isfile(value): source = FileInput(source_path=os.path.abspath(value)) else: source = StringInput(value) else: source = NullInput() super().__set__(instance, source)
def aFunction(string): source = StringInput(string, encoding='utf-8') docname = "fake" __app.env.temp_data['docname'] = docname settings = frontend.OptionParser((Writer, )).get_default_values() settings.tab_width = 8 settings.pep_references = False settings.rfc_references = False settings.env = __app.env reader = DoctreeReader() parser = parsers.get_parser_class("rst")() docu = utils.new_document(source.source_path, settings) parser.parse(source.read(), docu) __app.builder.prepare_writing((docname, )) return __app.builder.write_doc(docname, docu).destination
def build_markup(self): """Build markup""" from docutils.readers import Reader internal_rst = self.prepare_rst() self.info("Changelog code:\n%s" % internal_rst) internal_rst_fh = StringInput(source=internal_rst) reader = Reader(parser_name='rst') option_parser = OptionParser() settings = option_parser.get_default_values() settings.update( dict(tab_width=3, pep_references=False, rfc_references=False, file_insertion_enabled=True), option_parser) document = reader.read(internal_rst_fh, reader.parser, settings) return document.children
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()
def test_notebook_translator(testdata, name): """Ensure that the notebook translator can correctly convert an rst doctree into the correct sequence of notebook cells.""" rst = testdata(os.path.join("doc", name + ".rst")).decode("utf8") json = testdata(os.path.join("doc", name + ".ipynb")).decode("utf8") nb = nbf.reads(json) # Getting docutils to generate a doctree for us appears to require some gymnastics # perhaps there is a better way? parser = Parser() settings = _get_mock_settings() reader = Reader() doctree = reader.read(StringInput(rst), parser, settings) translator = NotebookTranslator(doctree) doctree.walkabout(translator) actual = nbf.writes(translator.asnotebook()) expected = nbf.writes(nb) assert expected == actual
def publish_msgstr(app: "Sphinx", source: str, source_path: str, source_line: int, config: Config, settings: Any) -> Element: """Publish msgstr (single line) into docutils document :param sphinx.application.Sphinx app: sphinx application :param str source: source text :param str source_path: source path for warning indication :param source_line: source line for warning indication :param sphinx.config.Config config: sphinx config :param docutils.frontend.Values settings: docutils settings :return: document :rtype: docutils.nodes.document """ try: # clear rst_prolog temporarily rst_prolog = config.rst_prolog config.rst_prolog = None # type: ignore from sphinx.io import SphinxI18nReader reader = SphinxI18nReader() reader.setup(app) filetype = get_filetype(config.source_suffix, source_path) parser = app.registry.create_source_parser(app, filetype) doc = reader.read( source=StringInput(source=source, source_path="%s:%s:<translated>" % (source_path, source_line)), parser=parser, settings=settings, ) try: doc = doc[0] # type: ignore except IndexError: # empty node pass return doc finally: config.rst_prolog = rst_prolog # type: ignore
def read1(text): reader = Reader() source = StringInput(text) parser = Parser() # one time document = reader.read(source, parser, get_settings()) return document
def read(self, source, parser, settings, **params): source = StringInput(source=source, **params) return super(TextReader, self).read(source, parser, settings)
def parse(src): parser = Parser() settings = rst_mock_settings reader = Reader() return reader.read(StringInput(src), parser, settings)
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))
class VBMethod(object): """Represents a method of a control object""" # << VBMethod methods >> (1 of 3) def __init__(self, name, parameters=None): """Initialize the method""" self.name = name self.parameters = parameters or [] # << VBMethod methods >> (2 of 3) def __repr__(self): """Representation of this method""" return "VBMethod('%s', %s)" % (self.name, self.parameters) # << VBMethod methods >> (3 of 3) def __str__(self): """String representation of this method""" try: if self.parameters: return "%s(*%s*)" % (self.name, ", ".join([str(item) for item in self.parameters])) else: return "%s()" % (self.name,) except: print "Failed on ", self.name # -- end -- << VBMethod methods >> # << tlbrowse methods >> (4 of 4) def enumerate(lst): "Mimic 2.3's enumerate function" return zip(xrange(sys.maxint), lst) # -- end -- << tlbrowse methods >> if __name__=='__main__': import sys fname = None try: fname = sys.argv[1] except: pass dlg = TypeBrowseDialog(fname) if fname: controls = dlg.dumpProperties() else: dlg.DoModal() sys.exit() results = [] l = results.append l("All VB Controls in %s\n\n" % fname) l("\n".join(["* %s_" % control for control in controls])) l("\n\n") for control in controls: l("\n%s\n%s\n\n" % (control, "="*len(control))) 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
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"))