예제 #1
0
def render_example(site, kw, in_names, out_name):
    """Render a set of .h and .cpp files to HTML with formatting.

    Parameters
    ==========
    site:
        An instance of a Nikola site, available in any plugin as ``self.site``
    kw:
        A dictionary of keywords for this task
    in_names:
        The files to be rendered, as a list of pathlib.Path objects
    out_name:
        A pathlib.Path instance pointing to the rendered output file

    """
    items = []
    for source_file in in_names:
        code = highlight(source_file.read_bytes(), CppLexer(),
                         utils.NikolaPygmentsHTML(source_file.name))
        items.append({
            "code": code,
            "title": source_file.name,
            "source_link": source_file.name,
        })

    context = {
        "items": items,
        "title": out_name,
        "lang": kw["default_lang"],
        "pagekind": ["example"],
    }
    site.render_template("multifile-example.tmpl", str(out_name), context)
예제 #2
0
 def GetCodeElement(self, code, style):
   if PYGMENTS_IMPORTED:
     formatter = HtmlFormatter(linenos=True,
             style=pygments.styles.get_style_by_name(style))
     return pygments.highlight(code, CppLexer(), formatter)
   else:
     return '<pre>Pygments highlighter not installed</pre>'
예제 #3
0
    def tesseract2(self, button, text, chooser, lang):
        file = chooser.get_filenames()
        str = ''.join(file)

        #code = img_prcs.image_enhance(str)
        code = image_to_string(Image.open(str))
        text_buffer = text.get_buffer()

        copy = code.replace('\n', ' ')
        lex = guess_lexer(copy)
        print lex
        if isinstance(lex, PythonLexer) == True:
            lang.set_active(3)
            html = highlight(code, PythonLexer(), HtmlFormatter())

        elif isinstance(lex, ArduinoLexer) == True or isinstance(
                lex, TextLexer) or isinstance(
                    lex, LassoCssLexer) or isinstance(lex, CppLexer) == True:
            lang.set_active(1)
            html = highlight(code, CppLexer(), HtmlFormatter())

        htmlfile = open("html_file.html", "w")
        htmlfile.write("%s" % html)
        htmlfile.close()
        # instantiate the parser and fed it some HTML
        parser = MyHTMLParser()
        parser.main(text_buffer)
        parser.feed(html)
        '''
예제 #4
0
    def getLexer(self):
        languageStr = gdb.execute('show language', False, True)

        if languageStr.find('c++') != -1:
            return CppLexer()
        else:
            return TextLexer()
예제 #5
0
    def update_syntax(self, s):

        if not _highlight_available:
            return

        cur = self.code_input.text.textCursor()
        cur_pos = cur.position()
        text = self.code_input.text.toPlainText()
        if len(text) > 1:
            cur_char = text[cur_pos - 1]
        cur_char = ''

        if cur_char == u'\n':
            s += ' '

        tab_added = False
        if cur_char == u'\t':
            tab_added = True
            s = s.replace('\t', '    ')

        if self.language == "python":
            html_result = highlight(s, PythonLexer(), HtmlFormatter(full=True))
        else:
            html_result = highlight(s, CppLexer(), HtmlFormatter(full=True))

        self.code_input.text.blockSignals(True)
        self.code_input.text.setHtml(html_result)
        self.code_input.text.blockSignals(False)

        if tab_added:
            cur.setPosition(cur_pos + 3)
        else:
            cur.setPosition(cur_pos)

        self.code_input.text.setTextCursor(cur)
예제 #6
0
class PygmentsPreviewer(Previewer):
    # All supported MIME types
    MIMETYPES = ('text/css', 'text/x-python', 'text/x-ruby-script', 'text/x-java-source', 'text/x-c',
                 'application/javascript', 'text/x-c', 'text/x-fortran', 'text/x-csharp', 'text/php',
                 'text/x-php')

    # Python's mimetypes lib and Pygments do not quite agree on some MIME types
    CUSTOM_LEXERS = {
        'text/x-c': CppLexer(),
        'text/x-java-source': JavaLexer(),
        'text/x-ruby-script': RubyLexer(),
        'text/php': PhpLexer()
    }

    @classmethod
    def can_preview(cls, attachment_file):
        return attachment_file.content_type in cls.MIMETYPES

    @classmethod
    def generate_content(cls, attachment):
        mime_type = attachment.file.content_type

        lexer = cls.CUSTOM_LEXERS.get(mime_type)
        if lexer is None:
            lexer = get_lexer_for_mimetype(mime_type)

        with attachment.file.open() as f:
            html_formatter = HtmlFormatter(style='tango', linenos='inline', prestyles='mono')
            html_code = highlight(f.read(), lexer, html_formatter)

        css_code = html_formatter.get_style_defs('.highlight')

        return render_template('previewer_code:pygments_preview.html', attachment=attachment,
                               html_code=html_code, css_code=css_code)
예제 #7
0
 def get_tokens_unprocessed(self, text):
     for index, token, value in CppLexer.get_tokens_unprocessed(self, text):
         if value in self.coro_keywords:
             yield index, Keyword, value
         elif token is Name.Function:
             yield index, Name, value
         else:
             yield index, token, value
class CppTest(unittest.TestCase):
    def setUp(self):
        self.lexer = CppLexer()

    def testGoodComment(self):
        fragment = u'/* foo */\n'
        tokens = [
            (Token.Comment.Multiline, u'/* foo */'),
            (Token.Text, u'\n'),
        ]
        self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))

    def testOpenComment(self):
        fragment = u'/* foo\n'
        tokens = [
            (Token.Comment.Multiline, u'/* foo\n'),
        ]
        self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))
def highlight_url(request):
    """Retrieves a generated source file and syntax highlights it

    Some stack frames are functions that are generated during the build process. Thus the stack
    frame itself isn't particularly helpful since the generated source file isn't available
    anywhere.

    Bug 1389217 and friends adjust the build process to capture the generated source and push it to
    S3.

    This view takes a URL for the generated source, retrieves it from S3, runs it through syntax
    highlighting, and returns that as an HTML page.

    NOTE(willkg): The output of pygments has CSS in the page, but no JS.

    """
    url = request.GET.get('url')

    if not url:
        return http.HttpResponseBadRequest('No url specified.')

    parsed = urlparse(url)

    # We will only pull urls from allowed hosts
    if parsed.netloc not in ALLOWED_SOURCE_HOSTS:
        return http.HttpResponseForbidden('Document at disallowed host.')

    if parsed.scheme not in ALLOWED_SCHEMES:
        return http.HttpResponseForbidden('Document at disallowed scheme.')

    resp = requests.get(url)
    if resp.status_code != 200:
        return http.HttpResponseNotFound('Document at URL does not exist.')

    filename = parsed.path.split('/')[-1]
    if filename.endswith('.h'):
        # Pygments will default to C which we don't want, so override it here.
        lexer = CppLexer()
    else:
        lexer = get_lexer_for_filename(filename)

    lines = []
    if request.GET.get('line'):
        try:
            lines = [int(request.GET.get('line'))]
        except ValueError:
            pass

    formatter = HtmlFormatter(
        full=True,
        title=parsed.path,
        linenos='table',
        lineanchors='L',
        hl_lines=lines,
    )
    return http.HttpResponse(highlight(resp.text, lexer, formatter),
                             content_type='text/html')
    def testC(self):
        """ Does the CompletionLexer work for C/C++?
        """
        lexer = CompletionLexer(CLexer())
        self.assertEquals(lexer.get_context("foo.bar"), [ "foo", "bar" ])
        self.assertEquals(lexer.get_context("foo->bar"), [ "foo", "bar" ])

        lexer = CompletionLexer(CppLexer())
        self.assertEquals(lexer.get_context("Foo::Bar"), [ "Foo", "Bar" ])
예제 #11
0
 def choose_lexer(filename):
     fs = FileSystem()
     filetype = fs.getFileType(filename)
     if filetype == 'py':
         TextEditor.lex = PythonLexer()
     elif filetype == 'java':
         TextEditor.lex = JavaLexer()
     elif filetype == 'cpp':
         TextEditor.lex = CppLexer()
     TextEditor.codeinput.lexer = TextEditor.lex
예제 #12
0
def highlight_cpp(code: str):
    """Highlight the given C/C++ source code with pygments."""
    from IPython.display import HTML, display
    from pygments import highlight
    # noinspection PyUnresolvedReferences
    from pygments.formatters import HtmlFormatter
    # noinspection PyUnresolvedReferences
    from pygments.lexers import CppLexer

    css = HtmlFormatter().get_style_defs('.highlight')
    css_tag = f"<style>{css}</style>"
    display(HTML(css_tag))
    return HTML(highlight(code, CppLexer(), HtmlFormatter()))
예제 #13
0
 def choose_lexer(self):
     fs = FileSystem()
     if TextEditor.filepath != None:
         ext = fs.getFileType(TextEditor.filepath)
         if ext == 'py':
             TextEditor.lex = PythonLexer()
         elif ext == 'java':
             TextEditor.lex = JavaLexer()
         elif ext == 'cpp':
             TextEditor.lex = CppLexer()
     else:
         # No extension
         TextEditor.lex = PythonLexer()
예제 #14
0
파일: Annotate.py 프로젝트: vladima/cython
    def _htmlify_code(self, code, language):
        try:
            from pygments import highlight
            from pygments.lexers import CythonLexer, CppLexer
            from pygments.formatters import HtmlFormatter
        except ImportError:
            # no Pygments, just escape the code
            return html_escape(code)

        if language == "cython":
            lexer = CythonLexer(stripnl=False, stripall=False)
        elif language == "c/cpp":
            lexer = CppLexer(stripnl=False, stripall=False)
        else:
            # unknown language, use fallback
            return html_escape(code)
        html_code = highlight(code, lexer, HtmlFormatter(nowrap=True))
        return html_code
예제 #15
0
    def expand(self, parameter, remark):
        # Extract the documentation for types and functions.
        text = self._extractDocumentation(parameter)
        
        # Hilight the text.
        hilightedText = highlight('\n'.join(parameter), CppLexer(), HtmlFormatter())

        # Prepare for Remark output.
        hilightedText = hilightedText.split('\n')

        # Copy the source and replace the includes with links.
        includeRegex = re.compile(r'(#include[ \t]+(?:(?:&quot)|(?:&lt));)(.*)((?:(?:&quot)|(?:&gt));)')
        replacer = lambda match: self._linkConverter(match, remark)

        for line in hilightedText:
            # Replace include file names with links to source files.
            text.append(re.sub(includeRegex, replacer, line))
        
        return htmlRegion(text)
예제 #16
0
def include_code(context, code):
    if not code:
        return "NULL"
    import urllib2
    code = urllib2.urlopen("file://" + code.codefile.path).read()
    from pygments import highlight
    from pygments.lexers import CppLexer, JavaLexer, HaskellLexer
    from pygments.formatters import HtmlFormatter
    return highlight(
        code, CppLexer(),
        HtmlFormatter(
            style='vimlight',
            full=True,
            linenos='table',
            lineanchors='Line',
            anchorlinenos=True,
            noclobber_cssfile=True,
            nobackground=False,
        ))
def render_example(site, kw, in_name, out_name):
    """Render a single source file to HTML with formatting.

    Parameters
    ==========
    site:
        An instance of a Nikola site, available in any plugin as ``self.site``
    kw:
        A dictionary of keywords for this task
    in_name:
        The file to be rendered, as an instance of pathlib.Path
    out_name:
        A pathlib.Path instance pointing to the rendered output file

    """
    if str(in_name).endswith('.cpp'):
        lexer = CppLexer()
    elif str(in_name).endswith('.f'):
        lexer = FortranFixedLexer()
    else:
        lexer = FortranLexer()
    code = highlight(
        in_name.read_bytes(), lexer, utils.NikolaPygmentsHTML(in_name.name)
    )

    title = in_name.name
    permalink = out_name.relative_to(kw["output_folder"])
    source_link = permalink.stem  # remove '.html'
    context = {
        "code": code,
        "title": title,
        "permalink": str(permalink),
        "lang": kw["default_lang"],
        "description": title,
        "source_link": source_link,
        "pagekind": ["example"],
    }
    site.render_template("examples.tmpl", str(out_name), context)
예제 #18
0
파일: retdec.py 프로젝트: s0i37/SBA
import r2pipe
from pygments import highlight
from pygments.lexers import CppLexer
from pygments.formatters import Terminal256Formatter
from os import popen, unlink
from tempfile import NamedTemporaryFile
import random
import string

DECOMPILER = 'retdec-decompiler.py'

r2 = r2pipe.open()
arch = 'x86' if r2.cmd("e asm.bits") == '32' else 'x86-64'
vma = r2.cmd("?v $$").strip()
with NamedTemporaryFile() as blob:
    exec(r2.cmd("pcp $FS"))
    blob.write(buf)
    blob.flush()
    with NamedTemporaryFile() as output_file:
        retdec = popen(
            "{decompiler} -l py -m raw -e little -a {arch} --raw-entry-point=0 --raw-section-vma={vma} -o {out_file} {blob}"
            .format(decompiler=DECOMPILER,
                    arch=arch,
                    vma=vma,
                    out_file=output_file.name,
                    blob=blob.name))
        retdec.read()
        print highlight(output_file.read(), CppLexer(),
                        Terminal256Formatter(style='pastie'))
        for ext in ['.bc', '.dsm', '.json', '.ll']:
            unlink(output_file.name + ext)
예제 #19
0
파일: repl.py 프로젝트: yarwelp/inspector
 def highlight(self, lines):
     tokens = CppLexer().get_tokens("\n".join(lines))
     source = pygments.format(tokens, TerminalFormatter())
     return source.split("\n")
예제 #20
0
파일: snow.py 프로젝트: s0i37/SBA
#!/usr/bin/python
import r2pipe
from pygments import highlight
from pygments.lexers import CppLexer
from pygments.formatters import Terminal256Formatter
from os import popen

r2 = r2pipe.open()
filepath = r2.cmdj("ij")['core']['file']
from_addr = r2.cmd("?v $FB")
to_addr = r2.cmd("?v $FE")

nocode = popen("nocode --from=%s --to=%s %s" % (from_addr, to_addr, filepath))

print highlight(nocode.read(), CppLexer(),
                Terminal256Formatter(style='monokai'))
예제 #21
0
파일: gen_docs.py 프로젝트: kshyatt/website
 def block_code(self, code, lang):
     lexer = CppLexer()
     formatter = HtmlFormatter()
     return highlight(code, lexer, formatter)
예제 #22
0
			<META http-equiv=Content-Type content="text/html; charset=UTF-8">
			<title>%s</title>
			<style type="text/css">
			%s
			</style>
		</head>
		<body>
			<h2>%s</h2>
			%s
		</body>
	</html>	
	''' % (co, sty, co, bo)


FileExistNames = os.listdir('lintcode/')

for codeName in FileExistNames:
    code = open('lintcode/' + codeName, 'r').read()

    htmlFile = open('lintcodeHTML/' + codeName + '.html', 'w')

    formatter = HtmlFormatter(encoding='utf-8',
                              style=available_styles[2],
                              linenos=True)
    style = formatter.get_style_defs()
    body = highlight(code, CppLexer(), formatter)
    try:
        htmlFile.writelines(generate_html(codeName, style, body))
    finally:
        htmlFile.close()
예제 #23
0
options_menu.add_cascade(label=lang['options'][0], menu=font_menu)
options_menu.add_cascade(label=lang['options'][1], menu=font_size_menu)
options_menu.add_cascade(label=lang['options'][2], menu=style_menu)
options_menu.add_cascade(label=lang['options'][3], menu=syntax_menu)
options_menu.add_cascade(label=lang['options'][4], menu=music_menu)
options_menu.add_separator()
options_menu.add_command(label=lang['options'][5], command=textbox.scrap_page)
options_menu.add_command(label=lang['options'][6], command=textbox.highlight_all)

style_menu.add_command(label=lang['style'][0], command=lambda: textbox.tagger('bold'))
style_menu.add_command(label=lang['style'][1], command=lambda: textbox.tagger('italic'))
style_menu.add_command(label=lang['style'][2], command=lambda: textbox.tagger('underline'))

syntax_menu.add_command(label='Python 3', command=lambda: textbox.set_lexer(Python3Lexer()))
syntax_menu.add_command(label='C/C++', command=lambda: textbox.set_lexer(CppLexer()))
syntax_menu.add_command(label='C#', command=lambda: textbox.set_lexer(CSharpLexer()))
syntax_menu.add_command(label='Java', command=lambda: textbox.set_lexer(JavaLexer()))
syntax_menu.add_command(label='Rust', command=lambda: textbox.set_lexer(RustLexer()))
syntax_menu.add_command(label='Go', command=lambda: textbox.set_lexer(GoLexer()))
syntax_menu.add_command(label='HTML', command=lambda: textbox.set_lexer(HtmlLexer()))
syntax_menu.add_command(label='CSS', command=lambda: textbox.set_lexer(CssLexer()))
syntax_menu.add_command(label='Javascript', command=lambda: textbox.set_lexer(JavascriptLexer()))
syntax_menu.add_command(label='PHP', command=lambda: textbox.set_lexer(PhpLexer()))
syntax_menu.add_command(label='SQL', command=lambda: textbox.set_lexer(SqlLexer()))
syntax_menu.add_command(label='Batch', command=lambda: textbox.set_lexer(BatchLexer()))
syntax_menu.add_command(label='Bash', command=lambda: textbox.set_lexer(BashLexer()))
syntax_menu.add_command(label='Markdown', command=lambda: textbox.set_lexer(MarkdownLexer()))

