Exemplo n.º 1
0
    def load_source(self, srcfile, code='python'):
        """
        Load source and format them as code

            #!jinja
            {{ load_source('example.py') }}
            {{ load_source('example.ini', 'ini') }}
        """
        x_srcfile = ''
        for path in self.paths:
            if os.access(path+'/'+srcfile, os.R_OK):
                x_srcfile = path+'/'+srcfile
                break
        if not x_srcfile:
            raise SystemExit('Access denied to text file %s' % srcfile)

        doc = ''
        with open(x_srcfile, 'r', encoding=self.encoding) as f:
            class Obj:
                def groups(self):
                    doc = self.uni(f.read())
                    doc = re_amp.sub(r"&", doc)
                    doc = re_gt.sub(r">", doc)
                    doc = re_lt.sub(r"<", doc)
                    return (code, doc)

            # highlighting in pre tags
            doc = _code(Obj())

            # links
            doc = re_link.sub(r'<a href="\1">\1</a>', doc)

            doc = self.linked_api(doc)       # api keywords
        return pep_rfc(doc)
Exemplo n.º 2
0
    def load_source(self, srcfile, code='python'):
        """
        Load source and format them as code

            #!jinja
            {{ load_source('example.py') }}
            {{ load_source('example.ini', 'ini') }}
        """
        x_srcfile = ''
        for path in self.paths:
            if os.access(path + '/' + srcfile, os.R_OK):
                x_srcfile = path + '/' + srcfile
                break
        if not x_srcfile:
            raise SystemExit('Access denied to text file %s' % srcfile)

        doc = ''
        with open(x_srcfile, 'r', encoding=self.encoding) as f:

            class Obj:
                def groups(self):
                    doc = f.read()
                    doc = re_amp.sub(r"&amp;", doc)
                    doc = re_gt.sub(r"&gt;", doc)
                    doc = re_lt.sub(r"&lt;", doc)
                    return (code, doc)

            # highlighting in pre tags
            doc = _code(Obj())

            # links
            doc = re_link.sub(r'<a href="\1">\1</a>', doc)

            doc = self.linked_api(doc)  # api keywords
        return pep_rfc(doc)
Exemplo n.º 3
0
    def rst(self, doc, link='link', top='top', title='__doc__',
            section_level=2, system_message=False):
        """
        Call reStructuredText docutil parser for doc and return it with html
        representation of reStructuredText formating. For more details see
        http://docutils.sourceforge.net/rst.html.
        """
        writer = Writer()
        parts = publish_parts(source=doc,
                              writer=writer,
                              writer_name='html',
                              settings_overrides={
                                  'link': link, 'top': top, 'title': title,
                                  'initial_header_level': section_level,
                                  'no_system_messages': not system_message})

        out = parts['body'] + parts['html_line'] + \
            parts['html_footnotes'] + parts['html_citations']

        out = re_source.sub(doctest_code, out.strip())

        if out.startswith('<p') and out.endswith('</p>') and \
                out.count('</p>') == 1:     # strip paragraph if is one
            out = out[out.index('>')+1:-4]
        return self.linked_api(pep_rfc(out))
Exemplo n.º 4
0
    def rst(self, doc, link='link', top='top', title='__doc__',
            section_level=2, system_message=False):
        """
        Call reStructuredText docutil parser for doc and return it with html
        representation of reStructuredText formating. For more details see
        http://docutils.sourceforge.net/rst.html.
        """
        writer = Writer()
        parts = publish_parts(source=doc,
                              writer=writer,
                              writer_name='html',
                              settings_overrides={
                                  'link': link, 'top': top, 'title': title,
                                  'initial_header_level': section_level,
                                  'no_system_messages': not system_message})

        out = parts['body'] + parts['html_line'] + \
            parts['html_footnotes'] + parts['html_citations']

        out = re_source.sub(_doctest_code, out.strip())

        if out.startswith('<p') and out.endswith('</p>') and \
                out.count('</p>') == 1:     # strip paragraph if is one
            out = out[out.index('>')+1:-4]
        return self.linked_api(pep_rfc(out))
Exemplo n.º 5
0
    def load_rst(self, rstfile, link='link', top='top', system_message=False):
        """Load reStructuredText file and create docs list of headers and text.

        Parameters:
            rstfile - string, reStructured source file name (readme.rst)
            link - link label for headers. If is empty, link href will be
                hidden.
            top - top label for headers. If is empty, top href will be hidden.

            #!jinja
            {% set sections = load_rst('readme.rst', '', '') %}
            {% type, filename, _none_, text = sections[-1] %}
        """

        x_rstfile = ''
        for path in self.paths:
            if os.access(path+'/'+rstfile, os.R_OK):
                x_rstfile = path+'/'+rstfile
                break
        if not x_rstfile:
            raise SystemExit('Access denied to text file %s' % rstfile)

        with open(x_rstfile, 'r', encoding=self.encoding) as f:
            doc = f.read()

        writer = Writer()
        parts = publish_parts(source=doc,
                              source_path=x_rstfile,
                              writer=writer,
                              writer_name='html',
                              settings_overrides={
                                  'link': link, 'top': top,
                                  'no_system_messages': not system_message})

        out = parts['body']
        if parts['html_footnotes'] or parts['html_citations']:
            out += parts['html_line'] + \
                parts['html_footnotes'] + parts['html_citations']

        out = re_source.sub(doctest_code, out)

        if out.startswith('<p') and out.endswith('</p>') and \
                out.count('</p>') == 1:     # strip paragraph if is one
            out = out[out.index('>')+1:-4]

        out = self.linked_api(pep_rfc(out))

        retval = list(('h%d' % lvl, name, id, '')
                      for lvl, name, id in parts['sections'])
        # TODO: append '(author, date, verstion)' from rst if exist like in
        # module
        retval.append(('text', parts['title'], None, out))
        return retval
Exemplo n.º 6
0
    def load_rst(self, rstfile, link='link', top='top', system_message=False):
        """Load reStructuredText file and create docs list of headers and text.

        Parameters:
            rstfile - string, reStructured source file name (readme.rst)
            link - link label for headers. If is empty, link href will be
                hidden.
            top - top label for headers. If is empty, top href will be hidden.

            #!jinja
            {% set sections = load_rst('readme.rst', '', '') %}
            {% type, filename, _none_, text = sections[-1] %}
        """

        x_rstfile = ''
        for path in self.paths:
            if os.access(path+'/'+rstfile, os.R_OK):
                x_rstfile = path+'/'+rstfile
                break
        if not x_rstfile:
            raise SystemExit('Access denied to text file %s' % rstfile)

        with open(x_rstfile, 'r', encoding=self.encoding) as f:
            doc = f.read()

        writer = Writer()
        parts = publish_parts(source=doc,
                              source_path=x_rstfile,
                              writer=writer,
                              writer_name='html',
                              settings_overrides={
                                  'link': link, 'top': top,
                                  'no_system_messages': not system_message})

        out = parts['body']
        if parts['html_footnotes'] or parts['html_citations']:
            out += parts['html_line'] + \
                parts['html_footnotes'] + parts['html_citations']

        out = re_source.sub(_doctest_code, out)

        if out.startswith('<p') and out.endswith('</p>') and \
                out.count('</p>') == 1:     # strip paragraph if is one
            out = out[out.index('>')+1:-4]

        out = self.linked_api(pep_rfc(out))

        retval = list(('h%d' % lvl, self.uni(name), id, '')
                      for lvl, name, id in parts['sections'])
        # TODO: append '(author, date, verstion)' from rst if exist like in
        # module
        retval.append(('text', parts['title'], None, self.uni(out)))
        return retval
