Created
November 27, 2017 12:38
-
-
Save Phuket2/bfb31f458b0601040359dd131f649b0c to your computer and use it in GitHub Desktop.
DisplayPythonSource.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import ui | |
import io | |
import editor | |
import console | |
import dialogs | |
from pygments import highlight | |
from pygments.formatters import HtmlFormatter | |
from pygments.styles import get_all_styles | |
from pygments.lexers import get_lexer_by_name | |
class CodeDisplay(ui.View): | |
def __init__(self, style='colorful', use_linenos=True, | |
hl_lines=None, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.code = '' | |
self.web_view = None | |
self.style = style | |
self.use_linenos = use_linenos | |
self.hl_lines = hl_lines or [] | |
self.make_view() | |
def make_view(self): | |
self.web_view = ui.WebView() | |
self.web_view.frame = self.bounds | |
self.web_view.size_to_fit() | |
self.web_view.scales_page_to_fit = True | |
self.web_view.flex = 'WH' | |
self.add_subview(self.web_view) | |
mbtn = ui.ButtonItem(title='Style', action=self.set_code_style) | |
mbtn2 = ui.ButtonItem(title='[Hilite ] ', action=self.hilite_word) | |
self.right_button_items = [mbtn, mbtn2] | |
def get_style_names(self): | |
return sorted([s for s in get_all_styles()]) | |
def load_code_str(self, code): | |
# load python code from a string into the webview | |
self.code = code | |
self.web_view.load_html(self.get_html(code)) | |
def load_code_from_file(self, fn): | |
# load a file into the webview | |
with io.open(fn, encoding='utf-8') as fp: | |
code = fp.read() | |
self.code = code | |
self.web_view.load_html(self.get_html(code)) | |
def reload_style(self): | |
self.load_code_str(self.code) | |
def hilite_lines_with_word(self, word): | |
if not word: | |
return | |
w = word.lower() | |
lns = [ln + 1 for ln, data in enumerate(self.code.splitlines()) | |
if w in data.lower()] | |
self.hl_lines = lns | |
self.reload_style() | |
def get_html(self, code, use_linenos=None, | |
style=None, hl_lines=None): | |
# returns the html from pygments | |
use_linenos = use_linenos or self.use_linenos | |
style = style or self.style | |
hl_lines = hl_lines or self.hl_lines | |
lexer = get_lexer_by_name("python", stripall=False) | |
formatter = HtmlFormatter(linenos=use_linenos, | |
style=style, | |
hl_lines=hl_lines, | |
full=True) | |
return highlight(code, lexer, formatter) | |
def set_code_style(self, sender=None): | |
''' | |
Displays a dialog to set the code style | |
''' | |
items = [{'title': t, 'accessory_type': ''} for t | |
in self.get_style_names()] | |
for d in items: | |
if d['title'] == self.style: | |
d['accessory_type'] = 'checkmark' | |
title_str = 'Current Style - {}'.format(self.style) | |
sel = dialogs.list_dialog(title=title_str, items=items) | |
if sel: | |
self.style = sel['title'] | |
self.reload_style() | |
console.hud_alert('{} style selected'.format(self.style)) | |
def hilite_word(self, sender=None): | |
''' | |
Asks user for a word to be hilited in the source | |
''' | |
word = console.input_alert('Enter a word') | |
self.hilite_lines_with_word(word) | |
console.hud_alert('{} lines hilighted'.format(len(self.hl_lines))) | |
def preview_Code(fn, word_hilite=None, *args, **kwargs): | |
cd = CodeDisplay(name='Source Code Display', *args, **kwargs) | |
cd.load_code_from_file(fn) | |
cd.hilite_lines_with_word(word_hilite) | |
cd.present() | |
if __name__ == '__main__': | |
this_file = editor.get_path() | |
preview_Code(this_file, 'html') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment