Exemplo n.º 1
0
def convertRemarkToHtml(remarkText,
                        document,
                        documentTree,
                        outputRootDirectory,
                        reporter=Reporter()):
    '''
    Converts Remark text to html.

    remarkText (list of strings):
    The Remark text to convert, string per row.

    document (Document):
    The underlying document.

    documentTree (DocumentTree):
    The underlying document tree.

    outputRootDirectory

    returns (list of strings):
    The converted html text.
    '''

    # Convert Remark to Markdown.
    markdownText, headText = convertRemarkToMarkdown(remarkText, document,
                                                     documentTree,
                                                     outputRootDirectory,
                                                     reporter)

    # Convert Markdown to html.
    return convertMarkdownToHtml(markdownText, headText, document,
                                 documentTree, outputRootDirectory, reporter)
Exemplo n.º 2
0
def convertMarkdownToHtml(markdownText,
                          headText,
                          document,
                          documentTree,
                          outputRootDirectory,
                          reporter=Reporter()):
    '''
    Converts Remark-generated Markdown to html.

    markdownText (list of strings):
    The Markdown text to convert, string per row.

    document (Document):
    The underlying document.

    documentTree (DocumentTree):
    The underlying document tree.

    outputRootDirectory (string):
    The root directory of the output.

    returns (list of strings):
    The converted html text.
    '''

    # Create a Markdown parser with Remark extensions.
    markdownParser = createMarkdownParser()

    # Convert Markdown to html.
    htmlString = markdownParser.convert('\n'.join(markdownText))
    htmlText = htmlString.split('\n')

    # Add html boilerplate.
    return addHtmlBoilerPlate(htmlText, document,
                              headText + document.tag('html_head'))
Exemplo n.º 3
0
def convertAll(documentTree, argumentSet, reporter=Reporter()):
    '''
    Converts all documents in the document-tree.

    documentTree (DocumentTree):
    The document-tree to use for conversion.

    outputRootDirectory (string):
    The output directory to place the files in.

    reporter (Reporter):
    The reporter to use for reporting.
    '''

    outputDirectory = os.path.normpath(argumentSet.outputDirectory)

    # We wish to convert the files in alphabetical order
    # by their relative-name (in the map they are in hashed order).
    sortedDocumentSet = list(documentTree.documentMap.values())
    sortedDocumentSet.sort(key=lambda x: x.relativeName)

    for document in sortedDocumentSet:
        if not document.regenerate():
            continue

        reporter.report('Generating ' + document.relativeName + '...',
                        'verbose')

        try:
            # Find the document-type based on the
            # filename-extension.
            type = documentType(document.extension)

            # Let the document-type convert the document.
            with ScopeGuard(reporter, document.relativeName):
                type.convert(document, documentTree, outputDirectory, reporter)
        except IOError:
            # If an exception is thrown, for example because a document
            # is deleted while Remark is running, then that exception
            # is acknowledged here, and a stack-trace is printed, but
            # the generation still continues for other documents.
            reporter.reportError(traceback.format_exc(), 'exception')
Exemplo n.º 4
0
def saveRemarkToHtml(remarkText,
                     document,
                     documentTree,
                     outputRootDirectory,
                     reporter=Reporter()):
    '''
    Converts Remark text to html text and saves it to a file.

    remarkText (list of strings):
    The Remark text to convert.

    documentTree (DocumentTree):
    The document-tree to use.

    outputRootDirectory (string):
    The output directory to save the file in.

    reporter (Reporter):
    The reporter to use for reporting.
    '''
    # Convert Remark to Markdown.
    markdownText, headText = convertRemarkToMarkdown(remarkText, document,
                                                     documentTree,
                                                     outputRootDirectory,
                                                     reporter)

    # Find out some names.
    outputRelativeName = outputDocumentName(document.relativeName)
    outputFullName = os.path.join(outputRootDirectory, outputRelativeName)

    if globalOptions().generateMarkdown:
        # Write the generated Markdown source.
        writeFile(markdownText,
                  withoutFileExtension(outputFullName) + '.md.txt')

    # Convert Markdown to Html.
    htmlText = convertMarkdownToHtml(markdownText, headText, document,
                                     documentTree, outputRootDirectory,
                                     reporter)

    # Write the generated html.
    writeFile(htmlText, outputFullName)
Exemplo n.º 5
0
    def __init__(self, rootDirectory, reporter = Reporter()):
        '''
        Constructs an empty document-tree using the 
        given directory as a root-directory.

        rootDirectory (string):
        The full-path to the directory to use as
        root-directory of the document-tree.

        See also:
        compute()
        insertDocument()
        '''

        # The root-directory whose child-directories
        # to construct the document-tree for.
        self.rootDirectory = rootDirectory

        # Create the orphan document, to which all the
        # unlinked documents will be given as children.
        self.orphan = Document('orphan.remark-orphan')
        self.orphan.setTag('description', ['Orphans'])

        # A map from a document's relative-name (string) to 
        # a document-object (Document).
        self.documentMap = {'orphan.remark-orphan' : self.orphan}

        # A map from a filename (string) to documents
        # with that filename (list of Document's).
        self.suffixMap = {}

        # A map from a directory (string) to a use-count (integer). 
        # The keys will contain all the directories in which the documents 
        # reside, and also all their parent-directories up to the
        # document-tree's root-directory. The value will contain the number
        # of documents that have generated the directory in the key.
        # The use-count allows us to dynamically track the directories
        # as documents are inserted and removed.
        self.directorySet = {}

        # A reporter object (Reporter).
        self.reporter = reporter