Exemplo n.º 7
0
    def wiki(self, doc, link='link', top='top', name='__doc__',
             section_level=2, system_message=False):
        """
        Call some regular expressions on doc, and return it with html
        interpretation of wiki formating. If you want to create links to
        know api for your module, just call keywords function after gets
        full api documentation list.

            #!jinja
            {{ wiki(string) }}
            {{ wiki('=header1 =') }}            {# <h1>header 1</h1> #}
            {{ wiki('=header2 =') }}            {# <h2>header 2</h2> #}
            {{ wiki('=header3 =') }}            {# <h3>header 3</h3> #}
            {{ wiki('=header4 =') }}            {# <h4>header 4</h4> #}
            {{ wiki('*bold text*') }}           {# <b>bold text</b> #}
            {{ wiki('/italic text/') }}         {# <i>iatlic text</i> #}
            {{ wiki('{code text}') }}           {# <code>code text</code> #}
            {{ wiki('http://a/b') }}

        Formated pre code type could be python (/default if not set/),
        jinja, ini or text. Text type stops highlighting. Code type
        must be on first line with hashbang prefix like in example:

            #!text
            #!python
            # simple python example
            from poorwsgi import *

            @app.route('/')                         # uri /
            def root(req):
                return 'Hello world %s' % 1234      # return text

        Looks that:

            #!python
            # simple python example
            from poorwsgi import *

            @app.route('/')                         # uri /
            def root(req):
                return 'Hello world %s' % 1234      # return text

        Parameters padding:

            #!text
            This is some text, which could be little bit long. Never mind
            if text is on next line.
                parameter - some text for parameter
                parameter - some text for parameter

        Looks that:

        This is some text, which could be little bit long. Never mind if
        text is on next line.
            parameter - some text for parameter
            parameter - some text for parameter
        """
        if isinstance(doc, Undefined):      # template error
            return doc

        doc = re_amp.sub(r"&amp;", doc)
        doc = re_gt.sub(r"&gt;", doc)
        doc = re_lt.sub(r"&lt;", doc)

        # main tags (pre, code and br)
        doc = re_preauto.sub(_pre, doc)
        # sys.stdout.write("doc: %s\n" % doc)
        doc = re_notpre.sub(_not_in_pre, doc)

        # highlighting in pre tags
        doc = re_source.sub(_code, doc)

        # links
        doc = re_link.sub(r'<a href="\1">\1</a>', doc)

        doc = self.linked_api(doc)   # api keywords
        return _nlstrip(pep_rfc(doc))
Exemplo n.º 8
0
    def wiki(self,
             doc,
             link='link',
             top='top',
             name='__doc__',
             section_level=2,
             system_message=False):
        """
        Call some regular expressions on doc, and return it with html
        interpretation of wiki formating. If you want to create links to
        know api for your module, just call keywords function after gets
        full api documentation list.

            #!jinja
            {{ wiki(string) }}
            {{ wiki('=header1 =') }}            {# <h1>header 1</h1> #}
            {{ wiki('=header2 =') }}            {# <h2>header 2</h2> #}
            {{ wiki('=header3 =') }}            {# <h3>header 3</h3> #}
            {{ wiki('=header4 =') }}            {# <h4>header 4</h4> #}
            {{ wiki('*bold text*') }}           {# <b>bold text</b> #}
            {{ wiki('/italic text/') }}         {# <i>iatlic text</i> #}
            {{ wiki('{code text}') }}           {# <code>code text</code> #}
            {{ wiki('http://a/b') }}

        Formated pre code type could be python (/default if not set/),
        jinja, ini or text. Text type stops highlighting. Code type
        must be on first line with hashbang prefix like in example:

            #!text
            #!python
            # simple python example
            from poorwsgi import *

            @app.route('/')                         # uri /
            def root(req):
                return 'Hello world %s' % 1234      # return text

        Looks that:

            #!python
            # simple python example
            from poorwsgi import *

            @app.route('/')                         # uri /
            def root(req):
                return 'Hello world %s' % 1234      # return text

        Parameters padding:

            #!text
            This is some text, which could be little bit long. Never mind
            if text is on next line.
                parameter - some text for parameter
                parameter - some text for parameter

        Looks that:

        This is some text, which could be little bit long. Never mind if
        text is on next line.
            parameter - some text for parameter
            parameter - some text for parameter
        """
        if isinstance(doc, Undefined):  # template error
            return doc

        doc = re_amp.sub(r"&amp;", doc)
        doc = re_gt.sub(r"&gt;", doc)
        doc = re_lt.sub(r"&lt;", doc)

        # main tags (pre, code and br)
        doc = re_preauto.sub(_pre, doc)
        # sys.stdout.write("doc: %s\n" % doc)
        doc = re_notpre.sub(_not_in_pre, doc)

        # highlighting in pre tags
        doc = re_source.sub(_code, doc)

        # links
        doc = re_link.sub(r'<a href="\1">\1</a>', doc)

        doc = self.linked_api(doc)  # api keywords
        return _nlstrip(pep_rfc(doc))