예제 #1
0
파일: util.py 프로젝트: carlkibler/rested
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
예제 #2
0
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
예제 #3
0
    print 'symbol_footnotes', pformat(document.symbol_footnotes)
    print 'symbol_footnote_refs', pformat(document.symbol_footnote_refs)
    print 'footnotes', pformat(document.footnotes)
    print 'citations', pformat(document.citations)
    print 'autofootnote_start', pformat(document.autofootnote_start)
    print 'symbol_footnote_start', pformat(document.symbol_footnote_start)
    print 'id_start', pformat(document.id_start)
    print 'transform_messages', pformat(document.transform_messages)
    print 'transformer', pformat(document.transformer)
    print 'decoration', pformat(document.decoration)
    sys.exit()
#dump(document)

### 2. Render doctree to output format, 
###    apply completion transforms.

pub.source = io.DocTreeInput(document)
pub.destination_class = io.FileOutput
pub.set_destination()

pub.reader = doctree.Reader(parser_name='null')
pub.writer = Writer()

pub.apply_transforms()
output = pub.writer.write(pub.document, pub.destination)
#pub.writer.assemble_parts()


# ----