def format(self, record): try: record.message = record.getMessage() except Exception as e: record.message = "Something exploded trying to calcualte this message: " + repr(e) try: formatted = super(JSONFormatter, self).formatMessage(record) except Exception as e: formatted = "Something exploded trying to format this message: " + repr(e) try: json = self.jsonify( record, separators = (', ' if not self.indent else ',', ': ') if __debug__ else (',', ':'), indent = "\t" if self.indent else None, ) except Exception as e: formatted = "JSON calculation failed: " + repr(e) json = None if json: if self.highlight: return '\n'.join([formatted, _highlight(json, JsonLexer(tabsize=4), Terminal256Formatter(style='monokai')).strip()]) return '\n'.join([formatted, json]).strip() return formatted
def highlight(content, lexer_name, formatter): try: lexer = get_lexer_by_name(lexer_name) except ClassNotFound: if lexer_name != '': return "No such lexer.", 400 if formatter and formatter != 'html': formatter = get_formatter_by_name(formatter) else: formatter = HtmlFormatter(linenos='table', anchorlinenos=True, lineanchors='L', linespans='L') if lexer_name == '': tokens = ((Token.Text, '{}\n'.format(c.decode('utf-8'))) for c in content.splitlines()) content = _format(tokens, formatter) else: content = _highlight(content, lexer, formatter) if not isinstance(formatter, HtmlFormatter): return content template = render_template("generic.html", cc='container-fluid', content=content, **style_args()) return template
def format(self, record): try: record.message = record.getMessage() except Exception as e: record.message = "Something exploded trying to calcualte this message: " + repr( e) try: formatted = super(JSONFormatter, self).formatMessage(record) except Exception as e: formatted = "Something exploded trying to format this message: " + repr( e) try: json = self.jsonify( record, separators=(', ' if not self.indent else ',', ': ') if __debug__ else (',', ':'), indent="\t" if self.indent else None, ) except Exception as e: formatted = "JSON calculation failed: " + repr(e) json = None if json: if self.highlight: return '\n'.join([ formatted, _highlight(json, JsonLexer(tabsize=4), Terminal256Formatter(style='monokai')).strip() ]) return '\n'.join([formatted, json]).strip() return formatted
def highlight(code, lexer_name='text'): formatter = MyHtmlFormatter(style='default', linenos=True, lineanchors='line') if code: return _highlight(code, get_lexer_by_name(lexer_name), formatter) else: return u''
def _highlight(value: str) -> str: style = "default" pyproject_file = _Path.cwd() / "pyproject.toml" if pyproject_file.is_file(): pyproject_obj = _PyProject.load(pyproject_file) style = pyproject_obj.tool.get(NAME, {}).get("style", style) return _highlight( value, _PythonLexer(), _Terminal256Formatter(style=style) )
def _repr_html_(self): html = '' if self.model_file is not None: html += '<html><span><pre> model_file: <b>' + self.model_file + '</b></pre></span>' if self.model_name is not None: html += '<span><pre> model_name: <b>' + self.model_name + '</b></pre></span>' html += '<span><pre><b> model_code :</b></pre></span>' html += '<style type="text/css">{}</style>{}'.format( \ _HtmlFormatter().get_style_defs('.highlight'), \ _highlight(self.model_code, StanLexer(), _HtmlFormatter())) return html
def highlight(text): # Generated HTML contains unnecessary newline at the end # before </pre> closing tag. # We need to remove that newline because it's screwing up # QTextEdit formatting and is being displayed # as a non-editable whitespace. highlighted_text = _highlight(text, SqlLexer(), HtmlFormatter()).strip() # Split generated HTML by last newline in it # argument 1 indicates that we only want to split the string # by one specified delimiter from the right. parts = highlighted_text.rsplit("\n", 1) # Glue back 2 split parts to get the HTML without last # unnecessary newline highlighted_text_no_last_newline = "".join(parts) return highlighted_text_no_last_newline
def highlight(content, lexer_name): try: lexer = get_lexer_by_name(lexer_name) except ClassNotFound: if lexer_name != '': return "No such lexer.", 400 formatter = HtmlFormatter(linenos='table', anchorlinenos=True, lineanchors='L', linespans='L') if lexer_name == '': tokens = ((Token.Text, '{}\n'.format(c.decode('utf-8'))) for c in content.splitlines()) content = _format(tokens, formatter) else: content = _highlight(content, lexer, formatter) template = render_template('generic.html', cc='container-fluid', content=content) return Response(template, mimetype='text/html')
def highlight(content, lexer_name): try: lexer = get_lexer_by_name(lexer_name) except ClassNotFound: if lexer_name != "": return "No such lexer.", 400 formatter = HtmlFormatter(linenos="table", anchorlinenos=True, lineanchors="L", linespans="L") if lexer_name == "": tokens = ((Token.Text, "{}\n".format(c.decode("utf-8"))) for c in content.splitlines()) content = _format(tokens, formatter) else: content = _highlight(content, lexer, formatter) template = render_template("generic.html", cc="container-fluid", content=content, **style_args()) return Response(template, mimetype="text/html")
def highlight(text): # Generated HTML contains unnecessary newline at the end # before </pre> closing tag. # We need to remove that newline because it's screwing up # QTextEdit formatting and is being displayed # as a non-editable whitespace. # However, origin solution works wired for \n at start and end # I add flag \b, which is invisible text = '\b' + text + '\b' highlighted_text = _highlight(text, MySqlLexer(), HtmlFormatter()) # Split generated HTML by last newline in it # argument 1 indicates that we only want to split the string # by one specified delimiter from the right. parts = highlighted_text.rsplit("\n", 1) # Glue back 2 split parts to get the HTML without last # unnecessary newline highlighted_text_no_last_newline = "".join(parts) return highlighted_text_no_last_newline.replace('\b', '')
def highlight(content, lexer_name): try: lexer = get_lexer_by_name(lexer_name) except ClassNotFound: if lexer_name != '': return "No such lexer.", 400 formatter = HtmlFormatter(linenos='table', anchorlinenos=True, lineanchors='L', linespans='L') if lexer_name == '': tokens = ((Token.Text, '{}\n'.format(c.decode('utf-8'))) for c in content.splitlines()) content = _format(tokens, formatter) else: content = _highlight(content, lexer, formatter) template = render_template('highlight.html', content=content) return Response(template, mimetype='text/html')
def highlight(content, lexer_name, formatter): try: lexer = get_lexer_by_name(lexer_name) except ClassNotFound: if lexer_name != '': return "No such lexer.", 400 if formatter: formatter = get_formatter_by_name(formatter) else: formatter = HtmlFormatter(linenos='table', anchorlinenos=True, lineanchors='L', linespans='L') if lexer_name == '': tokens = ((Token.Text, '{}\n'.format(c.decode('utf-8'))) for c in content.splitlines()) content = _format(tokens, formatter) else: content = _highlight(content, lexer, formatter) if not isinstance(formatter, HtmlFormatter): return content template = render_template("generic.html", cc='container-fluid', content=content, **style_args()) return template
def highlight(code, lexer_name="text"): # formatter = MyHtmlFormatter(style='default', linenos=True, lineanchors='line') if code: return _highlight(code, get_lexer_by_name(lexer_name), formatter) else: return u""
def highlight(raw: str) -> str: highlighted = _highlight(raw, http_lexer, formatter) if highlighted[-1] == '\n': highlighted = highlighted[:-1] + '⏎\n' return highlighted
def _repr_html_(self): html = '<style type="text/css">{}</style>{}'.format( \ _HtmlFormatter().get_style_defs('.highlight'), \ _highlight(self, StanLexer(), _HtmlFormatter())) return html
def highlight(code): return _highlight(code, CppLexer(), HtmlFormatter())