class Highlighter(SyntaxHighlighter): state_map = state_map create_formats_func = create_formats user_data_factory = CSSUserData if __name__ == '__main__': from calibre.gui2.tweak_book.editor.widget import launch_editor launch_editor('''\ @charset "utf-8"; /* A demonstration css sheet */ body { color: green; font-size: 12pt } div#main > a:hover { background: url("../image.png"); font-family: "A font", sans-serif; } li[rel="mewl"], p.mewl { margin-top: 2% 0 23pt 1em; } ''', path_is_raw=True, syntax='css')
if __name__ == '__main__': from calibre.gui2.tweak_book.editor.widget import launch_editor launch_editor('''\ <!DOCTYPE html> <html xml:lang="en" lang="en"> <head> <meta charset="utf-8" /> <title>A title with a tag <span> in it, the tag is treated as normal text</title> <style type="text/css"> body { color: green; font-size: 12pt; } </style> <style type="text/css">p.small { font-size: x-small; color:gray }</style> </head id="invalid attribute on closing tag"> <body> <!-- The start of the actual body text --> <h1>A heading that should appear in bold, with an <i>italic</i> word</h1> <p>Some text with inline formatting, that is syntax highlighted. A <b>bold</b> word, and an <em>italic</em> word. \ <i>Some italic text with a <b>bold-italic</b> word in </i>the middle.</p> <!-- Let's see what exotic constructs like namespace prefixes and empty attributes look like --> <svg:svg xmlns:svg="http://whatever" /> <input disabled><input disabled /><span attr=<></span> <!-- Non-breaking spaces are rendered differently from normal spaces, so that they stand out --> <p>Some\xa0words\xa0separated\xa0by\xa0non\u2011breaking\xa0spaces and non\u2011breaking hyphens.</p> </body> </html> ''', path_is_raw=True)
(r'(for|in|while|do|break|return|continue|switch|case|default|if|else|' r'throw|try|catch|finally|new|delete|typeof|instanceof|void|yield|' r'this)\b', Keyword, b'slashstartsregex'), (r'(var|let|with|function)\b', Keyword.Declaration, b'slashstartsregex'), (r'(abstract|boolean|byte|char|class|const|debugger|double|enum|export|' r'extends|final|float|goto|implements|import|int|interface|long|native|' r'package|private|protected|public|short|static|super|synchronized|throws|' r'transient|volatile)\b', Keyword.Reserved), (r'(true|false|null|NaN|Infinity|undefined)\b', Keyword.Constant), (r'(Array|Boolean|Date|Error|Function|Math|netscape|' r'Number|Object|Packages|RegExp|String|sun|decodeURI|' r'decodeURIComponent|encodeURI|encodeURIComponent|' r'Error|eval|isFinite|isNaN|parseFloat|parseInt|document|this|' r'window)\b', Name.Builtin), (JS_IDENT, Name.Other), (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float), (r'0x[0-9a-fA-F]+', Number.Hex), (r'[0-9]+', Number.Integer), (r'"(\\\\|\\"|[^"])*"', String.Double), (r"'(\\\\|\\'|[^'])*'", String.Single), ] } Highlighter = create_highlighter('JavascriptHighlighter', JavascriptLexer) if __name__ == '__main__': from calibre.gui2.tweak_book.editor.widget import launch_editor launch_editor(P('viewer/images.js'), syntax='javascript')
self.hide() return self.set_items(items, descriptions, result.query) self.show() if __name__ == '__main__': from calibre.utils.matcher import Matcher def test(editor): c = editor.__c = CompletionPopup(editor.editor, max_height=100) items = 'a ab abc abcd abcde abcdef abcdefg abcdefgh'.split() m = Matcher(items) c.set_items(m('a'), descriptions={x: x for x in items}) QTimer.singleShot(100, c.show) from calibre.gui2.tweak_book.editor.widget import launch_editor raw = textwrap.dedent('''\ Is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure. ''') launch_editor(raw, path_is_raw=True, callback=test)
<title>A title with a tag <span> in it, the tag is treated as normal text</title> <style type="text/css"> body { color: green; font-size: 12pt; } </style> <style type="text/css">p.small { font-size: x-small; color:gray }</style> </head id="invalid attribute on closing tag"> <body lang="en_IN"><p: <!-- The start of the actual body text --> <h1 lang="en_US">A heading that should appear in bold, with an <i>italic</i> word</h1> <p>Some text with inline formatting, that is syntax highlighted. A <b>bold</b> word, and an <em>italic</em> word. \ <i>Some italic text with a <b>bold-italic</b> word in </i>the middle.</p> <!-- Let's see what exotic constructs like namespace prefixes and empty attributes look like --> <svg:svg xmlns:svg="http://whatever" /> <input disabled><input disabled /><span attr=<></span> <!-- Non-breaking spaces are rendered differently from normal spaces, so that they stand out --> <p>Some\xa0words\xa0separated\xa0by\xa0non\u2011breaking\xa0spaces and non\u2011breaking hyphens.</p> <p>Some non-BMP unicode text:\U0001f431\U0001f431\U0001f431</p> </body> </html> ''' def callback(ed): import regex ed.find_text(regex.compile('A bold word')) launch_editor(raw, path_is_raw=True, syntax='html', callback=callback) # }}}
launch_editor('''\ <!DOCTYPE html> <html xml:lang="en" lang="en"> <!-- --> <head> <meta charset="utf-8" /> <title>A title with a tag <span> in it, the tag is treated as normal text</title> <style type="text/css"> body { color: green; font-size: 12pt; } </style> <style type="text/css">p.small { font-size: x-small; color:gray }</style> </head id="invalid attribute on closing tag"> <body lang="en_IN"><p: <!-- The start of the actual body text --> <h1 lang="en_US">A heading that should appear in bold, with an <i>italic</i> word</h1> <p>Some text with inline formatting, that is syntax highlighted. A <b>bold</b> word, and an <em>italic</em> word. \ <i>Some italic text with a <b>bold-italic</b> word in </i>the middle.</p> <!-- Let's see what exotic constructs like namespace prefixes and empty attributes look like --> <svg:svg xmlns:svg="http://whatever" /> <input disabled><input disabled /><span attr=<></span> <!-- Non-breaking spaces are rendered differently from normal spaces, so that they stand out --> <p>Some\xa0words\xa0separated\xa0by\xa0non\u2011breaking\xa0spaces and non\u2011breaking hyphens.</p> <p>Some non-BMP unicode text:\U0001f431\U0001f431\U0001f431</p> </body> </html> ''', path_is_raw=True)
if not items: self.hide() return self.set_items(items, descriptions, result.query) self.show() if __name__ == '__main__': from calibre.utils.matcher import Matcher def test(editor): c = editor.__c = CompletionPopup(editor.editor, max_height=100) items = 'a ab abc abcd abcde abcdef abcdefg abcdefgh'.split() m = Matcher(items) c.set_items(m('a'), descriptions={x:x for x in items}) QTimer.singleShot(100, c.show) from calibre.gui2.tweak_book.editor.widget import launch_editor raw = textwrap.dedent('''\ Is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure. ''') launch_editor(raw, path_is_raw=True, callback=test)
#!/usr/bin/env python # vim:fileencoding=utf-8 from __future__ import (unicode_literals, division, absolute_import, print_function) __license__ = 'GPL v3' __copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>' from pygments.lexers import PythonLexer from calibre.gui2.tweak_book.editor.syntax.pygments_highlighter import create_highlighter Highlighter = create_highlighter('PythonHighlighter', PythonLexer) if __name__ == '__main__': import os from calibre.gui2.tweak_book.editor.widget import launch_editor launch_editor(os.path.abspath(__file__), syntax='python')
r"extends|final|float|goto|implements|import|int|interface|long|native|" r"package|private|protected|public|short|static|super|synchronized|throws|" r"transient|volatile)\b", Keyword.Reserved, ), (r"(true|false|null|NaN|Infinity|undefined)\b", Keyword.Constant), ( r"(Array|Boolean|Date|Error|Function|Math|netscape|" r"Number|Object|Packages|RegExp|String|sun|decodeURI|" r"decodeURIComponent|encodeURI|encodeURIComponent|" r"Error|eval|isFinite|isNaN|parseFloat|parseInt|document|this|" r"window)\b", Name.Builtin, ), (JS_IDENT, Name.Other), (r"[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?", Number.Float), (r"0x[0-9a-fA-F]+", Number.Hex), (r"[0-9]+", Number.Integer), (r'"(\\\\|\\"|[^"])*"', String.Double), (r"'(\\\\|\\'|[^'])*'", String.Single), ], } Highlighter = create_highlighter("JavascriptHighlighter", JavascriptLexer) if __name__ == "__main__": from calibre.gui2.tweak_book.editor.widget import launch_editor launch_editor(P("viewer/images.js"), syntax="javascript")
(r'[})\].]', Punctuation), (r'(for|in|while|do|break|return|continue|switch|case|default|if|else|' r'throw|try|catch|finally|new|delete|typeof|instanceof|void|yield|' r'this)\b', Keyword, native_string_type('slashstartsregex')), (r'(var|let|with|function)\b', Keyword.Declaration, native_string_type('slashstartsregex')), (r'(abstract|boolean|byte|char|class|const|debugger|double|enum|export|' r'extends|final|float|goto|implements|import|int|interface|long|native|' r'package|private|protected|public|short|static|super|synchronized|throws|' r'transient|volatile)\b', Keyword.Reserved), (r'(true|false|null|NaN|Infinity|undefined)\b', Keyword.Constant), (r'(Array|Boolean|Date|Error|Function|Math|netscape|' r'Number|Object|Packages|RegExp|String|sun|decodeURI|' r'decodeURIComponent|encodeURI|encodeURIComponent|' r'Error|eval|isFinite|isNaN|parseFloat|parseInt|document|this|' r'window)\b', Name.Builtin), (JS_IDENT, Name.Other), (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float), (r'0x[0-9a-fA-F]+', Number.Hex), (r'[0-9]+', Number.Integer), (r'"(\\\\|\\"|[^"])*"', String.Double), (r"'(\\\\|\\'|[^'])*'", String.Single), ] } Highlighter = create_highlighter('JavascriptHighlighter', JavascriptLexer) if __name__ == '__main__': from calibre.gui2.tweak_book.editor.widget import launch_editor launch_editor(P('viewer/images.js'), syntax='javascript')
<meta charset="utf-8" /> <title>A title with a tag <span> in it, the tag is treated as normal text</title> <style type="text/css"> body { color: green; font-size: 12pt; } </style> <style type="text/css">p.small { font-size: x-small; color:gray }</style> </head id="invalid attribute on closing tag"> <body lang="en_IN"><p: <!-- The start of the actual body text --> <h1 lang="en_US">A heading that should appear in bold, with an <i>italic</i> word</h1> <p>Some text with inline formatting, that is syntax highlighted. A <b>bold</b> word, and an <em>italic</em> word. \ <i>Some italic text with a <b>bold-italic</b> word in </i>the middle.</p> <!-- Let's see what exotic constructs like namespace prefixes and empty attributes look like --> <svg:svg xmlns:svg="http://whatever" /> <input disabled><input disabled /><span attr=<></span> <!-- Non-breaking spaces are rendered differently from normal spaces, so that they stand out --> <p>Some\xa0words\xa0separated\xa0by\xa0non\u2011breaking\xa0spaces and non\u2011breaking hyphens.</p> <p>Some non-BMP unicode text:\U0001f431\U0001f431\U0001f431</p> </body> </html> ''' def callback(ed): import regex ed.find_text(regex.compile('A bold word')) launch_editor(raw, path_is_raw=True, syntax='html', callback=callback) # }}}