Exemplo n.º 6
0
def convertRemarkToMarkdown(remarkText,
                            document,
                            documentTree,
                            outputRootDirectory,
                            reporter=Reporter()):
    '''
    Converts Remark text to Markdown text. 

    remarkText (list of strings):
    The Remark text to convert, string per row.

    returns (list of strings, list of strings):
    The converted html text, and the html-head section.
    '''

    remark = Remark(document, documentTree, documentTree.rootDirectory,
                    outputRootDirectory, reporter)

    # Convert Remark to Markdown.
    markdownText = remark.convert(remarkText)
    markdownText += remark.postConversion()

    # Return the resulting Markdown text.
    return markdownText, remark.htmlHeader()
Exemplo n.º 7
0
remarkPageSet = ['.txt']
cppCodeViewSet = ['.cpp', '.cc', '.h', '.hh', '.hpp']
codeViewSet = ['.py', '.pyx', '.g4', '.m', '.pm', '.pl', '.css', '.js', '.json', '.lua', '.cmake']

from Remark.DocumentType_Registry import setDefaultDocumentType, associateDocumentType

setDefaultDocumentType('Copy')
associateDocumentType(remarkPageSet, 'RemarkPage')
associateDocumentType(cppCodeViewSet, 'CppCodeView')
associateDocumentType(codeViewSet, 'CodeView')
associateDocumentType('.remark-index', 'DirectoryView')
associateDocumentType('.remark-orphan', 'Orphan')

# Create a reporter for progress, error, and warning reporting.
from Remark.Reporting import Reporter
reporter = Reporter()

reporter.openScope('Remark ' + remarkVersion())

# Parse the command-line arguments
# --------------------------------

optionParser = constructOptionParser()
argumentSet, args = optionParser.parse_args(sys.argv[1:])

# Parse the command-line arguments.
argumentSet = parseArguments(argumentSet, args, reporter, optionParser)

# Act on options
# --------------
Exemplo n.º 8
0
    def __init__(self,
                 document,
                 documentTree,
                 inputRootDirectory,
                 outputRootDirectory,
                 reporter=Reporter()):
        self.scopeStack = ScopeStack()
        self.scopeStack.open('global')
        self.document = document
        self.documentTree = documentTree
        self.linkIndex = 0
        self.linkSet = []
        self.usedMacroSet = []
        self.inputRootDirectory = inputRootDirectory
        self.outputRootDirectory = outputRootDirectory
        self.reporter = reporter

        # Here we form regular expressions to identify
        # Remark macro invocations in the text.

        # This matches a macro-identifier. The macro
        # identifier is the string between [[ and ]].
        # It may include characters a to z, A to Z,
        # 0 to 9, the - and the _.
        # Examples: 'set some-variable', 'Gallery'.
        self.macroIdentifier = r'([a-zA-Z_.\- ][a-zA-Z0-9_.\- ]*)'

        # This matches whitespace, which to us means
        # spaces and tabs.
        self.whitespace = r'[ \t]*'

        # This matches an optional inline parameter.
        # Starting from the outside, the whole thing is optional.
        # The first parentheses (?: ) are just for grouping. The
        # inline parameter must start with ':', following by optional
        # whitespace. If something is left, the inline parameter is
        # that what becomes before ]].
        self.optionalInlineParameter = r'(?::' + self.whitespace + r'((?:(?!\]\]).)*))?'

        # The one-line parameter starts with a ':' and continues to
        # to end of the line. The dot . matches anything except \n.
        # The leading white-space is eaten away.
        self.optionalOneLineParameter = r'(?::' + self.whitespace + r'(.*))?'

        self.optionalOutputExpansion = r'(\+|\-)?'
        self.optionalParameterExpansion = r'(\+|\-)?'

        # Piece together the whole regex for macro-invocation.
        # It is something which starts with [[, ends with ]],
        # has expansion-signs either none, +, -, ++, +-, -+, or --,
        # has a macro identifier, and then an optional inline
        # parameter. Finally, there is an optional one-line paramater
        # after the ]].
        self.macroRegex = re.compile(r'\[\[' + self.optionalOutputExpansion +
                                     self.optionalParameterExpansion +
                                     self.macroIdentifier +
                                     self.optionalInlineParameter + r'\]\]' +
                                     self.optionalOneLineParameter)

        #macroText = r'((?:(?!]]).)*)'
        #macroRegex = re.compile(r'\[\[' + macroText + r'\]\]' + optionalOneLineParameter)
        self.wholeGroupId = 0
        self.outputExpansionGroupId = 1
        self.parameterExpansionGroupId = 2
        self.identifierGroupId = 3
        self.inlineGroupId = 4
        self.externalGroupId = 5
        self.recursionDepth = 0
        self.used = False

        # Set default variables.
        self.scopeStack.top().insert('indent', ['Verbatim'])
        self.scopeStack.top().insert('remark_version', [remarkVersion()])