for font_name in settings["fonts"]:
	font_menu.add_command(label=font_name, command=lambda font_name=font_name: textbox.change_font(font_name, 0))
예제 #24
0
 def setUp(self):
     self.lexer = CppLexer()
예제 #25
0
            for token in tokens:
                if token.startswith("}"):
                    active.remove(token[1:])
        else:
            if "JCB{" in l:
                vis = 0
            if "}JCB" in l:
                vis = 1
    return dict([(k, "".join(unindent(v))) for (k, v) in d.items()])


sources = """
termdriver-helloworld
termdriver-counter1
termdriver-counter2
termdriver-color1
"""

for fn in sources.split():
    code = Clean(open("samples/%s.ino" % fn))
    for (tag, body) in code.items():
        hh = open("%s-%s.inc" % (fn, tag), "w")
        hh.write(highlight(body, CppLexer(), LatexFormatter()))
        hh.close()

if __name__ == "__main__":
    for f in sources.split():
        ino = open("samples/" + f + ".ino", "rt").read()
        m = highlight(ino, CppLexer(), LatexFormatter())
        open("code/" + f + ".inc", "wt").write(m)
예제 #26
0
    def OnCommand(self, n, cmd_id):
        if cmd_id == self.cmd_show_reasons:
            match = self.items[n]
            reasons = match[len(match) - 1]
            msg = "\n".join(reasons)
            info(msg)
        elif cmd_id == self.cmd_show_source:
            item = self.items[n]
            src_id = int(item[1])
            cur = self.importer.db.cursor()
            sql = "select source from src.functions where id = ?"
            cur.execute(sql, (src_id, ))
            row = cur.fetchone()
            if row is not None:
                fmt = HtmlFormatter()
                fmt.noclasses = True
                fmt.linenos = True
                func = row["source"]
                src = highlight(func, CppLexer(), fmt)
                title = "Source code of %s" % repr(item[2])
                cdiffer = CHtmlViewer()
                cdiffer.Show(src, title)
            cur.close()
        elif cmd_id == self.cmd_import_all:
            if askyn_c(
                    0,
                    "HIDECANCEL\nDo you really want to import all matched functions as well as struct, union, enum and typedef definitions?"
            ) == 1:
                import_items = []
                for item in self.items:
                    src_id, src_name, bin_ea = int(item[1]), item[2], int(
                        item[3], 16)
                    import_items.append([src_id, src_name, bin_ea])

                self.importer.import_items(import_items)
        elif cmd_id == self.cmd_import_selected:
            if len(self.selected_items) == 1 or askyn_c(
                    1,
                    "HIDECANCEL\nDo you really want to import the selected functions?"
            ) == 1:
                import_items = []
                for index in self.selected_items:
                    item = self.items[index]
                    src_id, src_name, bin_ea = int(item[1]), item[2], int(
                        item[3], 16)
                    import_items.append([src_id, src_name, bin_ea])

                import_definitions = askyn_c(
                    0,
                    "HIDECANCEL\nDo you also want to import all struct, union, enum and typedef definitions?"
                ) == 1
                self.importer.import_items(
                    import_items, import_definitions=import_definitions)
        elif cmd_id == self.cmd_diff_c:
            html_diff = CHtmlDiff()
            item = self.items[n]

            src_id = long(item[1])
            cur = self.differ.db.cursor()

            sql = "select source from src.functions where id = ?"
            cur.execute(sql, (src_id, ))
            row = cur.fetchone()
            cur.close()
            if not row:
                Warning("Cannot find the source function.")
                return False

            ea = long(item[3], 16)
            proto = self.differ.decompile_and_get(ea)
            if not proto:
                Warning("Cannot decompile function 0x%08x" % ea)
                return False

            buf1 = indent_source(row[0].decode("utf-8", "ignore"))
            buf2 = proto
            buf2 += u"\n".join(self.differ.pseudo[ea])
            new_buf = indent_source(buf2)
            src = html_diff.make_file(new_buf.split(u"\n"), buf1.split(u"\n"))

            title = "Diff pseudo-source %s - %s" % (item[2], item[4])
            cdiffer = CHtmlViewer()
            cdiffer.Show(src, title)
