def __init__(self, names, tokentype, names2, tokentype2, names3,
              tokentype3):
     NameHighlightFilter.__init__(self, names=names, tokentype=tokentype)
     self.names2 = names2
     self.tokentype2 = tokentype2
     self.names3 = names3
     self.tokentype3 = tokentype3
Exemple #2
0
def add_tokens(tokens):
    """Register additional `tokens` to add custom syntax highlighting.

    `tokens` should be a dictionary, whose keys indicate a type of token and
    whose values are lists of strings to highlight with that token type.

    This is particularly useful to highlight custom tactics or symbols.  For
    example, if your code defines a tactic ``map_eq`` to decide map equalities,
    and two tactics ``map_simplify`` and ``map_subst`` to simplify map
    expressions, you might write the following:

    >>> add_tokens({
    ...     'tacn-solve': ['map_eq'],
    ...     'tacn': ['map_simplify', 'map_subst']
    ... })
    """
    filters = []
    for kind, names in tokens.items():
        tokentype = LEXER.TOKEN_TYPES.get(kind)
        if not tokentype:
            raise ValueError("Unknown token kind: {}".format(kind))
        filters.append(NameHighlightFilter(names=names, tokentype=tokentype))
    for f in filters:
        LEXER.add_filter(f)
    return filters
Exemple #3
0
    def display(self, stype, mode='raw', oformat='term'):
        """
        Display output for a single match

        :param stype: name of the matched type
        :type stype: str
        :param mode: display mode
        :type mode: str
        :param oformat: format of output for color (term, html)
        :type oformat: str
        :return: a human readable string containing the result of the search
                 (matched line, context, file name, etc.)
        """
        f = open(self.file, 'r')
        lines = f.readlines()
        pmatch = lines[self.line - 1][self.column:self.columnend]
        ptype = "*"  # match is a pointer to struct
        if (CocciMatch.ptype_regexp.search(lines[self.line -
                                                 1][self.columnend:])):
            ptype = ""
        output = ""
        if mode == 'color':
            output += "%s: l.%s -%d, l.%s +%d, %s %s%s\n" % (
                self.file, self.line, self.line - self.start_at, self.line,
                self.stop_at - self.line, stype, ptype, pmatch)
        for i in range(self.start_at - 1, min(self.stop_at, len(lines))):
            if mode == 'color':
                output += lines[i]
            elif mode == 'vim':
                output += "%s|%s| (%s %s%s): %s" % (self.file, i + 1, stype,
                                                    ptype, pmatch, lines[i])
            elif mode == 'emacs':
                output += "%s:%s: (%s %s%s): %s" % (self.file, i + 1, stype,
                                                    ptype, pmatch, lines[i])
            elif i == self.line - 1:
                output += "%s:%s (%s %s%s): %s" % (self.file, i + 1, stype,
                                                   ptype, pmatch, lines[i])
            else:
                output += "%s-%s %s - %s" % (self.file, i + 1, ' ' *
                                             (2 + len(stype + ptype + pmatch)),
                                             lines[i])
        f.close()
        if mode == 'color':
            if have_pygments:
                lexer = CLexer()
                lfilter = NameHighlightFilter(names=[pmatch])
                lexer.add_filter(lfilter)
                if oformat == "term":
                    return highlight(output, lexer, Terminal256Formatter())
                elif oformat == "html":
                    return highlight(output, lexer,
                                     HtmlFormatter(noclasses=True))
                else:
                    return output
        return output + self.trailer
Exemple #4
0
def setup(app):
    from sphinx.domains.python import PyField
    from sphinx.util.docfields import Field
    app.add_css_file('/source/_static/custom.css')
    app.add_stylesheet('/source/_static/theme_override.css')
    app.add_object_type('confval',
                        'confval',
                        objname='configuration value',
                        indextemplate='pair: %s; configuration value',
                        doc_field_types=[
                            PyField('type',
                                    label=_('Type'),
                                    has_arg=False,
                                    names=('type', ),
                                    bodyrolename='class'),
                            Field(
                                'default',
                                label=_('Default'),
                                has_arg=False,
                                names=('default', ),
                            ),
                        ])
    # Patch the MATLAB lexer to correct wrong highlighting
    from sphinx.highlighting import lexers
    from pygments.lexers import MatlabLexer
    from pygments.token import Name
    from pygments.filters import NameHighlightFilter
    matlab_lexer = MatlabLexer()
    matlab_lexer.add_filter(
        NameHighlightFilter(
            names=[
                'function', 'end', 'if', 'else', 'elseif', 'switch', 'case',
                'return', 'otherwise'
            ],
            tokentype=Name.Function,
        ))
    app.add_lexer('matlab', matlab_lexer)
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import TerminalFormatter
from pygments.filters import NameHighlightFilter

code = """
for i in range(1, 11):
    print("Hello world!")
if x and y:
    print("yes")
if x or y:
    print("dunno")
if x xor y:
    print("different")
goto 10
"""


print(highlight(code, PythonLexer(), TerminalFormatter()))

print("-----------------------")

lexer = PythonLexer()

# pridani filtru
lexer.add_filter(NameHighlightFilter(names=["xor", "goto"]))

print(highlight(code, lexer, TerminalFormatter()))
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import TerminalFormatter
from pygments.filters import NameHighlightFilter

code = """
for i in range(1, 11):
    print("Hello world!")
if x and y:
    print("yes")
if x or y:
    print("dunno")
if x xor y:
    print("different")
goto 10
"""

print(highlight(code, PythonLexer(), TerminalFormatter()))

print("-----------------------")

lexer = PythonLexer()

# pridani filtru
lexer.add_filter(NameHighlightFilter(names=['xor', 'goto']))

print(highlight(code, lexer, TerminalFormatter()))
# Add name -> formatter pairs for every variant you want to use
VARIANTS = {
    # 'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True),
}


from docutils import nodes
from docutils.parsers.rst import directives, Directive

from pygments import highlight
from pygments.lexers import get_lexer_by_name, TextLexer
from pygments.filters import NameHighlightFilter
from pygments.token import Name

ctypes_types_highlighter = NameHighlightFilter(names=['c_int', 'c_void_p'],
                                               tokentype=Name.Function)

custom_highlighters = NameHighlightFilter(names=['restype', 'argtypes'],
                                          tokentype=Name.String)

class Pygments(Directive):
    """ Source code syntax hightlighting.
    """
    required_arguments = 1
    optional_arguments = 0
    final_argument_whitespace = True
    option_spec = dict([(key, directives.flag) for key in VARIANTS])
    has_content = True

    def run(self):
        self.assert_has_content()
 def __init__(self, names, tokentype, names2, tokentype2, names3, tokentype3):
     NameHighlightFilter.__init__(self, names=names, tokentype=tokentype)
     self.names2 = names2
     self.tokentype2 = tokentype2
     self.names3 = names3
     self.tokentype3 = tokentype3
Exemple #9
0
def query():
    error = None
    if request.method == 'GET':
        if not request.args.get('url'):
            error = 'No URL'
        elif not request.args.get('query_string'):
            error = 'No query string'
        else:
            # url = request.args.get('url')
            # query_string = request.args.get('query_string')
            # flash('query!!')
            # html = rst.urlopen(url).read()
            with open('test.html') as reader:
                html = reader.read()
            soup = BeautifulSoup(html, 'html.parser')

            results = soup.find_all('title')
            mark = 'QUERY' + 100 * '-'
            highlight_result_lst = []
            for result in results:
                # hltag = soup.new_tag(mark)
                # hltag.attrs.setdefault('class', mark)
                # result.wrap(hltag)
                # classes = result.attrs.setdefault('class', mark)
                # if isinstance(classes, list) and mark not in classes:
                #     classes.append(mark)
                # elif not isinstance(classes, list) and classes!=mark:
                #     classes = mark
                highlight_result = highlight(
                    BeautifulSoup(str(result), 'html.parser').prettify(),
                    HtmlLexer(), HtmlFormatter())
                print(highlight_result)
                highlight_result = highlight_result.partition(
                    '<div class="highlight"><pre>')[2]
                print(highlight_result)
                print(highlight_result.rpartition('</pre></div>'))
                highlight_result = highlight_result.rpartition(
                    '</pre></div>')[0]
                # highlight_result = BeautifulSoup(highlight_result, 'html.parser').prettify()
                print(highlight_result)
                highlight_result_lst.append(highlight_result)

            html = soup.prettify()
            html_lexer = HtmlLexer()
            namefilter = NameHighlightFilter(names=[mark])
            html_lexer.add_filter(namefilter)
            highlight_html = highlight(html, html_lexer, HtmlFormatter())

            print()
            print()
            print()
            print()
            print()

            print(highlight_html)

            for highlight_result in highlight_result_lst:
                highlight_html = highlight_html.replace(
                    highlight_result, '222' + highlight_result + '222')

            return render_template('query.html', html=Markup(highlight_html))

    return render_template('query.html', error=error)