def handle(self, scheme=None, **options): if not PYGMENTS: raise CommandError('Unable to load pygments. ' 'Please install pygments to use this command.') if options['all_styles']: for scheme in get_all_styles(): print HtmlFormatter(style=scheme)\ .get_style_defs('.%s .codehilite' % scheme) # generated all styles, done and done sys.exit(0) if not scheme: print """ Usage: ./manage.py pygments_styles <scheme_name> Available color schemes: """ + '\n'.join([" %s" % name for name in get_all_styles()]) else: try: assert(scheme in list(get_all_styles())) except AssertionError: raise CommandError('Invalid scheme name "% s"\n' % scheme + 'Please use one of the available color' ' schemes on your system:\n' + '\n'.join([" %s" % name for name in \ get_all_styles()])) print HtmlFormatter(style=scheme).get_style_defs('.codehilite') sys.exit(0)
def _discover_styles(): import inspect from pygments.styles import get_all_styles, get_style_by_name # maps style 'name' (not the class name) and aliases to (module, classname) tuples default_names = {} names = {} styles = {"names": names} if DEBUG: from collections import defaultdict duplicates = defaultdict(set) for name in get_all_styles(): cls = get_style_by_name(name) mod = inspect.getmodule(cls) val = (mod.__name__, cls.__name__) if DEBUG and name in names and names[name] != val and name not in default_names: duplicates[name].add(val) duplicates[name].add(names[name]) names[name] = val # remove some ambiquity names.update(default_names) # print dumplicate message if DEBUG: _print_duplicate_message(duplicates) return styles
def highlight_(data, lang): """A helper function for highlighting data with pygments.""" try: from pygments import highlight from pygments.util import ClassNotFound from pygments.styles import get_all_styles from pygments.lexers import get_lexer_by_name from pygments.formatters import Terminal256Formatter except ImportError: echo_error('Pygments is missing') click.echo('Syntax highlighting is provided by pygments.') click.echo('Please install pygments (http://pygments.org)!') exit(1) try: lexer = get_lexer_by_name(lang) except ClassNotFound: echo_error('Lexer not found!') exit(1) try: formatter = Terminal256Formatter(style=config.PYGMENTS_THEME) except ClassNotFound: styles = get_all_styles() error_msg = 'Pygments theme {} not found!'.format(config.PYGMENTS_THEME) echo_error(error_msg) click.echo("Please correct pygments_theme in your '~/.noterc'!") click.echo('Supported themes are:') click.echo() click.echo('\n'.join(styles)) exit(1) return highlight(data, lexer, formatter)
def register_options(cls, register): register('--code-style', choices=list(get_all_styles()), default='friendly', help='Use this stylesheet for code highlights.') register('--open', action='store_true', help='Open the generated documents in a browser.') register('--fragment', action='store_true', help='Generate a fragment of html to embed in a page.')
def __init__(self, parser_state, outdir, line_numbers=True, formatter_style='default'): self.state = parser_state # expect parser.ParserState instance self.outdir = outdir self.line_numbers = line_numbers self.formatter_style = formatter_style if not formatter_style in get_all_styles(): raise CheckfortException("Invalid HtmlFormatter style - " + style) # vars to supply to all HTML templates self.default_context = { "to_root": "", "gen_date": strftime("%a, %d %b %Y %H:%M:%S", gmtime()), "project_url": project_url, } # default args for pygments.formatters.HtmlFormatter self.fmt_args = { "lineanchors": "line", "cssclass": "highlight", "style": formatter_style, } if self.line_numbers: self.fmt_args["linenos"] = "inline" # cache Event instances self.events = [ Event(code, self.state.event_message[code], count) for code, count in sorted(self.state.event_counter.iteritems(), key=itemgetter(1), reverse=True)]
def get_all_code_styles(): """ Return a mapping from style names to their classes. """ result = dict((name, get_style_by_name(name).styles) for name in get_all_styles()) result['win32'] = win32_code_style return result
def style_info(request, response, function_info): """ Listet alle Stylesheet-Namen auf und zwigt die jeweiligen Styles an. """ style_list = list(get_all_styles()) selected_style = None if function_info!=None: selected_style = function_info[0] if not selected_style in style_list: self.page_msg.red("Name Error!") selected_style = None context = { "styles": style_list, "selected_style": selected_style, "menu_link": request.URLs.actionLink("menu"), } request.templates.write("pygments_css", context, debug=False) if selected_style == None: # Es wurde kein Style ausgewählt return # CSS zum Style anzeigen stylesheet = HtmlFormatter(style=selected_style) stylesheet = stylesheet.get_style_defs('.pygments_code') request.render.highlight( ".css", stylesheet, pygments_style=selected_style )
def index(): code = request.form.get('code', "print 'hello world!'") lexer = ( request.form.get('lexer', '') or unquote(request.cookies.get('lexer', 'python'))) lexers = [(l[1][0], l[0]) for l in get_all_lexers()] lexers = sorted(lexers, lambda a, b: cmp(a[1].lower(), b[1].lower())) style = ( request.form.get('style', '') or unquote(request.cookies.get('style', 'colorful'))) styles = sorted(get_all_styles(), key=str.lower) linenos = ( request.form.get('linenos', '') or request.method == 'GET' and unquote(request.cookies.get('linenos', ''))) or '' divstyles = request.form.get( 'divstyles', unquote(request.cookies.get('divstyles', ''))) divstyles = divstyles or get_default_style() html = hilite_me(code, lexer, {}, style, linenos, divstyles) response = make_response(render_template('index.html', **locals())) next_year = datetime.datetime.now() + datetime.timedelta(days=365) response.set_cookie('lexer', quote(lexer), expires=next_year) response.set_cookie('style', quote(style), expires=next_year) response.set_cookie('linenos', quote(linenos), expires=next_year) response.set_cookie('divstyles', quote(divstyles), expires=next_year) return response
def load_settings(self): '''This function loads project settings ''' self.config_parser = ConfigParser(name='DesignerSettings') DESIGNER_CONFIG = os.path.join(get_config_dir(), constants.DESIGNER_CONFIG_FILE_NAME) DEFAULT_CONFIG = os.path.join(get_kd_dir(), constants.DESIGNER_CONFIG_FILE_NAME) if not os.path.exists(DESIGNER_CONFIG): shutil.copyfile(DEFAULT_CONFIG, DESIGNER_CONFIG) self.config_parser.read(DESIGNER_CONFIG) self.config_parser.upgrade(DEFAULT_CONFIG) # creates a panel before insert it to update code input theme list panel = self.create_json_panel('Kivy Designer Settings', self.config_parser, os.path.join(get_kd_data_dir(), 'settings', 'designer_settings.json')) uid = panel.uid if self.interface is not None: self.interface.add_panel(panel, 'Kivy Designer Settings', uid) # loads available themes for child in panel.children: if child.id == 'code_input_theme_options': child.items = styles.get_all_styles() # tries to find python and buildozer path if it's not defined path = self.config_parser.getdefault( 'global', 'python_shell_path', '') if path.strip() == '': self.config_parser.set('global', 'python_shell_path', sys.executable) self.config_parser.write() buildozer_path = self.config_parser.getdefault('buildozer', 'buildozer_path', '') if buildozer_path.strip() == '': buildozer_path = find_executable('buildozer') if buildozer_path: self.config_parser.set('buildozer', 'buildozer_path', buildozer_path) self.config_parser.write() self.add_json_panel('Buildozer', self.config_parser, os.path.join(get_kd_data_dir(), 'settings', 'buildozer_settings.json')) self.add_json_panel('Hanga', self.config_parser, os.path.join(get_kd_data_dir(), 'settings', 'hanga_settings.json')) self.add_json_panel('Keyboard Shortcuts', self.config_parser, os.path.join(get_kd_data_dir(), 'settings', 'shortcuts.json'))
def handle(self, *args, **options): styles = list(get_all_styles()) path = os.path.join(settings.MEDIA_ROOT, "css", "highlighting", "%s.css") for style in styles: f = file(path % style, "w+") f.write(HtmlFormatter(style=style).get_style_defs('.highlight')) f.close() print "generated stylesheets for %i pygments styles" % len(styles)
def register_options(cls, register): register('--code-style', choices=list(get_all_styles()), default='friendly', help='Use this stylesheet for code highlights.') register('--open', action='store_true', help='Open the generated documents in a browser.') register('--fragment', action='store_true', help='Generate a fragment of html to embed in a page.') register('--ignore-failure', default=False, action='store_true', help='Do not consider rendering errors to be build errors.')
def pref_style(): styles = list(get_all_styles()) print "Choose from one of the styles" count=1 for i in styles: print count,":",i count+=1 k=input() return styles[k-1]
def register_options(cls, register): register('--code-style', choices=list(get_all_styles()), default='friendly', help='Use this stylesheet for code highlights.') register('--open', action='store_true', help='Open the generated documents in a browser.') register('--fragment', action='store_true', help='Generate a fragment of html to embed in a page.') register('--extension', action='append', default=['.md', '.markdown'], help='Process files with these extensions (as well as the standard extensions).')
def pygments_css(schema='colorful'): if schema=='list': from pygments.styles import get_all_styles _message_ok('Avalible styles: {}'.format(', '.join(list(get_all_styles())))) else: local("pygmentize -f html -S {schema} -a .codehilite > {static_root}/css/pygments.css".format( static_root=django_settings.STATIC_ROOT, schema=schema, ))
def configure_codehighlight_options(option_group, mkflag): all_styles = list(get_all_styles()) option_group.add_option( mkflag("code-style"), dest="markdown_to_html_code_style", type="choice", choices=all_styles, help="Selects the stylesheet to use for code highlights, one of: " "%s." % " ".join(all_styles), )
def register_options(cls, register): register('--code-style', choices=list(get_all_styles()), help=('Selects the stylesheet to use for code highlights, ' 'one of {0}'.format(' '.join(get_all_styles()))), legacy='markdown_to_html_code_style') register('--open', action='store_true', help='Open the generated documents in a browser.', legacy='markdown_to_html_open') register('--fragment', action='store_true', help='Generate a fragment of html to embed in a page.', legacy='markdown_to_html_fragment') register('--extension', action='append', help=('Override the default markdown extensions and process pages ' 'whose source have these extensions instead.'), legacy='markdown_to_html_extensions')
def load_settings(self): """This function loads project settings """ self.config_parser = ConfigParser(name="DesignerSettings") DESIGNER_CONFIG = os.path.join(get_kivy_designer_dir(), DESIGNER_CONFIG_FILE_NAME) _dir = os.path.dirname(designer.__file__) _dir = os.path.split(_dir)[0] DEFAULT_CONFIG = os.path.join(_dir, DESIGNER_CONFIG_FILE_NAME) if not os.path.exists(DESIGNER_CONFIG): shutil.copyfile(DEFAULT_CONFIG, DESIGNER_CONFIG) self.config_parser.read(DESIGNER_CONFIG) self.config_parser.upgrade(DEFAULT_CONFIG) # creates a panel before insert it to update code input theme list panel = self.create_json_panel( "Kivy Designer Settings", self.config_parser, os.path.join(_dir, "designer", "settings", "designer_settings.json"), ) uid = panel.uid if self.interface is not None: self.interface.add_panel(panel, "Kivy Designer Settings", uid) # loads available themes for child in panel.children: if child.id == "code_input_theme_options": child.items = styles.get_all_styles() # tries to find python and buildozer path if it's not defined path = self.config_parser.getdefault("global", "python_shell_path", "") if path.strip() == "": self.config_parser.set("global", "python_shell_path", sys.executable) self.config_parser.write() buildozer_path = self.config_parser.getdefault("buildozer", "buildozer_path", "") if buildozer_path.strip() == "": buildozer_path = find_executable("buildozer") if buildozer_path: self.config_parser.set("buildozer", "buildozer_path", buildozer_path) self.config_parser.write() self.add_json_panel( "Buildozer", self.config_parser, os.path.join(_dir, "designer", "settings", "buildozer_settings.json") ) self.add_json_panel( "Hanga", self.config_parser, os.path.join(_dir, "designer", "settings", "hanga_settings.json") ) self.add_json_panel( "Keyboard Shortcuts", self.config_parser, os.path.join(_dir, "designer", "settings", "shortcuts.json") )
def rtf_out(name,k): """Rich text format""" styles = list(get_all_styles()) m=styles[k-1] new="" for i in name: if i==".": break new+=i stri="pygmentize -O full,style="+m+" -o "+new+".rtf "+name os.system(stri)
def add_code_styles_combobox(self, func, previous_style): combo = QComboBox() combo.addItem(previous_style) for style in sorted(list(get_all_styles())): combo.addItem(style) combo.activated[str].connect(func) self.addWidget(combo) return combo
def html_out(name,k): """HTML printed""" styles = list(get_all_styles()) m=styles[k-1] print m new="" for i in name: if i==".": break new+=i stri="pygmentize -O full,style="+m+" -o "+new+".html "+name print stri os.system(stri)
def _print_list(what): if what == "lexer": print() print("Lexers:") print("~~~~~~~") info = [] for fullname, names, exts, _ in get_all_lexers(): tup = (", ".join(names) + ":", fullname, exts and "(filenames " + ", ".join(exts) + ")" or "") info.append(tup) info.sort() for i in info: print(("* %s\n %s %s") % i) elif what == "formatter": print() print("Formatters:") print("~~~~~~~~~~~") info = [] for cls in get_all_formatters(): doc = docstring_headline(cls) tup = ( ", ".join(cls.aliases) + ":", doc, cls.filenames and "(filenames " + ", ".join(cls.filenames) + ")" or "", ) info.append(tup) info.sort() for i in info: print(("* %s\n %s %s") % i) elif what == "filter": print() print("Filters:") print("~~~~~~~~") for name in get_all_filters(): cls = find_filter_class(name) print("* " + name + ":") print(" %s" % docstring_headline(cls)) elif what == "style": print() print("Styles:") print("~~~~~~~") for name in get_all_styles(): cls = get_style_by_name(name) print("* " + name + ":") print(" %s" % docstring_headline(cls))
def run(self): pygments_stylesheets = {} for style_name in get_all_styles(): for formatter_class in [pygments.formatters.LatexFormatter, pygments.formatters.HtmlFormatter]: pygments_formatter = formatter_class(style=style_name) style_info = pygments_formatter.get_style_defs() for fn in pygments_formatter.filenames: ext = fn.split(".")[1] if ext == 'htm': ext = 'css' # swap the more intuitive '.css' for the unlikely '.htm' key = "%s.%s" % (style_name, ext) pygments_stylesheets[key] = style_info return {'pygments' : pygments_stylesheets, 'highlight' : self.highlight }
def _print_list(what): if what == 'lexer': print() print("Lexers:") print("~~~~~~~") info = [] for fullname, names, exts, _ in get_all_lexers(): tup = (', '.join(names)+':', fullname, exts and '(filenames ' + ', '.join(exts) + ')' or '') info.append(tup) info.sort() for i in info: print(('* %s\n %s %s') % i) elif what == 'formatter': print() print("Formatters:") print("~~~~~~~~~~~") info = [] for cls in get_all_formatters(): doc = docstring_headline(cls) tup = (', '.join(cls.aliases) + ':', doc, cls.filenames and '(filenames ' + ', '.join(cls.filenames) + ')' or '') info.append(tup) info.sort() for i in info: print(('* %s\n %s %s') % i) elif what == 'filter': print() print("Filters:") print("~~~~~~~~") for name in get_all_filters(): cls = find_filter_class(name) print("* " + name + ':') print(" %s" % docstring_headline(cls)) elif what == 'style': print() print("Styles:") print("~~~~~~~") for name in get_all_styles(): cls = get_style_by_name(name) print("* " + name + ':') print(" %s" % docstring_headline(cls))
def get_script_to_html_parser(): parser = argparse.ArgumentParser('sos convert FILE.sos FILE.html (or --to html)', description='''Convert sos file to html format with syntax highlighting, and save the output either to a HTML file or view it in a broaser.''') parser.add_argument('--raw', help='''URL to the raw sos file, which will be linked to filenames in the HTML output''') parser.add_argument('--style', choices=list(get_all_styles()), help='''Pygments style for the HTML output.''', default='default') parser.add_argument('--linenos', action='store_true', help='''Display lineno to the left of the source code''') parser.add_argument('-v', '--view', action='store_true', help='''Open the output file in a broswer. In case no html file is specified, this option will display the HTML file in a browser, instead of writing its content to standard output.''') return parser
def render_preference_panel(self, req, panel): styles = list(get_all_styles()) if req.method == 'POST': style = req.args.get('style') if style and style in styles: req.session['pygments_style'] = style add_notice(req, _('Your preferences have been saved.')) req.redirect(req.href.prefs(panel or None)) output = self._generate('html', self.EXAMPLE) return 'prefs_pygments.html', { 'output': output, 'selection': req.session.get('pygments_style', self.default_style), 'styles': styles }
def highlight_source_code(*args): ctx = XSCRIPTCONTEXT doc = ctx.getDocument() # Get the selected item selected_item = doc.getCurrentController().getSelection() # Validate that the selected item is a Shape if 'com.sun.star.drawing.Shapes' in selected_item.getSupportedServiceNames(): for item_idx in range(selected_item.getCount()): # Get the textbox from the shape box = selected_item.getByIndex(item_idx) if 'com.sun.star.drawing.Text' in box.SupportedServiceNames: # Extract the language name. smt = '' try: # Libreoffice Writer and Calc have the Description attribute smt = box.Description.lower() except Exception as err: smt = '' if not smt: try: # Libreoffice Impress has Style attribute not Description smt = box.Style.getName().lower() except Exception as err: smt = '' if smt: if 'code-' in smt: # Remove the prefix code- lang = smt.replace('code-', '') # Default style style = 'default' # Check for explicit style. lang-style if '-' in lang: temp = lang.split('-') lang = temp[0] style = temp[1] # Check whether the style is supported by pygments supported_styles = list(get_all_styles()) if(style not in supported_styles): # If the given style is not supported, use the default style style = 'default' highlight_code(style, lang, box)
def __init__(self, parent): QMenu.__init__(self, _('Choose theme (needs restart)')) parent.addMenu(self) self.group = QActionGroup(self) current = prefs['theme'] alls = list(sorted(get_all_styles())) if current not in alls: current = prefs['theme'] = 'default' self.actions = [] for style in alls: ac = self.group.addAction(style) ac.setCheckable(True) if current == style: ac.setChecked(True) self.actions.append(ac) ac.triggered.connect(partial(self.set_theme, style)) self.addAction(ac)
def create_pygments_stylesheets(self): if not hasattr(self.__class__, 'PYGMENTS_STYLESHEETS'): pygments_stylesheets = {} for style_name in get_all_styles(): for formatter_class in [pygments.formatters.LatexFormatter, pygments.formatters.HtmlFormatter]: pygments_formatter = formatter_class(style=style_name) style_info = pygments_formatter.get_style_defs() for fn in pygments_formatter.filenames: ext = fn.split(".")[1] if ext == 'htm': ext = 'css' # swap the more intuitive '.css' for the unlikely '.htm' key = "%s.%s" % (style_name, ext) pygments_stylesheets[key] = style_info # cache this so we only need to do this once per dexy run... self.__class__.PYGMENTS_STYLESHEETS = pygments_stylesheets return self.__class__.PYGMENTS_STYLESHEETS
def export(): if not os.path.exists(PYGMENTS_PATH): os.makedirs(PYGMENTS_PATH) styles = list(get_all_styles()) for style in styles: print("Generating CSS for %s" % style) opts = {"style": style} path = os.path.join(PYGMENTS_PATH, "%s.css" % style) formatter = HtmlFormatter(**opts) css_content = formatter.get_style_defs() # little fix because pelican doesn't append background color. css_content = css_content.replace(".hll", ".highlight") with open(path, "w") as f: f.write(css_content)
def get_colorscheme_choices(): ''' Yields a two-tuple consisting of the internal pygments style name, and a prettified version of the class name. If the class name is "VisualStudioStyle", it becomes "Visual studio", and the tuple that is yielded is ('vs', 'Visual studio'). ''' for name in get_all_styles(): style_cls = get_style_by_name(name) # Strip the Style-suffix from the class name. pretty_name = style_cls.__name__.replace('Style', '') # Split the name upon case changes, filter out the empty pieces, and # join the remainding pieces with a single space between them. pretty_name = ' '.join(filter(None, re.split(r'([A-Z][a-z]+)', pretty_name))) yield name, pretty_name
def styles_that_need_css(): styles_to_set = [] for s in get_all_styles(): if mw.col.find_cards(f"shf__{s}__highlight"): styles_to_set.append(s) return styles_to_set
def list_styles(): return '{}\n'.format('\n'.join(get_all_styles()))
from pygments.styles import get_all_styles print('\n'.join(get_all_styles()))
option, argument = option.split(':') option = option.strip() argument = argument.strip() if option == 'color': style['textColor'] = argument.strip() if option == 'background-color': style['backColor'] = argument.strip() # These two can come in any order if option == 'font-weight' and argument == 'bold': if 'fontName' in style and \ style['fontName'] == 'stdMonoItalic': style['fontName'] = 'stdMonoBoldItalic' else: style['fontName'] = 'stdMonoBold' if option == 'font-style' and argument == 'italic': if 'fontName' in style and style['fontName'] == 'stdBold': style['fontName'] = 'stdMonoBoldItalic' else: style['fontName'] = 'stdMonoItalic' if style.get('textColor', None) is None: style['textColor'] = 'black' styles.append([sname, style]) return simplejson.dumps({'styles': styles}, indent=2) for name in list(pstyles.get_all_styles()): css = os.popen('pygmentize -S %s -f html' % name, 'r').read() open(name + '.json', 'w').write(css2rl(css))
def on_codeinput_theme(self, section, key, value, *args): if not value in styles.get_all_styles(): show_alert("Error", "This theme is not available") else: self.style_name = value
def list_styles(): return DictResponse(list(get_all_styles()))
""" Using pygments to render the code. """ from pygments import highlight, styles from pygments.lexers import get_all_lexers, get_lexer_by_name from pygments.formatters.html import HtmlFormatter from pygments.styles import get_all_styles from django.utils.translation import ugettext as _ from fluent_contents.plugins.code import appsettings STYLE_CHOICES = map(lambda x: (x, x), get_all_styles()) STYLE_CHOICES.sort(lambda x, y: cmp(x[1].lower(), y[1].lower())) _languageChoices = map(lambda x: (x[1][0], x[0]), get_all_lexers()) _languageChoices.sort(lambda x, y: cmp(x[1].lower(), y[1].lower())) LANGUAGE_CHOICES = tuple( [t for t in _languageChoices if t[0] in appsettings.FLUENT_CODE_SHORTLIST]) if not appsettings.FLUENT_CODE_SHORTLIST_ONLY: LANGUAGE_CHOICES += (_('Combinations'), [t for t in _languageChoices if '+' in t[0]]), LANGUAGE_CHOICES += (_('Advanced'), [ t for t in _languageChoices if '+' not in t[0] and t[0] not in appsettings.FLUENT_CODE_SHORTLIST ]), def render_code(instance, style_name='default'): # Some interesting options in the HtmlFormatter: # - nowrap -> no wrap inside <pre> # - classprefix -> prefix for the classnames
#!/usr/bin/env python3 # Generate CSS code for KwPastebin. # License: 3-clause BSD. from sys import argv from pygments.formatters import get_formatter_by_name from pygments.styles import get_all_styles if len(argv) != 2: print( "Usage: ./generate_css.py style > static/code.css\nStyles available:", ', '.join([str(i) for i in get_all_styles()])) exit(2) style = argv[1] formatter = get_formatter_by_name('html', style=style) print("/* Pygments Style {0} for KwPastebin */\n".format(style)) print(formatter.get_style_defs('.highlight pre')) print( "\ntable.highlighttable {width: 100%;} td.linenos {text-align: right; width: 4em;}" )
from IPython.terminal import prompts try: from traitlets.config.loader import Config except ImportError: from IPython.config.loader import Config from rekall import constants from rekall import config from rekall import session as session_module from rekall_lib import utils config.DeclareOption("--highlighting_style", default="monokai", type="Choices", choices=list(styles.get_all_styles()), help="Highlighting style for interactive console.") def RekallCompleter(self, text): """Sophisticated command line completer for Rekall.""" try: command_parts = self.line_buffer.split(" ") command = command_parts[0] if command.startswith("plugins."): command = command[len("plugins."):] global_matches = set(self.global_matches(command)) # Complete strings which look like symbol names.
class TerminalInteractiveShell(InteractiveShell): space_for_menu = Integer( 6, help='Number of line at the bottom of the screen ' 'to reserve for the completion menu').tag(config=True) def _space_for_menu_changed(self, old, new): self._update_layout() pt_cli = None debugger_history = None _pt_app = None simple_prompt = Bool( _use_simple_prompt, help= """Use `raw_input` for the REPL, without completion, multiline input, and prompt colors. Useful when controlling IPython as a subprocess, and piping STDIN/OUT/ERR. Known usage are: IPython own testing machinery, and emacs inferior-shell integration through elpy. This mode default to `True` if the `IPY_TEST_SIMPLE_PROMPT` environment variable is set, or the current terminal is not a tty. """).tag(config=True) @property def debugger_cls(self): return Pdb if self.simple_prompt else TerminalPdb confirm_exit = Bool( True, help=""" Set to confirm when you try to exit IPython with an EOF (Control-D in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', you can force a direct exit without any confirmation.""", ).tag(config=True) editing_mode = Unicode( 'emacs', help="Shortcut style to use at the prompt. 'vi' or 'emacs'.", ).tag(config=True) mouse_support = Bool( False, help="Enable mouse support in the prompt").tag(config=True) highlighting_style = Union( [Unicode('legacy'), Type(klass=Style)], help="""The name or class of a Pygments style to use for syntax highlighting: \n %s""" % ', '.join(get_all_styles())).tag(config=True) @observe('highlighting_style') @observe('colors') def _highlighting_style_changed(self, change): self.refresh_style() def refresh_style(self): self._style = self._make_style_from_name_or_cls( self.highlighting_style) highlighting_style_overrides = Dict( help="Override highlighting format for specific tokens").tag( config=True) true_color = Bool( False, help=("Use 24bit colors instead of 256 colors in prompt highlighting. " "If your terminal supports true color, the following command " "should print 'TRUECOLOR' in orange: " "printf \"\\x1b[38;2;255;100;0mTRUECOLOR\\x1b[0m\\n\"")).tag( config=True) editor = Unicode( get_default_editor(), help="Set the editor used by IPython (default to $EDITOR/vi/notepad)." ).tag(config=True) prompts_class = Type( Prompts, help='Class used to generate Prompt token for prompt_toolkit').tag( config=True) prompts = Instance(Prompts) @default('prompts') def _prompts_default(self): return self.prompts_class(self) @observe('prompts') def _(self, change): self._update_layout() @default('displayhook_class') def _displayhook_class_default(self): return RichPromptDisplayHook term_title = Bool( True, help="Automatically set the terminal title").tag(config=True) display_completions = Enum( ('column', 'multicolumn', 'readlinelike'), help= ("Options for displaying tab completions, 'column', 'multicolumn', and " "'readlinelike'. These options are for `prompt_toolkit`, see " "`prompt_toolkit` documentation for more information."), default_value='multicolumn').tag(config=True) highlight_matching_brackets = Bool( True, help="Highlight matching brackets .", ).tag(config=True) @observe('term_title') def init_term_title(self, change=None): # Enable or disable the terminal title. if self.term_title: toggle_set_term_title(True) set_term_title('IPython: ' + abbrev_cwd()) else: toggle_set_term_title(False) def init_display_formatter(self): super(TerminalInteractiveShell, self).init_display_formatter() # terminal only supports plain text self.display_formatter.active_types = ['text/plain'] def init_prompt_toolkit_cli(self): if self.simple_prompt: # Fall back to plain non-interactive output for tests. # This is very limited, and only accepts a single line. def prompt(): return cast_unicode_py2( input('In [%d]: ' % self.execution_count)) self.prompt_for_code = prompt return # Set up keyboard shortcuts kbmanager = KeyBindingManager.for_prompt() register_ipython_shortcuts(kbmanager.registry, self) # Pre-populate history from IPython's history database history = InMemoryHistory() last_cell = u"" for __, ___, cell in self.history_manager.get_tail( self.history_load_length, include_latest=True): # Ignore blank lines and consecutive duplicates cell = cell.rstrip() if cell and (cell != last_cell): history.append(cell) last_cell = cell self._style = self._make_style_from_name_or_cls( self.highlighting_style) style = DynamicStyle(lambda: self._style) editing_mode = getattr(EditingMode, self.editing_mode.upper()) self._pt_app = create_prompt_application( editing_mode=editing_mode, key_bindings_registry=kbmanager.registry, history=history, completer=IPythonPTCompleter(shell=self), enable_history_search=True, style=style, mouse_support=self.mouse_support, **self._layout_options()) self._eventloop = create_eventloop(self.inputhook) self.pt_cli = CommandLineInterface( self._pt_app, eventloop=self._eventloop, output=create_output(true_color=self.true_color)) def _make_style_from_name_or_cls(self, name_or_cls): """ Small wrapper that make an IPython compatible style from a style name We need that to add style for prompt ... etc. """ style_overrides = {} if name_or_cls == 'legacy': legacy = self.colors.lower() if legacy == 'linux': style_cls = get_style_by_name('monokai') style_overrides = _style_overrides_linux elif legacy == 'lightbg': style_overrides = _style_overrides_light_bg style_cls = get_style_by_name('pastie') elif legacy == 'neutral': # The default theme needs to be visible on both a dark background # and a light background, because we can't tell what the terminal # looks like. These tweaks to the default theme help with that. style_cls = get_style_by_name('default') style_overrides.update({ Token.Number: '#007700', Token.Operator: 'noinherit', Token.String: '#BB6622', Token.Name.Function: '#2080D0', Token.Name.Class: 'bold #2080D0', Token.Name.Namespace: 'bold #2080D0', Token.Prompt: '#009900', Token.PromptNum: '#00ff00 bold', Token.OutPrompt: '#990000', Token.OutPromptNum: '#ff0000 bold', }) elif legacy == 'nocolor': style_cls = _NoStyle style_overrides = {} else: raise ValueError('Got unknown colors: ', legacy) else: if isinstance(name_or_cls, string_types): style_cls = get_style_by_name(name_or_cls) else: style_cls = name_or_cls style_overrides = { Token.Prompt: '#009900', Token.PromptNum: '#00ff00 bold', Token.OutPrompt: '#990000', Token.OutPromptNum: '#ff0000 bold', } style_overrides.update(self.highlighting_style_overrides) style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls, style_dict=style_overrides) return style def _layout_options(self): """ Return the current layout option for the current Terminal InteractiveShell """ return { 'lexer': IPythonPTLexer(), 'reserve_space_for_menu': self.space_for_menu, 'get_prompt_tokens': self.prompts.in_prompt_tokens, 'get_continuation_tokens': self.prompts.continuation_prompt_tokens, 'multiline': True, 'display_completions_in_columns': (self.display_completions == 'multicolumn'), # Highlight matching brackets, but only when this setting is # enabled, and only when the DEFAULT_BUFFER has the focus. 'extra_input_processors': [ ConditionalProcessor( processor=HighlightMatchingBracketProcessor( chars='[](){}'), filter=HasFocus(DEFAULT_BUFFER) & ~IsDone() & Condition(lambda cli: self.highlight_matching_brackets)) ], } def _update_layout(self): """ Ask for a re computation of the application layout, if for example , some configuration options have changed. """ if self._pt_app: self._pt_app.layout = create_prompt_layout( **self._layout_options()) def prompt_for_code(self): document = self.pt_cli.run(pre_run=self.pre_prompt, reset_current_buffer=True) return document.text def enable_win_unicode_console(self): if sys.version_info >= (3, 6): # Since PEP 528, Python uses the unicode APIs for the Windows # console by default, so WUC shouldn't be needed. return import win_unicode_console if PY3: win_unicode_console.enable() else: # https://github.com/ipython/ipython/issues/9768 from win_unicode_console.streams import (TextStreamWrapper, stdout_text_transcoded, stderr_text_transcoded) class LenientStrStreamWrapper(TextStreamWrapper): def write(self, s): if isinstance(s, bytes): s = s.decode(self.encoding, 'replace') self.base.write(s) stdout_text_str = LenientStrStreamWrapper(stdout_text_transcoded) stderr_text_str = LenientStrStreamWrapper(stderr_text_transcoded) win_unicode_console.enable(stdout=stdout_text_str, stderr=stderr_text_str) def init_io(self): if sys.platform not in {'win32', 'cli'}: return self.enable_win_unicode_console() import colorama colorama.init() # For some reason we make these wrappers around stdout/stderr. # For now, we need to reset them so all output gets coloured. # https://github.com/ipython/ipython/issues/8669 # io.std* are deprecated, but don't show our own deprecation warnings # during initialization of the deprecated API. with warnings.catch_warnings(): warnings.simplefilter('ignore', DeprecationWarning) io.stdout = io.IOStream(sys.stdout) io.stderr = io.IOStream(sys.stderr) def init_magics(self): super(TerminalInteractiveShell, self).init_magics() self.register_magics(TerminalMagics) def init_alias(self): # The parent class defines aliases that can be safely used with any # frontend. super(TerminalInteractiveShell, self).init_alias() # Now define aliases that only make sense on the terminal, because they # need direct access to the console in a way that we can't emulate in # GUI or web frontend if os.name == 'posix': for cmd in ['clear', 'more', 'less', 'man']: self.alias_manager.soft_define_alias(cmd, cmd) def __init__(self, *args, **kwargs): super(TerminalInteractiveShell, self).__init__(*args, **kwargs) self.init_prompt_toolkit_cli() self.init_term_title() self.keep_running = True self.debugger_history = InMemoryHistory() def ask_exit(self): self.keep_running = False rl_next_input = None def pre_prompt(self): if self.rl_next_input: self.pt_cli.application.buffer.text = cast_unicode_py2( self.rl_next_input) self.rl_next_input = None def interact(self, display_banner=DISPLAY_BANNER_DEPRECATED): if display_banner is not DISPLAY_BANNER_DEPRECATED: warn( 'interact `display_banner` argument is deprecated since IPython 5.0. Call `show_banner()` if needed.', DeprecationWarning, stacklevel=2) self.keep_running = True while self.keep_running: print(self.separate_in, end='') try: code = self.prompt_for_code() except EOFError: if (not self.confirm_exit) \ or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'): self.ask_exit() else: if code: self.run_cell(code, store_history=True) def mainloop(self, display_banner=DISPLAY_BANNER_DEPRECATED): # An extra layer of protection in case someone mashing Ctrl-C breaks # out of our internal code. if display_banner is not DISPLAY_BANNER_DEPRECATED: warn( 'mainloop `display_banner` argument is deprecated since IPython 5.0. Call `show_banner()` if needed.', DeprecationWarning, stacklevel=2) while True: try: self.interact() break except KeyboardInterrupt: print("\nKeyboardInterrupt escaped interact()\n") _inputhook = None def inputhook(self, context): if self._inputhook is not None: self._inputhook(context) def enable_gui(self, gui=None): if gui: self._inputhook = get_inputhook_func(gui) else: self._inputhook = None # Run !system commands directly, not through pipes, so terminal programs # work correctly. system = InteractiveShell.system_raw def auto_rewrite_input(self, cmd): """Overridden from the parent class to use fancy rewriting prompt""" if not self.show_rewritten_input: return tokens = self.prompts.rewrite_prompt_tokens() if self.pt_cli: self.pt_cli.print_tokens(tokens) print(cmd) else: prompt = ''.join(s for t, s in tokens) print(prompt, cmd, sep='') _prompts_before = None def switch_doctest_mode(self, mode): """Switch prompts to classic for %doctest_mode""" if mode: self._prompts_before = self.prompts self.prompts = ClassicPrompts(self) elif self._prompts_before: self.prompts = self._prompts_before self._prompts_before = None self._update_layout()
def generate_built_in_styles(): """ Return a mapping from style names to their classes. """ return dict( (name, get_editor_style_by_name(name)) for name in get_all_styles())
from __future__ import unicode_literals from django.db import models from pygments.lexers import get_all_lexers, get_lexer_by_name from pygments.styles import get_all_styles from pygments.formatters.html import HtmlFormatter from pygments import highlight LEXERS = [item for item in get_all_lexers() if item[1]] LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS]) STYLE_CHOICES = sorted((item, item) for item in get_all_styles()) class MyLesson(models.Model): created = models.DateTimeField(auto_now_add=True) title = models.CharField(max_length=100, blank=True, default='') code = models.TextField() linenos = models.BooleanField(default=False) language = models.CharField(choices=LANGUAGE_CHOICES, default='python', max_length=100) style = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100) owner = models.ForeignKey('auth.User', related_name='myLesson', on_delete=models.CASCADE) highlighted = models.TextField() class Meta: ordering = ('created', )
# .. |Binder| image:: https://mybinder.org/badge.svg # :target: https://mybinder.org/v2/gh/euroargodev/argopy/master-doc?urlpath=lab/tree/docs/{{ docname }} # """ # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'none' # Create local pygments copies # Previously used: https://github.com/richleland/pygments-css # But do not want to depend on some random repository from pygments.formatters import HtmlFormatter # noqa: E402 from pygments.styles import get_all_styles # noqa: E402 path = os.path.join('_static', 'pygments') if not os.path.isdir(path): os.mkdir(path) for style in get_all_styles(): path = os.path.join('_static', 'pygments', style + '.css') if os.path.isfile(path): continue with open(path, 'w') as f: f.write(HtmlFormatter(style=style).get_style_defs('.highlight')) # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = True autosummary_generate = True numpydoc_class_members_toctree = True numpydoc_show_class_members = False # If true, the current module name will be prepended to all description # unit titles (such as .. function::).
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from pygments.styles import get_all_styles styles = list(get_all_styles()) for style in styles: import subprocess print('pygmentize -f html -S {style} -a .highlight>{style}.css'.format( style=style)) subprocess.call( 'pygmentize -f html -S {style} -a .highlight>{style}.css'.format( style=style), shell=True) #if __name__ == '__main__':
PATH_LEARNXINY = os.path.join(MYDIR, "cheatsheets/learnxinyminutes-docs") # # Reading configuration from etc/config.yaml # config overrides default settings # if os.path.exists(_CONF_FILE): _CONFIG = yaml.load(_CONF_FILE) if 'server' in _CONFIG: _SERVER_CONFIG = _CONFIG['server'] if 'address' in _SERVER_CONFIG: SERVER_ADDRESS = _SERVER_CONFIG['address'] if 'port' in _SERVER_CONFIG: SERVER_ADDRESS = _SERVER_CONFIG['port'] COLOR_STYLES = sorted(list(get_all_styles())) MALFORMED_RESPONSE_HTML_PAGE = open( os.path.join(STATIC, 'malformed-response.html')).read() def error(text): """ Log error `text` and produce a RuntimeError exception """ if not text.startswith('Too many queries'): print(text) logging.error("ERROR %s", text) raise RuntimeError(text)
#!/usr/bin/python from pygments.lexers import get_all_lexers from pygments.styles import get_all_styles from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter all_lexers = [] for i in get_all_lexers(): i = i[0] if (" " not in i) and ("+" not in i) and ("(" not in i) and ( "-" not in i) and ("/" not in i): all_lexers.append(i) all_lexers.sort() all_formats = [i for i in get_all_styles()] all_formats.sort() def cool(code): tmp = '<div style="border-radius: 4px;border: solid #4286f4;background-color: whitesmoke;"> {} <div style="background-color: lawngreen;"><object align="right">♥Dcode♥ </object></div></div>' return tmp.format(code) def codes(codes, lexer='html', style='colorful'): beauti_looks = "overflow:auto; width:auto; background-color:#f6f8fa; border-radius: 3px;font-family: Courier,monospace;" #beauti_looks+= 'border:solid;border-width:.1em .1em .1em .1em;padding:.2em .6em;' formatter = HtmlFormatter( style=style, noclasses=True, linenos='table',
class Code(VGroup): """A highlighted source code listing. An object ``listing`` of :class:`.Code` is a :class:`.VGroup` consisting of three objects: - The background, ``listing.background_mobject``. This is either a :class:`.Rectangle` (if the listing has been initialized with ``background="rectangle"``, the default option) or a :class:`.VGroup` resembling a window (if ``background="window"`` has been passed). - The line numbers, ``listing.line_numbers`` (a :class:`.Paragraph` object). - The highlighted code itself, ``listing.code`` (a :class:`.Paragraph` object). .. WARNING:: Using a :class:`.Transform` on text with leading whitespace (and in this particular case: code) can look `weird <https://github.com/3b1b/manim/issues/1067>`_. Consider using :meth:`remove_invisible_chars` to resolve this issue. Parameters ---------- file_name : :class:`str` Name of the code file to display. tab_width : :class:`int`, optional Number of space characters corresponding to a tab character. Defaults to 3. line_spacing : :class:`float`, optional Amount of space between lines in relation to font size. Defaults to 0.3, which means 30% of font size. scale_factor : class:`float`, optional A number which scales displayed code. Defaults to 0.5. font : :class:`str`, optional The name of the text font to be used. Defaults to ``"Monospac821 BT"``. stroke_width : class:`float`, optional Stroke width for text. 0 is recommended, and the default. margin: class :`float`, optional Inner margin of text from the background. Defaults to 0.3. indentation_chars : :class:`str`, optional "Indentation chars" refers to the spaces/tabs at the beginning of a given code line. Defaults to ``" "`` (spaces). background : :class:`str`, optional Defines the background's type. Currently supports only ``"rectangle"`` (default) and ``"window"``. background_stroke_width : class:`float`, optional Defines the stroke width of the background. Defaults to 1. background_stroke_color : class:`str`, optional Defines the stroke color for the background. Defaults to ``WHITE``. corner_radius : :class:`float`, optional Defines the corner radius for the background. Defaults to 0.2. insert_line_no : :class:`bool`, optional Defines whether line numbers should be inserted in displayed code. Defaults to ``True``. line_no_from : :class:`int`, optional Defines the first line's number in the line count. Defaults to 1. line_no_buff : :class:`float`, optional Defines the spacing between line numbers and displayed code. Defaults to 0.4. style : :class:`str`, optional Defines the style type of displayed code. You can see possible names of styles in with :attr:`styles_list`. Defaults to ``"vim"``. language : Optional[:class:`str`], optional Specifies the programming language the given code was written in. If ``None`` (the default), the language will be automatically detected. For the list of possible options, visit https://pygments.org/docs/lexers/ and look for 'aliases or short names'. generate_html_file : :class:`bool`, optional Defines whether to generate highlighted html code to the folder `assets/codes/generated_html_files`. Defaults to `False`. Attributes ---------- background_mobject : :class:`~.VGroup` The background of the code listing. line_numbers : :class:`~.Paragraph` The line numbers for the code listing. Empty, if ``insert_line_no=False`` has been specified. code : :class:`~.Paragraph` The highlighted code. Examples -------- Normal usage:: listing = Code( "helloworldcpp.cpp", tab_width=4, background_stroke_width=1, background_stroke_color=WHITE, insert_line_no=True, style=Code.styles_list[15], background="window", language="cpp", ) Remove unwanted invisible characters:: self.play(Transform(remove_invisible_chars(listing.code.chars[0:2]), remove_invisible_chars(listing.code.chars[3][0:3]))) remove_invisible_chars(listing.code) remove_invisible_chars(listing) """ # tuples in the form (name, aliases, filetypes, mimetypes) # 'language' is aliases or short names # For more information about pygments.lexers visit https://pygments.org/docs/lexers/ # from pygments.lexers import get_all_lexers # all_lexers = get_all_lexers() styles_list = list(get_all_styles()) # For more information about pygments.styles visit https://pygments.org/docs/styles/ def __init__( self, file_name=None, tab_width=3, line_spacing=0.3, scale_factor=0.5, font="Monospac821 BT", stroke_width=0, margin=0.3, indentation_chars=" ", background="rectangle", # or window background_stroke_width=1, background_stroke_color=WHITE, corner_radius=0.2, insert_line_no=True, line_no_from=1, line_no_buff=0.4, style="vim", language=None, generate_html_file=False, **kwargs, ): VGroup.__init__( self, stroke_width=stroke_width, background_stroke_color=background_stroke_color, background_stroke_width=background_stroke_width, **kwargs, ) self.tab_width = tab_width self.line_spacing = line_spacing self.scale_factor = scale_factor self.font = font self.margin = margin self.indentation_chars = indentation_chars self.background = background self.corner_radius = corner_radius self.insert_line_no = insert_line_no self.line_no_from = line_no_from self.line_no_buff = line_no_buff self.style = style self.language = language self.generate_html_file = generate_html_file self.file_name = file_name or self.file_name self.ensure_valid_file() self.style = self.style.lower() self.gen_html_string() strati = self.html_string.find("background:") self.background_color = self.html_string[strati + 12:strati + 19] self.gen_code_json() self.code = self.gen_colored_lines() if self.insert_line_no: self.line_numbers = self.gen_line_numbers() self.line_numbers.next_to(self.code, direction=LEFT, buff=self.line_no_buff) if self.background == "rectangle": if self.insert_line_no: foreground = VGroup(self.code, self.line_numbers) else: foreground = self.code rect = SurroundingRectangle( foreground, buff=self.margin, color=self.background_color, fill_color=self.background_color, stroke_width=self.background_stroke_width, stroke_color=self.background_stroke_color, fill_opacity=1, ) rect.round_corners(self.corner_radius) self.background_mobject = VGroup(rect) else: if self.insert_line_no: foreground = VGroup(self.code, self.line_numbers) else: foreground = self.code height = foreground.get_height() + 0.1 * 3 + 2 * self.margin width = foreground.get_width() + 0.1 * 3 + 2 * self.margin rect = RoundedRectangle( corner_radius=self.corner_radius, height=height, width=width, stroke_width=self.background_stroke_width, stroke_color=self.background_stroke_color, color=self.background_color, fill_opacity=1, ) red_button = Dot(radius=0.1, stroke_width=0, color="#ff5f56") red_button.shift(LEFT * 0.1 * 3) yellow_button = Dot(radius=0.1, stroke_width=0, color="#ffbd2e") green_button = Dot(radius=0.1, stroke_width=0, color="#27c93f") green_button.shift(RIGHT * 0.1 * 3) buttons = VGroup(red_button, yellow_button, green_button) buttons.shift( UP * (height / 2 - 0.1 * 2 - 0.05) + LEFT * (width / 2 - 0.1 * 5 - self.corner_radius / 2 - 0.05)) self.background_mobject = VGroup(rect, buttons) x = (height - foreground.get_height()) / 2 - 0.1 * 3 self.background_mobject.shift(foreground.get_center()) self.background_mobject.shift(UP * x) if self.insert_line_no: VGroup.__init__(self, self.background_mobject, self.line_numbers, self.code, **kwargs) else: VGroup.__init__( self, self.background_mobject, Dot(fill_opacity=0, stroke_opacity=0), self.code, **kwargs, ) self.move_to(np.array([0, 0, 0])) def ensure_valid_file(self): """Function to validate file.""" if self.file_name is None: raise Exception("Must specify file for Code") possible_paths = [ os.path.join(os.path.join("assets", "codes"), self.file_name), self.file_name, ] for path in possible_paths: if os.path.exists(path): self.file_path = path return error = ( f"From: {os.getcwd()}, could not find {self.file_name} at either " + "of these locations: {possible_paths}") raise IOError(error) def gen_line_numbers(self): """Function to generate line_numbers. Returns ------- :class:`~.Paragraph` The generated line_numbers according to parameters. """ line_numbers_array = [] for line_no in range(0, self.code_json.__len__()): number = str(self.line_no_from + line_no) line_numbers_array.append(number) line_numbers = Paragraph( *[i for i in line_numbers_array], line_spacing=self.line_spacing, alignment="right", font=self.font, disable_ligatures=True, stroke_width=self.stroke_width, ).scale(self.scale_factor) for i in line_numbers: i.set_color(self.default_color) return line_numbers def gen_colored_lines(self): """Function to generate code. Returns ------- :class:`~.Paragraph` The generated code according to parameters. """ lines_text = [] for line_no in range(0, self.code_json.__len__()): line_str = "" for word_index in range(self.code_json[line_no].__len__()): line_str = line_str + self.code_json[line_no][word_index][0] lines_text.append(self.tab_spaces[line_no] * "\t" + line_str) code = Paragraph( *[i for i in lines_text], line_spacing=self.line_spacing, tab_width=self.tab_width, font=self.font, disable_ligatures=True, stroke_width=self.stroke_width, ).scale(self.scale_factor) for line_no in range(code.__len__()): line = code.chars[line_no] line_char_index = self.tab_spaces[line_no] for word_index in range(self.code_json[line_no].__len__()): line[line_char_index:line_char_index + self. code_json[line_no][word_index][0].__len__()].set_color( self.code_json[line_no][word_index][1]) line_char_index += self.code_json[line_no][word_index][ 0].__len__() return code def gen_html_string(self): """Function to generate html string with code highlighted and stores in variable html_string.""" file = open(self.file_path, "r") code_str = file.read() file.close() self.html_string = hilite_me( code_str, self.language, self.style, self.insert_line_no, "border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;", self.file_path, self.line_no_from, ) if self.generate_html_file: os.makedirs(os.path.join("assets", "codes", "generated_html_files"), exist_ok=True) file = open( os.path.join("assets", "codes", "generated_html_files", self.file_name + ".html"), "w", ) file.write(self.html_string) file.close() def gen_code_json(self): """Function to background_color, generate code_json and tab_spaces from html_string. background_color is just background color of displayed code. code_json is 2d array with rows as line numbers and columns as a array with length 2 having text and text's color value. tab_spaces is 2d array with rows as line numbers and columns as corresponding number of indentation_chars in front of that line in code. """ if (self.background_color == "#111111" or self.background_color == "#272822" or self.background_color == "#202020" or self.background_color == "#000000"): self.default_color = "#ffffff" else: self.default_color = "#000000" # print(self.default_color,self.background_color) for i in range(3, -1, -1): self.html_string = self.html_string.replace("</" + " " * i, "</") for i in range(10, -1, -1): self.html_string = self.html_string.replace( "</span>" + " " * i, " " * i + "</span>") self.html_string = self.html_string.replace("background-color:", "background:") if self.insert_line_no: start_point = self.html_string.find("</td><td><pre") start_point = start_point + 9 else: start_point = self.html_string.find("<pre") self.html_string = self.html_string[start_point:] # print(self.html_string) lines = self.html_string.split("\n") lines = lines[0:lines.__len__() - 2] start_point = lines[0].find(">") lines[0] = lines[0][start_point + 1:] # print(lines) self.code_json = [] self.tab_spaces = [] code_json_line_index = -1 for line_index in range(0, lines.__len__()): # print(lines[line_index]) self.code_json.append([]) code_json_line_index = code_json_line_index + 1 if lines[line_index].startswith(self.indentation_chars): start_point = lines[line_index].find("<") starting_string = lines[line_index][:start_point] indentation_chars_count = lines[ line_index][:start_point].count(self.indentation_chars) if (starting_string.__len__() != indentation_chars_count * self.indentation_chars.__len__()): lines[line_index] = ( "\t" * indentation_chars_count + starting_string[starting_string. rfind(self.indentation_chars) + self.indentation_chars.__len__():] + lines[line_index][start_point:]) else: lines[line_index] = ("\t" * indentation_chars_count + lines[line_index][start_point:]) indentation_chars_count = 0 if lines[line_index]: while lines[line_index][indentation_chars_count] == "\t": indentation_chars_count = indentation_chars_count + 1 self.tab_spaces.append(indentation_chars_count) # print(lines[line_index]) lines[line_index] = self.correct_non_span(lines[line_index]) # print(lines[line_index]) words = lines[line_index].split("<span") for word_index in range(1, words.__len__()): color_index = words[word_index].find("color:") if color_index == -1: color = self.default_color else: starti = words[word_index][color_index:].find("#") color = words[word_index][color_index + starti:color_index + starti + 7] start_point = words[word_index].find(">") end_point = words[word_index].find("</span>") text = words[word_index][start_point + 1:end_point] text = html.unescape(text) if text != "": # print(text, "'" + color + "'") self.code_json[code_json_line_index].append([text, color]) # print(self.code_json) def correct_non_span(self, line_str): """Function put text color to those strings that don't have one according to background_color of displayed code. Parameters --------- line_str : :class:`str` Takes a html element's string to put color to it according to background_color of displayed code. Returns ------- :class:`str` The generated html element's string with having color attributes. """ words = line_str.split("</span>") line_str = "" for i in range(0, words.__len__()): if i != words.__len__() - 1: j = words[i].find("<span") else: j = words[i].__len__() temp = "" starti = -1 for k in range(0, j): if words[i][k] == "\t" and starti == -1: continue else: if starti == -1: starti = k temp = temp + words[i][k] if temp != "": if i != words.__len__() - 1: temp = ('<span style="color:' + self.default_color + '">' + words[i][starti:j] + "</span>") else: temp = ('<span style="color:' + self.default_color + '">' + words[i][starti:j]) temp = temp + words[i][j:] words[i] = temp if words[i] != "": line_str = line_str + words[i] + "</span>" return line_str
def get_available_syntax_styles(self): """Get a list with the syntax styles available.""" styles = list(get_all_styles()) return sorted(styles)
def configure_codehighlight_options(option_group, mkflag): all_styles = list(get_all_styles()) option_group.add_option(mkflag("code-style"), dest="markdown_to_html_code_style", type="choice", choices=all_styles, help="Selects the stylesheet to use for code highlights, one of: " "%s." % ' '.join(all_styles))
def get_style_names(self): l = [] for name in get_all_styles(): l.append(name) l.sort() return l
from django.db import models # Create your models here. from pygments import highlight from pygments.formatters.html import HtmlFormatter from pygments.lexers import get_all_lexers, get_lexer_by_name from pygments.styles import get_all_styles LEXERS = [item for item in get_all_lexers() if item[1]] LANGUAGE_CHOICE = sorted([(item[1][0], item[0]) for item in LEXERS]) STYLE_CHOICE = sorted([(item, item) for item in get_all_styles()]) class Snippet(models.Model): created = models.DateTimeField(auto_now_add=True) title = models.CharField(max_length=100, default='', blank=True) code = models.TextField() linenos = models.BooleanField(default=False) language = models.CharField(choices=LANGUAGE_CHOICE, default='python', max_length=100) style = models.CharField(choices=STYLE_CHOICE, default='friendly', max_length=100) owner = models.ForeignKey('auth.User', related_name='snippets', on_delete=models.CASCADE) highlighted = models.TextField() class Meta: ordering = ['created']
def onSelectStyle(self): d = FilterDialog(parent=None, values=list(get_all_styles())) if d.exec(): self.dialog.lab_style_selected.setText(d.selkey)
import json import functools import shutil import colored from pygments import highlight, lexers, formatters from pygments import styles STYLE_NAMES = list(styles.get_all_styles()) + ['none'] PARENT_ZNODE_STYLE = angry = colored.fg("blue") + colored.attr("bold") def chunks(sequence, num_chunks): """Yield successive n-sized chunks from l.""" for i in range(0, len(sequence), num_chunks): yield sequence[i:i + num_chunks] def columnize(items, nb_columns): """Format the argument items in columns, using the current terminal width""" items_lines = chunks(items, nb_columns) term_width, _ = shutil.get_terminal_size() col_width = int(term_width / nb_columns) lines = '\n'.join([ "".join(item.ljust(col_width) for item in item_line) for item_line in items_lines ]) return lines
def build_argparser(): python_version = sys.version.split(' ', 1)[0] keystone_version = '.'.join(str(i) for i in keystone.version_bind()) unicorn_version = '.'.join(str(i) for i in unicorn.version_bind()) parser = argparse.ArgumentParser(fromfile_prefix_chars='@', description=__doc__) parser.add_argument("-a", "--arch", "--architecture", metavar='<arch>', dest='arch', required=True, help='select the CPU architecture.', choices=_supported_archs.keys()) parser.add_argument("-m", "--mode", metavar='<mode>', dest='mode', required=True, help='select the CPU mode.', choices=_supported_modes.keys()) parser.add_argument("--code-size", metavar='<sz>', dest='code_sz', required=False, type=int, default=2 * 1024 * 1024, help='memory allocated for code.') parser.add_argument("--program-counter", metavar='<pc>', dest='pc_addr', required=False, type=int, default=0x1000000, help='starting program counter address.') parser.add_argument("--style", metavar='<style>', dest='style', required=False, default='paraiso-dark', help='style for the prompt.', choices=sorted(list(get_all_styles()) + ['none'])) parser.add_argument( "-r", "--reg-glob", metavar='<glob>', dest='reg_globs', required=False, action='append', help= 'add one or more glob expressions to show a subset of the registers, like "e?x eip" (x86) or "r*" (arm).' ) parser.add_argument( "-i", "--init", metavar='<file>', dest='init_file', required=False, help= 'execute the instructions from the given file before starting the interactive session.' ) parser.add_argument( "--show-regs", action='store_true', dest='show_regs', required=False, help= 'show the registers available for the given architecture/mode and quit.' ) parser.add_argument( "--timeout", metavar='<secs>', dest='timeout', type=float, required=False, default=5, help='timeout for parsing and executing each assembly instruction.') parser.add_argument("-v", metavar='<version>', dest='isa_version', required=False, default='latest', help='version of the ISA.') parser.add_argument("--simple-prompt", action='store_true', dest='simple_prompt', required=False, default=False, help='simpler alternative prompt.') parser.add_argument("--no-history", action='store_true', required=False, default=False, help='disable shell history of the commands.') parser.add_argument( '-V', '--version', nargs=0, action=_Print, message= '{prog} {version} (Python {python_version}, Keystone {keystone_version}, Unicorn {unicorn_version}) - {license}\n\n{doc}' '\n\n{license_disclaimer}'.format( prog=parser.prog, doc=__doc__, version=__version__, python_version=python_version, keystone_version=keystone_version, unicorn_version=unicorn_version, license=_license, license_disclaimer=_license_disclaimer.format(author=_author, url=_url)), help='show %(prog)s\'s version and license, then exit') return parser
from src.configuration import make_property, options from src.utilities import extract_code, wrap_in_div _pygment_lexer_names = {} _pygment_language_names = [] for name in LEXERS: aliases = LEXERS[name][2] _pygment_lexer_names[name] = aliases[0] for alias in aliases: _pygment_language_names.append(alias) interface.crunchy_pygments = CRUNCHY_PYGMENTS = \ "crunchy_pygments_%d" % int(random.random()*1000000000000) lexers = {} options['style'] = list(get_all_styles()) additional_properties['style'] = make_property('style', default='tango', doc="""\ Style used by pygments to colorize the code. In addition to the default value ['tango'], it includes all the styles [css classes] included in the pygments distribution.""") def register(): for language in _pygment_language_names: plugin["register_tag_handler"]("code", "title", language, pygments_style) plugin["register_tag_handler"]("pre", "title", language, pygments_style) for language in _pygment_lexer_names:
class TerminalInteractiveShell(InteractiveShell): colors_force = True space_for_menu = Integer(6, config=True, help='Number of line at the bottom of the screen ' 'to reserve for the completion menu') def _space_for_menu_changed(self, old, new): self._update_layout() pt_cli = None autoedit_syntax = CBool(False, config=True, help="auto editing of files with syntax errors.") confirm_exit = CBool( True, config=True, help=""" Set to confirm when you try to exit IPython with an EOF (Control-D in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', you can force a direct exit without any confirmation.""", ) vi_mode = Bool( False, config=True, help="Use vi style keybindings at the prompt", ) mouse_support = Bool(False, config=True, help="Enable mouse support in the prompt") highlighting_style = Unicode( 'default', config=True, help= "The name of a Pygments style to use for syntax highlighting: \n %s" % ', '.join(get_all_styles())) def _highlighting_style_changed(self, old, new): self._style = self._make_style_from_name(self.highlighting_style) highlighting_style_overrides = Dict( config=True, help="Override highlighting format for specific tokens") editor = Unicode( get_default_editor(), config=True, help="Set the editor used by IPython (default to $EDITOR/vi/notepad).") term_title = Bool(True, config=True, help="Automatically set the terminal title") def _term_title_changed(self, name, new_value): self.init_term_title() def init_term_title(self): # Enable or disable the terminal title. if self.term_title: toggle_set_term_title(True) set_term_title('IPython: ' + abbrev_cwd()) else: toggle_set_term_title(False) def get_prompt_tokens(self, cli): return [ (Token.Prompt, 'In ['), (Token.PromptNum, str(self.execution_count)), (Token.Prompt, ']: '), ] def get_continuation_tokens(self, cli, width): return [ (Token.Prompt, (' ' * (width - 5)) + '...: '), ] def init_prompt_toolkit_cli(self): if ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or not sys.stdin.isatty(): # Fall back to plain non-interactive output for tests. # This is very limited, and only accepts a single line. def prompt(): return cast_unicode_py2( input('In [%d]: ' % self.execution_count)) self.prompt_for_code = prompt return kbmanager = KeyBindingManager.for_prompt(enable_vi_mode=self.vi_mode) insert_mode = ViStateFilter(kbmanager.get_vi_state, InputMode.INSERT) # Ctrl+J == Enter, seemingly @kbmanager.registry.add_binding(Keys.ControlJ, filter=(HasFocus(DEFAULT_BUFFER) & ~HasSelection() & insert_mode)) def _(event): b = event.current_buffer d = b.document if not (d.on_last_line or d.cursor_position_row >= d.line_count - d.empty_line_count_at_the_end()): b.newline() return status, indent = self.input_splitter.check_complete(d.text) if (status != 'incomplete') and b.accept_action.is_returnable: b.accept_action.validate_and_handle(event.cli, b) else: b.insert_text('\n' + (' ' * (indent or 0))) @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(DEFAULT_BUFFER)) def _(event): event.current_buffer.reset() @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(SEARCH_BUFFER)) def _(event): if event.current_buffer.document.text: event.current_buffer.reset() else: event.cli.push_focus(DEFAULT_BUFFER) supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP')) @kbmanager.registry.add_binding(Keys.ControlZ, filter=supports_suspend) def _(event): event.cli.suspend_to_background() @Condition def cursor_in_leading_ws(cli): before = cli.application.buffer.document.current_line_before_cursor return (not before) or before.isspace() # Ctrl+I == Tab @kbmanager.registry.add_binding(Keys.ControlI, filter=(HasFocus(DEFAULT_BUFFER) & ~HasSelection() & insert_mode & cursor_in_leading_ws)) def _(event): event.current_buffer.insert_text(' ' * 4) # Pre-populate history from IPython's history database history = InMemoryHistory() last_cell = u"" for _, _, cell in self.history_manager.get_tail( self.history_load_length, include_latest=True): # Ignore blank lines and consecutive duplicates cell = cell.rstrip() if cell and (cell != last_cell): history.append(cell) self._style = self._make_style_from_name(self.highlighting_style) style = DynamicStyle(lambda: self._style) self._app = create_prompt_application( key_bindings_registry=kbmanager.registry, history=history, completer=IPythonPTCompleter(self.Completer), enable_history_search=True, style=style, mouse_support=self.mouse_support, **self._layout_options()) self.pt_cli = CommandLineInterface(self._app, eventloop=create_eventloop( self.inputhook)) def _make_style_from_name(self, name): """ Small wrapper that make an IPython compatible style from a style name We need that to add style for prompt ... etc. """ style_cls = get_style_by_name(name) style_overrides = { Token.Prompt: style_cls.styles.get(Token.Keyword, '#009900'), Token.PromptNum: style_cls.styles.get(Token.Literal.Number, '#00ff00 bold') } if name is 'default': style_cls = get_style_by_name('default') # The default theme needs to be visible on both a dark background # and a light background, because we can't tell what the terminal # looks like. These tweaks to the default theme help with that. style_overrides.update({ Token.Number: '#007700', Token.Operator: 'noinherit', Token.String: '#BB6622', Token.Name.Function: '#2080D0', Token.Name.Class: 'bold #2080D0', Token.Name.Namespace: 'bold #2080D0', }) style_overrides.update(self.highlighting_style_overrides) style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls, style_dict=style_overrides) return style def _layout_options(self): """ Return the current layout option for the current Terminal InteractiveShell """ return { 'lexer': IPythonPTLexer(), 'reserve_space_for_menu': self.space_for_menu, 'get_prompt_tokens': self.get_prompt_tokens, 'get_continuation_tokens': self.get_continuation_tokens, 'multiline': False, } def _update_layout(self): """ Ask for a re computation of the application layout, if for example , some configuration options have changed. """ self._app.layout = create_prompt_layout(**self._layout_options()) def prompt_for_code(self): document = self.pt_cli.run(pre_run=self.pre_prompt) return document.text def init_io(self): if sys.platform not in {'win32', 'cli'}: return import colorama colorama.init() # For some reason we make these wrappers around stdout/stderr. # For now, we need to reset them so all output gets coloured. # https://github.com/ipython/ipython/issues/8669 from IPython.utils import io io.stdout = io.IOStream(sys.stdout) io.stderr = io.IOStream(sys.stderr) def init_magics(self): super(TerminalInteractiveShell, self).init_magics() self.register_magics(TerminalMagics) def init_alias(self): # The parent class defines aliases that can be safely used with any # frontend. super(TerminalInteractiveShell, self).init_alias() # Now define aliases that only make sense on the terminal, because they # need direct access to the console in a way that we can't emulate in # GUI or web frontend if os.name == 'posix': for cmd in ['clear', 'more', 'less', 'man']: self.alias_manager.soft_define_alias(cmd, cmd) def __init__(self, *args, **kwargs): super(TerminalInteractiveShell, self).__init__(*args, **kwargs) self.init_prompt_toolkit_cli() self.init_term_title() self.keep_running = True def ask_exit(self): self.keep_running = False rl_next_input = None def pre_prompt(self): if self.rl_next_input: self.pt_cli.application.buffer.text = cast_unicode_py2( self.rl_next_input) self.rl_next_input = None def interact(self): while self.keep_running: print(self.separate_in, end='') try: code = self.prompt_for_code() except EOFError: if (not self.confirm_exit) \ or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'): self.ask_exit() else: if code: self.run_cell(code, store_history=True) if self.autoedit_syntax and self.SyntaxTB.last_syntax_error: self.edit_syntax_error() def mainloop(self): # An extra layer of protection in case someone mashing Ctrl-C breaks # out of our internal code. while True: try: self.interact() break except KeyboardInterrupt: print("\nKeyboardInterrupt escaped interact()\n") _inputhook = None def inputhook(self, context): if self._inputhook is not None: self._inputhook(context) def enable_gui(self, gui=None): if gui: self._inputhook = get_inputhook_func(gui) else: self._inputhook = None # Methods to support auto-editing of SyntaxErrors: def edit_syntax_error(self): """The bottom half of the syntax error handler called in the main loop. Loop until syntax error is fixed or user cancels. """ while self.SyntaxTB.last_syntax_error: # copy and clear last_syntax_error err = self.SyntaxTB.clear_err_state() if not self._should_recompile(err): return try: # may set last_syntax_error again if a SyntaxError is raised self.safe_execfile(err.filename, self.user_ns) except: self.showtraceback() else: try: with open(err.filename) as f: # This should be inside a display_trap block and I # think it is. sys.displayhook(f.read()) except: self.showtraceback() def _should_recompile(self, e): """Utility routine for edit_syntax_error""" if e.filename in ('<ipython console>', '<input>', '<string>', '<console>', '<BackgroundJob compilation>', None): return False try: if (self.autoedit_syntax and not self.ask_yes_no( 'Return to editor to correct syntax error? ' '[Y/n] ', 'y')): return False except EOFError: return False def int0(x): try: return int(x) except TypeError: return 0 # always pass integer line and offset values to editor hook try: self.hooks.fix_error_editor(e.filename, int0(e.lineno), int0(e.offset), e.msg) except TryNext: warn('Could not open editor') return False return True # Run !system commands directly, not through pipes, so terminal programs # work correctly. system = InteractiveShell.system_raw
import os, re, sys from tempfile import mktemp from pygments import highlight from pygments.console import ansiformat from pygments.lexers import BashLexer, RstLexer from pygments.filter import Filter from pygments.formatter import Formatter from pygments.formatters.terminal import TERMINAL_COLORS from pygments.formatters import TerminalFormatter, Terminal256Formatter from pygments.token import Keyword, Name, Comment, String, Error, \ Number, Operator, Generic, Token, Whitespace from pygments.util import get_choice_opt from pygments.styles import get_all_styles style_names = sorted(list(get_all_styles())) warnings.simplefilter("ignore") # FIXME: change some horrible colors under atom dark # this is a hack until I get general way to do colorstyle setting color_scheme = TERMINAL_COLORS.copy() color_scheme[Generic.Strong] = ('*black*', '*white*') color_scheme[Name.Variable] = ('_black_', '_white_') color_scheme[Generic.Strong] = ('*black*', '*white*') color_scheme[Name.Variable] = ('_black_', '_white_') color_scheme[Generic.Emph] = color_scheme[Comment.Preproc] # FIXME: change some horrible colors under atom dark # this is a hack until I get general way to do colorstyle setting
class CodeInput(CodeNavigationBehavior, TextInput): '''CodeInput class, used for displaying highlighted code. ''' lexer = ObjectProperty(None) '''This holds the selected Lexer used by pygments to highlight the code. :attr:`lexer` is an :class:`~kivy.properties.ObjectProperty` and defaults to `PythonLexer`. ''' style_name = OptionProperty('default', options=list(styles.get_all_styles())) '''Name of the pygments style to use for formatting. :attr:`style_name` is an :class:`~kivy.properties.OptionProperty` and defaults to ``'default'``. ''' style = ObjectProperty(None) '''The pygments style object to use for formatting. When ``style_name`` is set, this will be changed to the corresponding style object. :attr:`style` is a :class:`~kivy.properties.ObjectProperty` and defaults to ``None`` ''' def __init__(self, **kwargs): stylename = kwargs.get('style_name', 'default') style = kwargs['style'] if 'style' in kwargs \ else styles.get_style_by_name(stylename) self.formatter = BBCodeFormatter(style=style) 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 on_style_name(self, *args): self.style = styles.get_style_by_name(self.style_name) self.background_color = get_color_from_hex(self.style.background_color) self._trigger_refresh_text() def on_style(self, *args): self.formatter = BBCodeFormatter(style=self.style) self._trigger_update_graphics() def _create_line_label(self, text, hint=False): # Create a label from a text, using line options ntext = text.replace(u'\n', u'').replace(u'\t', u' ' * self.tab_width) if self.password and not hint: # Don't replace hint_text with * ntext = u'*' * len(ntext) ntext = self._get_bbcode(ntext) kw = self._get_line_options() cid = u'{}\0{}\0{}'.format(text, self.password, kw) texture = Cache_get('textinput.label', cid) if texture is None: # FIXME right now, we can't render very long line... # if we move on "VBO" version as fallback, we won't need to # do this. # try to find the maximum text we can handle label = Label(text=ntext, **kw) if text.find(u'\n') > 0: label.text = u'' else: label.text = ntext label.refresh() # ok, we found it. texture = label.texture Cache_append('textinput.label', cid, texture) label.text = '' return texture def _get_line_options(self): kw = super(CodeInput, self)._get_line_options() kw['markup'] = True kw['valign'] = 'top' kw['codeinput'] = repr(self.lexer) return kw def _get_text_width(self, text, tab_width, _label_cached): # Return the width of a text, according to the current line options. cid = u'{}\0{}\0{}'.format(text, self.password, self._get_line_options()) width = Cache_get('textinput.width', cid) if width is not None: return width lbl = self._create_line_label(text) width = lbl.width Cache_append('textinput.width', cid, width) return width def _get_bbcode(self, ntext): # get bbcoded text for python try: ntext[0] # replace brackets with special chars that aren't highlighted # by pygment. can't use &bl; ... cause & is highlighted ntext = ntext.replace(u'[', u'\x01').replace(u']', u'\x02') ntext = highlight(ntext, self.lexer, self.formatter) ntext = ntext.replace(u'\x01', u'&bl;').replace(u'\x02', u'&br;') # replace special chars with &bl; and &br; ntext = ''.join( (u'[color=', str(self.text_color), u']', ntext, u'[/color]')) ntext = ntext.replace(u'\n', u'') # remove possible extra highlight options ntext = ntext.replace(u'[u]', '').replace(u'[/u]', '') return ntext except IndexError: return '' # overridden to prevent cursor position off screen def _cursor_offset(self): '''Get the cursor x offset on the current line ''' offset = 0 try: if self.cursor_col: offset = self._get_text_width( self._lines[self.cursor_row][:self.cursor_col]) return offset except: pass finally: return offset def on_lexer(self, instance, value): self._trigger_refresh_text() def on_foreground_color(self, instance, text_color): if not self.use_text_color: self.use_text_color = True return self.text_color = get_hex_from_color(text_color) self.use_text_color = False self.foreground_color = (1, 1, 1, .999) self._trigger_refresh_text()
class TerminalInteractiveShell(InteractiveShell): colors_force = True space_for_menu = Integer( 6, help='Number of line at the bottom of the screen ' 'to reserve for the completion menu').tag(config=True) def _space_for_menu_changed(self, old, new): self._update_layout() pt_cli = None debugger_history = None simple_prompt = Bool( _use_simple_prompt, help= """Use `raw_input` for the REPL, without completion, multiline input, and prompt colors. Useful when controlling IPython as a subprocess, and piping STDIN/OUT/ERR. Known usage are: IPython own testing machinery, and emacs inferior-shell integration through elpy. This mode default to `True` if the `IPY_TEST_SIMPLE_PROMPT` environment variable is set, or the current terminal is not a tty. """).tag(config=True) @property def debugger_cls(self): return Pdb if self.simple_prompt else TerminalPdb autoedit_syntax = Bool( False, help="auto editing of files with syntax errors.", ).tag(config=True) confirm_exit = Bool( True, help=""" Set to confirm when you try to exit IPython with an EOF (Control-D in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', you can force a direct exit without any confirmation.""", ).tag(config=True) editing_mode = Unicode( 'emacs', help="Shortcut style to use at the prompt. 'vi' or 'emacs'.", ).tag(config=True) mouse_support = Bool( False, help="Enable mouse support in the prompt").tag(config=True) highlighting_style = Unicode( 'default', help= "The name of a Pygments style to use for syntax highlighting: \n %s" % ', '.join(get_all_styles())).tag(config=True) @observe('highlighting_style') def _highlighting_style_changed(self, change): self._style = self._make_style_from_name(self.highlighting_style) highlighting_style_overrides = Dict( help="Override highlighting format for specific tokens").tag( config=True) editor = Unicode( get_default_editor(), help="Set the editor used by IPython (default to $EDITOR/vi/notepad)." ).tag(config=True) prompts_class = Type( Prompts, help='Class used to generate Prompt token for prompt_toolkit').tag( config=True) prompts = Instance(Prompts) @default('prompts') def _prompts_default(self): return self.prompts_class(self) @observe('prompts') def _(self, change): self._update_layout() @default('displayhook_class') def _displayhook_class_default(self): return RichPromptDisplayHook term_title = Bool( True, help="Automatically set the terminal title").tag(config=True) display_completions_in_columns = Bool( False, help="Display a multi column completion menu.", ).tag(config=True) highlight_matching_brackets = Bool( True, help="Highlight matching brackets .", ).tag(config=True) @observe('term_title') def init_term_title(self, change=None): # Enable or disable the terminal title. if self.term_title: toggle_set_term_title(True) set_term_title('IPython: ' + abbrev_cwd()) else: toggle_set_term_title(False) def init_prompt_toolkit_cli(self): self._app = None if self.simple_prompt: # Fall back to plain non-interactive output for tests. # This is very limited, and only accepts a single line. def prompt(): return cast_unicode_py2( input('In [%d]: ' % self.execution_count)) self.prompt_for_code = prompt return kbmanager = KeyBindingManager.for_prompt() insert_mode = ViInsertMode() | EmacsInsertMode() # Ctrl+J == Enter, seemingly @kbmanager.registry.add_binding(Keys.ControlJ, filter=(HasFocus(DEFAULT_BUFFER) & ~HasSelection() & insert_mode)) def _(event): b = event.current_buffer d = b.document if b.complete_state: cc = b.complete_state.current_completion if cc: b.apply_completion(cc) return if not (d.on_last_line or d.cursor_position_row >= d.line_count - d.empty_line_count_at_the_end()): b.newline() return status, indent = self.input_splitter.check_complete(d.text) if (status != 'incomplete') and b.accept_action.is_returnable: b.accept_action.validate_and_handle(event.cli, b) else: b.insert_text('\n' + (' ' * (indent or 0))) @kbmanager.registry.add_binding(Keys.ControlP, filter=(ViInsertMode() & HasFocus(DEFAULT_BUFFER))) def _previous_history_or_previous_completion(event): """ Control-P in vi edit mode on readline is history next, unlike default prompt toolkit. If completer is open this still select previous completion. """ event.current_buffer.auto_up() @kbmanager.registry.add_binding(Keys.ControlN, filter=(ViInsertMode() & HasFocus(DEFAULT_BUFFER))) def _next_history_or_next_completion(event): """ Control-N in vi edit mode on readline is history previous, unlike default prompt toolkit. If completer is open this still select next completion. """ event.current_buffer.auto_down() @kbmanager.registry.add_binding(Keys.ControlG, filter=(HasFocus(DEFAULT_BUFFER) & HasCompletions())) def _dismiss_completion(event): b = event.current_buffer if b.complete_state: b.cancel_completion() @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(DEFAULT_BUFFER)) def _reset_buffer(event): b = event.current_buffer if b.complete_state: b.cancel_completion() else: b.reset() @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(SEARCH_BUFFER)) def _reset_search_buffer(event): if event.current_buffer.document.text: event.current_buffer.reset() else: event.cli.push_focus(DEFAULT_BUFFER) supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP')) @kbmanager.registry.add_binding(Keys.ControlZ, filter=supports_suspend) def _suspend_to_bg(event): event.cli.suspend_to_background() @Condition def cursor_in_leading_ws(cli): before = cli.application.buffer.document.current_line_before_cursor return (not before) or before.isspace() # Ctrl+I == Tab @kbmanager.registry.add_binding(Keys.ControlI, filter=(HasFocus(DEFAULT_BUFFER) & ~HasSelection() & insert_mode & cursor_in_leading_ws)) def _indent_buffer(event): event.current_buffer.insert_text(' ' * 4) # Pre-populate history from IPython's history database history = InMemoryHistory() last_cell = u"" for __, ___, cell in self.history_manager.get_tail( self.history_load_length, include_latest=True): # Ignore blank lines and consecutive duplicates cell = cell.rstrip() if cell and (cell != last_cell): history.append(cell) self._style = self._make_style_from_name(self.highlighting_style) style = DynamicStyle(lambda: self._style) editing_mode = getattr(EditingMode, self.editing_mode.upper()) self._app = create_prompt_application( editing_mode=editing_mode, key_bindings_registry=kbmanager.registry, history=history, completer=IPythonPTCompleter(self.Completer), enable_history_search=True, style=style, mouse_support=self.mouse_support, **self._layout_options()) self._eventloop = create_eventloop(self.inputhook) self.pt_cli = CommandLineInterface(self._app, eventloop=self._eventloop) def _make_style_from_name(self, name): """ Small wrapper that make an IPython compatible style from a style name We need that to add style for prompt ... etc. """ style_cls = get_style_by_name(name) style_overrides = { Token.Prompt: '#009900', Token.PromptNum: '#00ff00 bold', Token.OutPrompt: '#990000', Token.OutPromptNum: '#ff0000 bold', } if name == 'default': style_cls = get_style_by_name('default') # The default theme needs to be visible on both a dark background # and a light background, because we can't tell what the terminal # looks like. These tweaks to the default theme help with that. style_overrides.update({ Token.Number: '#007700', Token.Operator: 'noinherit', Token.String: '#BB6622', Token.Name.Function: '#2080D0', Token.Name.Class: 'bold #2080D0', Token.Name.Namespace: 'bold #2080D0', }) style_overrides.update(self.highlighting_style_overrides) style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls, style_dict=style_overrides) return style def _layout_options(self): """ Return the current layout option for the current Terminal InteractiveShell """ return { 'lexer': IPythonPTLexer(), 'reserve_space_for_menu': self.space_for_menu, 'get_prompt_tokens': self.prompts.in_prompt_tokens, 'get_continuation_tokens': self.prompts.continuation_prompt_tokens, 'multiline': True, 'display_completions_in_columns': self.display_completions_in_columns, # Highlight matching brackets, but only when this setting is # enabled, and only when the DEFAULT_BUFFER has the focus. 'extra_input_processors': [ ConditionalProcessor( processor=HighlightMatchingBracketProcessor( chars='[](){}'), filter=HasFocus(DEFAULT_BUFFER) & ~IsDone() & Condition(lambda cli: self.highlight_matching_brackets)) ], } def _update_layout(self): """ Ask for a re computation of the application layout, if for example , some configuration options have changed. """ if getattr(self, '._app', None): self._app.layout = create_prompt_layout(**self._layout_options()) def prompt_for_code(self): document = self.pt_cli.run(pre_run=self.pre_prompt, reset_current_buffer=True) return document.text def init_io(self): if sys.platform not in {'win32', 'cli'}: return import colorama colorama.init() # For some reason we make these wrappers around stdout/stderr. # For now, we need to reset them so all output gets coloured. # https://github.com/ipython/ipython/issues/8669 from IPython.utils import io io.stdout = io.IOStream(sys.stdout) io.stderr = io.IOStream(sys.stderr) def init_magics(self): super(TerminalInteractiveShell, self).init_magics() self.register_magics(TerminalMagics) def init_alias(self): # The parent class defines aliases that can be safely used with any # frontend. super(TerminalInteractiveShell, self).init_alias() # Now define aliases that only make sense on the terminal, because they # need direct access to the console in a way that we can't emulate in # GUI or web frontend if os.name == 'posix': for cmd in ['clear', 'more', 'less', 'man']: self.alias_manager.soft_define_alias(cmd, cmd) def __init__(self, *args, **kwargs): super(TerminalInteractiveShell, self).__init__(*args, **kwargs) self.init_prompt_toolkit_cli() self.init_term_title() self.keep_running = True self.debugger_history = InMemoryHistory() def ask_exit(self): self.keep_running = False rl_next_input = None def pre_prompt(self): if self.rl_next_input: self.pt_cli.application.buffer.text = cast_unicode_py2( self.rl_next_input) self.rl_next_input = None def interact(self): while self.keep_running: print(self.separate_in, end='') try: code = self.prompt_for_code() except EOFError: if (not self.confirm_exit) \ or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'): self.ask_exit() else: if code: self.run_cell(code, store_history=True) if self.autoedit_syntax and self.SyntaxTB.last_syntax_error: self.edit_syntax_error() def mainloop(self): # An extra layer of protection in case someone mashing Ctrl-C breaks # out of our internal code. while True: try: self.interact() break except KeyboardInterrupt: print("\nKeyboardInterrupt escaped interact()\n") if hasattr(self, '_eventloop'): self._eventloop.close() _inputhook = None def inputhook(self, context): if self._inputhook is not None: self._inputhook(context) def enable_gui(self, gui=None): if gui: self._inputhook = get_inputhook_func(gui) else: self._inputhook = None # Methods to support auto-editing of SyntaxErrors: def edit_syntax_error(self): """The bottom half of the syntax error handler called in the main loop. Loop until syntax error is fixed or user cancels. """ while self.SyntaxTB.last_syntax_error: # copy and clear last_syntax_error err = self.SyntaxTB.clear_err_state() if not self._should_recompile(err): return try: # may set last_syntax_error again if a SyntaxError is raised self.safe_execfile(err.filename, self.user_ns) except: self.showtraceback() else: try: with open(err.filename) as f: # This should be inside a display_trap block and I # think it is. sys.displayhook(f.read()) except: self.showtraceback() def _should_recompile(self, e): """Utility routine for edit_syntax_error""" if e.filename in ('<ipython console>', '<input>', '<string>', '<console>', '<BackgroundJob compilation>', None): return False try: if (self.autoedit_syntax and not self.ask_yes_no( 'Return to editor to correct syntax error? ' '[Y/n] ', 'y')): return False except EOFError: return False def int0(x): try: return int(x) except TypeError: return 0 # always pass integer line and offset values to editor hook try: self.hooks.fix_error_editor(e.filename, int0(e.lineno), int0(e.offset), e.msg) except TryNext: warn('Could not open editor') return False return True # Run !system commands directly, not through pipes, so terminal programs # work correctly. system = InteractiveShell.system_raw def auto_rewrite_input(self, cmd): """Overridden from the parent class to use fancy rewriting prompt""" if not self.show_rewritten_input: return tokens = self.prompts.rewrite_prompt_tokens() if self.pt_cli: self.pt_cli.print_tokens(tokens) print(cmd) else: prompt = ''.join(s for t, s in tokens) print(prompt, cmd, sep='') _prompts_before = None def switch_doctest_mode(self, mode): """Switch prompts to classic for %doctest_mode""" if mode: self._prompts_before = self.prompts self.prompts = ClassicPrompts(self) elif self._prompts_before: self.prompts = self._prompts_before self._prompts_before = None
def hilcd(ed, code, langAlias): global LASTUSED linenos = gc('linenos') centerfragments = gc('centerfragments') noclasses = not gc('cssclasses') if (ed.mw.app.keyboardModifiers() & Qt.ShiftModifier): linenos ^= True if (ed.mw.app.keyboardModifiers() & Qt.ControlModifier): centerfragments ^= True mystyle = gc("style") if (ed.mw.app.keyboardModifiers() & Qt.AltModifier): d = FilterDialog(parent=None, values=list(get_all_styles())) if d.exec(): mystyle = d.selkey noclasses = True inline = False if (ed.mw.app.keyboardModifiers() & Qt.MetaModifier): inline = True if inline: linenos = False try: if gc("remove leading spaces if possible", True): my_lexer = get_lexer_by_name(langAlias, stripall=False) else: my_lexer = get_lexer_by_name(langAlias, stripall=True) except ClassNotFound as e: print(e) print(ERR_LEXER) showError(ERR_LEXER, parent=ed.parentWindow) return False try: # http://pygments.org/docs/formatters/#HtmlFormatter """ nowrap If set to True, don’t wrap the tokens at all, not even inside a <pre> tag. This disables most other options (default: False). full Tells the formatter to output a “full” document, i.e. a complete self-contained document (default: False). title If full is true, the title that should be used to caption the document (default: ''). style The style to use, can be a string or a Style subclass (default: 'default'). This option has no effect if the cssfile and noclobber_cssfile option are given and the file specified in cssfile exists. noclasses If set to true, token <span> tags (as well as line number elements) will not use CSS classes, but inline styles. This is not recommended for larger pieces of code since it increases output size by quite a bit (default: False). classprefix Since the token types use relatively short class names, they may clash with some of your own class names. In this case you can use the classprefix option to give a string to prepend to all Pygments-generated CSS class names for token types. Note that this option also affects the output of get_style_defs(). cssclass CSS class for the wrapping <div> tag (default: 'highlight'). If you set this option, the default selector for get_style_defs() will be this class. If you select the 'table' line numbers, the wrapping table will have a CSS class of this string plus 'table', the default is accordingly 'highlighttable'. cssstyles Inline CSS styles for the wrapping <div> tag (default: ''). prestyles Inline CSS styles for the <pre> tag (default: ''). cssfile If the full option is true and this option is given, it must be the name of an external file. If the filename does not include an absolute path, the file’s path will be assumed to be relative to the main output file’s path, if the latter can be found. The stylesheet is then written to this file instead of the HTML file. noclobber_cssfile If cssfile is given and the specified file exists, the css file will not be overwritten. This allows the use of the full option in combination with a user specified css file. Default is False. linenos If set to 'table', output line numbers as a table with two cells, one containing the line numbers, the other the whole code. This is copy-and-paste-friendly, but may cause alignment problems with some browsers or fonts. If set to 'inline', the line numbers will be integrated in the <pre> tag that contains the code The default value is False, which means no line numbers at all. For compatibility with Pygments 0.7 and earlier, every true value except 'inline' means the same as 'table' (in particular, that means also True). hl_lines Specify a list of lines to be highlighted. linenostart The line number for the first line (default: 1). linenostep If set to a number n > 1, only every nth line number is printed. linenospecial If set to a number n > 0, every nth line number is given the CSS class "special" (default: 0). nobackground If set to True, the formatter won’t output the background color for the wrapping element (this automatically defaults to False when there is no wrapping element [eg: no argument for the get_syntax_defs method given]) (default: False). lineseparator This string is output between lines of code. It defaults to "\n", which is enough to break a line inside <pre> tags, but you can e.g. set it to "<br>" to get HTML line breaks. lineanchors If set to a nonempty string, e.g. foo, the formatter will wrap each output line in an anchor tag with a name of foo-linenumber. This allows easy linking to certain lines. linespans If set to a nonempty string, e.g. foo, the formatter will wrap each output line in a span tag with an id of foo-linenumber. This allows easy access to lines via javascript. anchorlinenos If set to True, will wrap line numbers in <a> tags. Used in combination with linenos and lineanchors. tagsfile If set to the path of a ctags file, wrap names in anchor tags that link to their definitions. lineanchors should be used, and the tags file should specify line numbers (see the -n option to ctags). tagurlformat A string formatting pattern used to generate links to ctags definitions. Available variables are %(path)s, %(fname)s and %(fext)s. Defaults to an empty string, resulting in just #prefix-number links. filename A string used to generate a filename when rendering <pre> blocks, for example if displaying source code. wrapcode Wrap the code inside <pre> blocks using <code>, as recommended by the HTML5 specification. """ tablestyling = "" if noclasses: tablestyling += "text-align: left;" if gc("cssclasses") and not gc("css_custom_class_per_style"): css_class = "highlight" # the default for pygments else: css_class = f"shf__{mystyle}__highlight" my_formatter = HtmlFormatter( cssclass=css_class, cssstyles=tablestyling, font_size=16, linenos=linenos, lineseparator="<br>", nobackground= False, # True would solve night mode problem without any config (as long as no line numbers are used) noclasses=noclasses, style=mystyle, wrapcode=True) except ClassNotFound as e: print(e) print(ERR_STYLE) showError(ERR_STYLE, parent=ed.parentWindow) return False pygmntd = highlight(code, my_lexer, my_formatter).rstrip() # when using noclasses/inline styling pygments adds line-height 125%, see # see https://github.com/pygments/pygments/blob/2fe2152377e317fd215776b6d7467bda3e8cda28/pygments/formatters/html.py#L269 # It's seems to be only relevant for IE and makes the line numbers misaligned on my PC. So I remove it. if noclasses: pygmntd = pygmntd.replace('line-height: 125%;', '') if inline: pretty_code = "".join([pygmntd, "<br>"]) replacements = { '<div class=': '<span class=', "<pre": "<code", "</pre></div>": "</code></span>", "<br>": "", "</br>": "", "</ br>": "", "<br />": "", 'style="line-height: 125%"': '', } for k, v in replacements.items(): pretty_code = pretty_code.replace(k, v) else: if linenos: pretty_code = "".join([pygmntd, "<br>"]) # to show line numbers pygments uses a table. The background color for the code # highlighting is limited to this table # If pygments doesn't use linenumbers it doesn't use a table. This means # that the background color covers the whole width of the card. # I don't like this. I didn't find an easier way than reusing the existing # solution. # Glutanimate removed the table in the commit # https://github.com/glutanimate/syntax-highlighting/commit/afbf5b3792611ecd2207b9975309d05de3610d45 # which hasn't been published on Ankiweb in 2019-10-02. else: pretty_code = "".join([ f'<table class="{css_class}table"><tbody><tr><td>', pygmntd, "</td></tr></tbody></table><br>" ]) """ I can't solely rely on the pygments-HTMLFormatter A lot of the stuff I did before 2020-11 with bs4 can indeed be done by adjusting the HTMLFormatter options: - I can override the ".card {text-align: center}" by using the option "cssstyles" (Inline CSS styles for the wrapping <div> tag). - I can set a custom class by adjusting the option "cssclass" which defaults to "highlight" Besides this there are the classes linenos and linenodiv. BUT I don't need to customize the latter classes. I can also work with longer css rules: /*syntax highlighting fork add-on: dark background*/ .night_mode .shf__default__highlight{ background-color: #222222 !important; } /*syntax highlighting fork add-on: line numbers: white on black: sometimes a span is used, sometimes not*/ .night_mode .shf__default__highlighttable tr td.linenos div.linenodiv pre span { background-color: #222222 !important; color: #f0f0f0 !important; } .night_mode .shf__default__highlighttable tr td.linenos div.linenodiv pre { background-color: #222222 !important; color: #f0f0f0 !important; } BUT as far as I see I can't set inline styling for the surrounding table. But to center the table I need to add something like "margin: 0 auto;". If you rely on css it's easy because the "the wrapping table will have a CSS class of [the cssclass] string plus 'table', the default is accordingly 'highlighttable'.". But my option should work without the user adjusting each template and the editor. I also need to set the font. """ if centerfragments or (noclasses and gc('font')): soup = BeautifulSoup(pretty_code, 'html.parser') if centerfragments: tablestyling = "margin: 0 auto;" for t in soup.findAll("table"): if t.has_attr('style'): t['style'] = tablestyling + t['style'] else: t['style'] = tablestyling if noclasses and gc('font'): for t in soup.findAll("code"): t['style'] = "font-family: %s;" % gc('font') pretty_code = str(soup) if noclasses: out = json.dumps(pretty_code).replace('\n', ' ').replace('\r', '') # In 2020-05 I don't remember why I used backticks/template literals # here in commit 6ea0fe8 from 2019-11. # Maybe for multi-line strings(?) but json.dumps shouldn't include newlines, because # json.dumps does "Serialize obj to a JSON formatted str using this conversion table." # for the conversion table see https://docs.python.org/3/library/json.html#py-to-json-table # which includes JSONEncoder(.. indent=None, separators=None,..) and # If indent ... None (the default) selects the most compact representation. # out = "`" + json.dumps(pretty_code)[1:-1] + "`" ed.web.eval("MyInsertHtml(%s);" % out) else: # setFormat is a thin wrapper in Anki around document.execCommand ed.web.eval("setFormat('inserthtml', %s);" % json.dumps(pretty_code)) LASTUSED = langAlias