예제 #27
0
#!/usr/bin/python

import sys
import subprocess
import re
import time
from termcolor import colored
import math
import pygments
from pygments.lexers import CppLexer, PythonLexer, JavaLexer
from pygments.formatters import TerminalFormatter

cpp_lexer = CppLexer()
python_lexer = PythonLexer()
java_lexer = JavaLexer()

terminal_formatter = TerminalFormatter()


def colorize_cpp(line):
    return pygments.highlight(code, cpp_lexer,
                              terminal_formatter).rstrip("\r\n")


def colorize_python(line):
    return pygments.highlight(code, python_lexer,
                              terminal_formatter).rstrip("\r\n")


def colorize_java(line):
    return pygments.highlight(code, java_lexer,
예제 #28
0
파일: color-c.py 프로젝트: zzz9328fb/retdec
    sys.exit(1)

inConfigName = sys.argv[2]
with open(inConfigName, 'r') as inConfigFile:
    data = json.load(inConfigFile)
for f in data.get('functions', []):
    if (f.get('fncType') == 'userDefined'):
        userFunctionList.append(f.get('name'))
    else:
        externalFunctionList.append(f.get('name'))
for g in data.get('globals', []):
    globalVariableList.append(g.get('name'))

inFileName = sys.argv[1]
with open(inFileName, 'r') as f:
    code = f.read()
code = highlight(code, CppLexer(), IDAFormatter())

# This comes in handy when we want to find out token name for same particular lexeme.
# It just dumps all the lexemes and their token names that can be used in TERMINAL_COLORS mapping.
#
#code = highlight(code, CppLexer(), RawTokenFormatter())

# Enable only during debugging.
#
#print(code)

outFileName = inFileName if inSitu else inFileName + extension
with open(outFileName, 'w') as f:
    f.write(code)
예제 #29
0
 def GetCodeElement(self, code, style):
     formatter = HtmlFormatter(
         linenos=True, style=pygments.styles.get_style_by_name(style))
     return pygments.highlight(code, CppLexer(), formatter)
예제 #30
0
 def load_cpp_syntax(self):
     self.master.lexer = CppLexer()
     self.master.initial_highlight()
예제 #31
0
 def highlight(src):
     return pygments.highlight(src, CppLexer(), HtmlFormatter())