def publish_string_with_traceback(reader=None,reader_name=None, parser_name=None,writer_name=None, source=None,source_path=None): """A modified version of publish_string, so I can request traceback. """ from docutils.core import Publisher from docutils import io pub = Publisher(reader=reader, source_class=io.StringInput, destination_class=io.StringOutput) pub.set_components(reader_name="python", parser_name="restructuredtext", writer_name="pseudoxml") pub.process_command_line(argv=["--traceback"]) pub.set_source(source=source, source_path=source_path) return pub.publish(enable_exit=False)
settings = None settings_spec = settings_spec settings_overrides = None config_section = None enable_exit_status = 1 argv = None usage = default_usage description = default_description pub = Publisher(reader, parser, writer, settings) pub.set_components(reader_name, parser_name, writer_name) if pub.settings is None: pub.process_command_line( argv, usage, description, settings_spec, config_section, **(settings_overrides or {})) output = pub.publish( argv, usage, description, settings_spec, settings_overrides, config_section, enable_exit_status)
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
# Argument values given to publish_cmdline() in rst2latex.py:: description = ('Generates PDF documents from standalone reStructuredText ' 'sources using the "latex" Writer and the "rubber" ' 'building system for LaTeX documents. ' + default_description) writer_name = 'latex' # Set up the publisher:: pub = Publisher(reader, parser, writer, settings=settings) pub.set_components(reader_name, parser_name, writer_name) # Parse the command line args # (Publisher.publish does this in a try statement):: pub.process_command_line(argv, usage, description, settings_spec, config_section, **(settings_overrides or {})) # pprint(pub.settings.__dict__) # Get source and destination path:: source = pub.settings._source destination = pub.settings._destination # print source, destination # Generate names for the temporary files and set ``destination`` to temporary # latex file: # # make_name() from rubber.cmd_pipe checks that no existing file is # overwritten. If we are going to support rubbers ``--inplace`` and ``--into`` # options, the chdir() must occure before this point to have the check in the # right directory. ::
### apply extraction/rewriter transforms. ## Prepare publisher usage = "%prog [options] [<source> [<destination>]]" description = ('Reads main document from standalone reStructuredText ' 'from <source> (default is stdin). ') pub = Publisher(Reader(), None, Writer(), settings=None, destination_class=io.NullOutput) ## Get settings for components from command-line pub.set_components(None, 'rst', None) pub.process_command_line(None, None, None, None, {}) ## Publish to doctree using reader/parser but without writing pub.set_writer('null') null_output = pub.publish( None, usage, description, None, None, config_section=None, enable_exit_status=1) document = pub.document def dump(document): # these indices are rebuild every time for a document from pprint import pformat print "Dumping vars for doc ", document['source'] print 'current_line', pformat(document.current_line) #print 'settings', pformat(document.settings)