def __init__(self, **kwargs): self.formatter = BBCodeFormatter() self.lexer = lexers.PythonLexer() self.text_color = '#000000' self._label_cached = Label() self.use_text_color = True super(CodeInput, self).__init__(**kwargs) self._line_options = kw = self._get_line_options() self._label_cached = Label(**kw) # use text_color as foreground color text_color = kwargs.get('foreground_color') if text_color: self.text_color = get_hex_from_color(text_color) # set foreground to white to allow text colors to show # use text_color as the default color in bbcodes self.use_text_color = False self.foreground_color = [1, 1, 1, .999] if not kwargs.get('background_color'): self.background_color = [.9, .92, .92, 1]
def test_basic(self): filter_args = { 'whitespace': { 'spaces': True, 'tabs': True, 'newlines': True }, 'highlight': { 'names': ['isinstance', 'lexers', 'x'] }, } for x in filters.FILTERS.keys(): lx = lexers.PythonLexer() lx.add_filter(x, **filter_args.get(x, {})) text = open(TESTFILE, 'rb').read().decode('utf-8') tokens = list(lx.get_tokens(text)) roundtext = ''.join([t[1] for t in tokens]) if x not in ('whitespace', 'keywordcase'): # these filters change the text self.assertEquals(roundtext, text, "lexer roundtrip with %s filter failed" % x)
def report(self, line, cell): """Renders the named template. Syntax: %%report results_var template_name """ var_name = line template_name = cell template = self.shell.ev(template_name) results = self.shell.ev(var_name) source = self.shell.user_ns['submission_source'].source formatted_source = pygments.highlight( source, lexers.PythonLexer(), formatters.HtmlFormatter()).rstrip() # Render the template giving the specified variable as 'results', # and render the result as inlined HTML in cell output. 'source' is # the prerendered source code. return display.HTML( template.render(results=results, source=source, formatted_source=formatted_source))
class CodeHighlighter(EncodeUnicode): ''' The CodeHighlighter filter depends on the "pygments" module which you can download and install from: http://pygments.org What the CodeHighlighter assumes the string that it's receiving is source code and uses pygments.lexers.guess_lexer() to try to guess which parser to use when highlighting it. CodeHighlighter will return the HTML and CSS to render the code block, syntax highlighted, in a browser NOTE: I had an issue installing pygments on Linux/amd64/Python 2.6 dealing with importing of pygments.lexers, I was able to correct the failure by adding: raise ImportError to line 39 of pygments/plugin.py (since importing pkg_resources was causing issues) ''' def filter(self, source, **kwargs): encoded = super(CodeHighlighter, self).filter(source, **kwargs) try: from pygments import highlight from pygments import lexers from pygments import formatters except ImportError, ex: print('<%s> - Failed to import pygments! (%s)' % (self.__class__.__name__, ex)) print('-- You may need to install it from: http://pygments.org') return encoded lexer = None try: lexer = lexers.guess_lexer(source) except lexers.ClassNotFound: lexer = lexers.PythonLexer() formatter = formatters.HtmlFormatter(cssclass='code_highlighter') encoded = highlight(encoded, lexer, formatter) css = formatter.get_style_defs('.code_highlighter') return '''<style type="text/css"><!-- %(css)s --></style>%(source)s''' % {'css' : css, 'source' : encoded}
def test_basic(self): filters_args = [ ('whitespace', { 'spaces': True, 'tabs': True, 'newlines': True }), ('whitespace', { 'wstokentype': False, 'spaces': True }), ('highlight', { 'names': ['isinstance', 'lexers', 'x'] }), ('codetagify', { 'codetags': 'API' }), ('keywordcase', { 'case': 'capitalize' }), ('raiseonerror', {}), ('gobble', { 'n': 4 }), ('tokenmerge', {}), ] for x, args in filters_args: lx = lexers.PythonLexer() lx.add_filter(x, **args) with open(TESTFILE, 'rb') as fp: text = fp.read().decode('utf-8') tokens = list(lx.get_tokens(text)) assert all(isinstance(t[1], str) for t in tokens), \ '%s filter did not return Unicode' % x roundtext = ''.join([t[1] for t in tokens]) if x not in ('whitespace', 'keywordcase', 'gobble'): # these filters change the text assert roundtext == text, \ "lexer roundtrip with %s filter failed" % x
def filter(self, source, **kwargs): encoded = super(CodeHighlighter, self).filter(source, **kwargs) try: from pygments import highlight from pygments import lexers from pygments import formatters except ImportError as ex: print('<%s> - Failed to import pygments! (%s)' % (self.__class__.__name__, ex)) print('-- You may need to install it from: http://pygments.org') return encoded lexer = None try: lexer = lexers.guess_lexer(source) except lexers.ClassNotFound: lexer = lexers.PythonLexer() formatter = formatters.HtmlFormatter(cssclass='code_highlighter') encoded = highlight(encoded, lexer, formatter) css = formatter.get_style_defs('.code_highlighter') return '''<style type="text/css"><!-- %(css)s --></style>%(source)s''' % {'css' : css, 'source' : encoded}
def pygmentize(value): last_end = 0 to_return = '' found = 0 for match_obj in regex.finditer(value): code_class = match_obj.group(1) code_string = match_obj.group(2) if code_class.find('class'): language = re.split(r'"|\'', code_class)[1] lexer = lexers.get_lexer_by_name(language) else: try: lexer = lexers.guess_lexer(str(code)) except ValueError: lexer = lexers.PythonLexer() pygmented_string = pygments.highlight(code_string, lexer, formatters.HtmlFormatter()) to_return = to_return + value[last_end:match_obj. start(0)] + pygmented_string last_end = match_obj.end(2) found = found + 1 to_return = to_return + value[last_end:] return to_return
def wrapper(*args, **kwargs): """Pretty-print container types and syntax-highlight command results.""" from .prompt import g out = f(*args, **kwargs) if out is None: return if g.style is None: return out printable = repr(out) lexer = lexers.PythonLexer() try: serialized = json.loads(out) except ValueError: pass else: if isinstance(serialized, (tuple, list, dict)): printable = json.dumps(serialized, indent=2) lexer = lexers.JsonLexer() printable = highlight( printable, lexer, formatters.Terminal256Formatter(style=g.style)) return printable
def pygmentize(value): ''' Finds all <code class="python"></code> blocks in a text block and replaces it with pygments-highlighted html semantics. It relies that you provide the format of the input as class attribute. Inspiration: http://www.djangosnippets.org/snippets/25/ Updated by: Samualy Clay Example ------- {% post.body|pygmentize %} ''' last_end = 0 to_return = '' found = 0 for match_obj in regex.finditer(value): code_class = match_obj.group(1) code_string = match_obj.group(2) if code_class.find('class'): language = re.split(r'"|\'', code_class)[1] lexer = lexers.get_lexer_by_name(language) else: try: lexer = lexers.guess_lexer(str(code)) except ValueError: lexer = lexers.PythonLexer() pygmented_string = pygments.highlight(code_string, lexer, formatters.HtmlFormatter()) to_return = to_return + value[last_end:match_obj. start(0)] + pygmented_string last_end = match_obj.end(2) found = found + 1 to_return = to_return + value[last_end:] return to_return
def handle(self, *args, **options): try: from pygments import lexers except ImportError: self.stdout.write("This command requires Pygments package.") self.stdout.write("Please install it with:\n\n") self.stdout.write(" pip install Pygments\n\n") return # Invocation examples _process("bash", lexers.BashLexer()) _process("browser", lexers.JavascriptLexer()) _process("crontab", lexers.BashLexer()) _process("python", lexers.PythonLexer()) _process("php", lexers.PhpLexer()) _process("powershell", lexers.shell.PowerShellLexer()) _process("node", lexers.JavascriptLexer()) # API examples _process("list_checks_request", lexers.BashLexer()) _process("list_checks_response", lexers.JsonLexer()) _process("create_check_request", lexers.BashLexer()) _process("create_check_response", lexers.JsonLexer())
def test_raiseonerror(self): lx = lexers.PythonLexer() lx.add_filter('raiseonerror', excclass=RuntimeError) assert pytest.raises(RuntimeError, list, lx.get_tokens('$'))
def run(out, dry_run=False, check_built=False): snippets = [] py_lexer = lexers.PythonLexer() go_lexer = lexers.GoLexer() html_formatter = HtmlFormatter() for snippet_dir in _dirs(): go_codes = [] for fn in glob(os.path.join(snippet_dir, "*.go")): with open(fn) as f: go_codes.append((fn, f.read())) if not go_codes: print(snippet_dir, "has no .go code") continue py_codes = [] for fn in glob(os.path.join(snippet_dir, "*.py")): with open(fn) as f: py_codes.append((fn, f.read())) if not py_codes: print(snippet_dir, "has no .py code") continue id = os.path.basename(snippet_dir) if os.path.isfile(os.path.join(snippet_dir, "title")): with open(os.path.join(snippet_dir, "title")) as f: title = f.read().strip() else: title = id.replace("_", " ").title() readme = None for fn in glob(os.path.join(snippet_dir, "*.md")): if readme is not None: raise SnippetError("%s contains multiple .md files") with open(fn) as f: readme = special_markdown(f.read()) snippets.append( { "id": id, "title": title, "readme": readme, "go_codes": [ (filename, pygments.highlight(code, go_lexer, html_formatter)) for filename, code in go_codes ], "py_codes": [ (filename, pygments.highlight(code, py_lexer, html_formatter)) for filename, code in py_codes ], } ) template = env.get_template("build.html") html = template.render( snippets=snippets, highlight_css=html_formatter.get_style_defs(".highlight"), bootstrap_css=env.get_template("bootstrap.min.css").render(), ) if dry_run: # Everything worked! return 0 if check_built: # Raise an error if the newly created HTML isn't the same as what # was created before. with open(out) as f: before = f.read() lines_before = [x.strip() for x in before.strip().splitlines() if x.strip()] lines_html = [x.strip() for x in html.strip().splitlines() if x.strip()] if lines_before != lines_html: import difflib print( "".join( difflib.unified_diff( before.splitlines(True), html.splitlines(True), fromfile="HTML before", tofile="New HTML", ) ) ) raise CheckBuiltFailed( "The generated HTML is different from what it was before. " "That means that the HTML made from the snippets doesn't match " "the build HTML. Run this script without --check-built and check " "in the changes to the dist HTML." ) elif out: with open(out, "w") as f: f.write(html) f.write("\n") else: print(html)
def pretty_obj(data): data = pprint.pformat(data, 1) return highlight(data, lexers.PythonLexer(), formatters.TerminalFormatter())
class EditorScreen(Screen): row_num = ObjectProperty(None) col_num = ObjectProperty(None) text_input_area = ObjectProperty(None) file_name_input = ObjectProperty(None) language_mode_chooser = ObjectProperty(None) dashboard = ObjectProperty(None) toggle_btn_file_chooser = ObjectProperty(None) lexer_dict = DictProperty({ "Plain text": lexers.get_lexer_by_name("text"), "C": lexers.CLexer(), "C++": lexers.CppLexer(), "CSS": lexers.CssLexer(), "HTML": lexers.HtmlLexer(), "JavaScript": lexers.JavascriptLexer(), "Kivy": KivyLexer(), "Rust": lexers.RustLexer(), "Python": lexers.PythonLexer(), "Python3": lexers.Python3Lexer(), "SASS": lexers.SassLexer(), "SCSS": lexers.ScssLexer(), }) def __init__(self, **kwargs): super().__init__(**kwargs) self.set_syntax_highlight(tuple(self.lexer_dict.keys())[0]) self.text_input_area.bind(cursor=self.on_cursor) self.language_mode_chooser.bind(text=self.on_lang_mode_chooser) self.file_path = "" self.progress_popup = None self.gen_to_progress = None def on_cursor(self, instance, cursor): # cursor[0]: current cursor postition(col) # cursor[1]: current cursor postition(row) self.col_num.text = str(cursor[0]) self.row_num.text = str(cursor[1] + 1) def on_lang_mode_chooser(self, instance, text): self.set_syntax_highlight(text) def set_syntax_highlight(self, language): self.text_input_area.lexer = self.lexer_dict[language] def toggle_file_chooser_show(self): if self.toggle_btn_file_chooser.state == "normal": # self.dashboard_file_chooser = None self.dashboard.clear_widgets() self.dashboard.size_hint_x = None self.dashboard.width = 0 elif self.toggle_btn_file_chooser.state == "down": self.dashboard.add_widget( DashBoardFileChooserView(file_chooser_user=self)) self.dashboard.size_hint_x = 0.4 def clear_text(self): self.text_input_area.text = "" def save_file(self, file_path): with open(file_path, "w") as f: f.write(self.text_input_area.text) self.file_path = file_path return True def show_save(self): def close(): if self.toggle_btn_file_chooser.state == "down": self.toggle_btn_file_chooser.state = "normal" self.toggle_file_chooser_show() # self.toggle_btn_file_chooser.state = "down" # self.toggle_file_chooser_show() popup = SaveFilePopup(self.save_file, close) popup.open() def open_file_yield_progress(self, file_path, *args): progress_max = 0 progress_count = 0 try: with open(file_path, "r") as f: progress_max = sum(1 for line in open(file_path, "r")) + 1 yield progress_count, progress_max text = "" for line in f: text += line progress_count += 1 yield progress_count, progress_max self.text_input_area.text = text self.file_name_input.text = Path(file_path).parts[-1] self.file_path = file_path progress_count += 1 yield progress_count, progress_max except PermissionError: self.text_input_area.text = (":( Permisson denined: {}".format( Path(file_path).parts[-1])) yield progress_max, progress_max except UnicodeDecodeError: self.text_input_area.text = (":( Unicode decode error: {}".format( Path(file_path).parts[-1])) yield progress_max, progress_max def open_file(self, file_path): try: with open(file_path, "r") as f: text = "" for line in f: text += line self.text_input_area.text = text self.file_name_input.text = Path(file_path).parts[-1] self.file_path = file_path except PermissionError: self.text_input_area.text = (":( Permisson denined: {}".format( Path(file_path).parts[-1])) except UnicodeDecodeError: self.text_input_area.text = (":( Unicode decode error: {}".format( Path(file_path).parts[-1])) else: return True def update_progress_popup(self, dt): try: value, max_value = next(self.gen_to_progress) self.progress_popup.set_progress_max(max_value) self.progress_popup.set_progress_value(value) # --- This code For speeding up to opening file. if dt == 0: dt = -1 else: dt = 0 # --- Clock.schedule_once(self.update_progress_popup, dt) except StopIteration: Clock.unschedule(self.show_progress_schedule) self.progress_popup.dismiss() def open_file_with_progress(self, file_path): self.progress_popup = ProgressPopup() self.gen_to_progress = self.open_file_yield_progress(file_path) self.show_progress_schedule = Clock.schedule_interval( self.update_progress_popup, 0) def show_open(self): open_file_popup = OpenFilePopup(self.open_file_with_progress) open_file_popup.open() def show_license(self): save_file_popup = LicensePopup() save_file_popup.open()
import sys sys.path.insert(0, '..\packages') import pygments from pygments import lexers from pygments import styles from pygments import formatters codeTxt = open('test.py').read() codeTokens = pygments.lex(codeTxt, lexers.PythonLexer()) for token in codeTokens: print(token) formatter = formatters.HtmlFormatter(linenos=True, noclasses=True, style='manni') formatedTxt = (pygments.highlight(codeTxt, lexers.PythonLexer(), formatter)) #print(formatedTxt) with open("highlightedCode.html", "w") as htmlFile: htmlFile.write(formatedTxt)
def pretty(data): result = pprint.pformat(data, indent=4, depth=None) result = highlight(result, lexers.PythonLexer(), formatters.TerminalFormatter()) return result
def test_whitespace(self): lx = lexers.PythonLexer() lx.add_filter('whitespace', spaces='%') text = open(TESTFILE, 'rb').read().decode('utf-8') lxtext = ''.join([t[1] for t in list(lx.get_tokens(text))]) self.failIf(' ' in lxtext)
def format_python(obj): return highlight(pformat(obj), lexers.PythonLexer(), formatters.TerminalFormatter())
LIGHT_CYAN = Fore.LIGHTCYAN_EX LIGHT_GREEN = Fore.LIGHTGREEN_EX LIGHT_MAGENTA = Fore.LIGHTMAGENTA_EX LIGHT_RED = Fore.LIGHTRED_EX LIGHT_WHITE = Fore.LIGHTWHITE_EX LIGHT_YELLOW = Fore.LIGHTYELLOW_EX color_scheme = { token.Token: ('white', 'white'), token.Punctuation: ('', ''), token.Operator: ('', ''), token.Literal: ('brown', 'brown'), token.Keyword: ('brown', 'brown'), token.Name: ('white', 'white'), token.Name.Constant: ('brown', 'brown'), token.Name.Attribute: ('brown', 'brown'), # token.Name.Tag: ('white', 'white'), # token.Name.Function: ('white', 'white'), # token.Name.Variable: ('white', 'white'), } formatter = formatters.TerminalFormatter(colorscheme=color_scheme) json_lexer = lexers.JsonLexer() python_lexer = lexers.PythonLexer() def colorize_json(s): return highlight(s, json_lexer, formatter).rstrip() def color_repr(obj): return highlight(repr(obj), python_lexer, formatter).rstrip()
import random import pygments from pygments import lexers, formatters lex = lexers.PythonLexer() HTML = formatters.HtmlFormatter() with open("text.py", "rb") as f: content = f.read().decode("utf8") html = pygments.highlight(content, lex, HTML) random_str = lambda: "".join([(lambda: random.choice( "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"))() for i in range(64)]) code_id = random_str() final_text = """ <div class="code" id="%s"> <button class="copy_button" onclick=" var parent_ = document.getElementById('%s'); var content_ = parent_.children[1]; var btn = parent_.children[0]; var clearSlct = 'getSelection' in window ? function () {//清除已选择,来自https://www.cnblogs.com/wangpeng-friend/p/6733070.html window.getSelection().removeAllRanges();} : function () {document.selection.empty();}; btn.innerHTML = 'Copied'; if (document.body.createTextRange) { var range = document.body.createTextRange(); range.moveToElementText(content_);
def test_keywordcase(self): lx = lexers.PythonLexer() lx.add_filter('keywordcase', case='capitalize') text = open(TESTFILE, 'rb').read().decode('utf-8') lxtext = ''.join([t[1] for t in list(lx.get_tokens(text))]) self.assert_('Def' in lxtext and 'Class' in lxtext)