1616# You should have received a copy of the GNU General Public License
1717# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
19+ import uno
20+
1921from pygments import styles
22+ from pygments .lexers import get_all_lexers
2023from pygments .lexers import get_lexer_by_name
2124from pygments .lexers import guess_lexer
2225from pygments .styles import get_all_styles
26+ import pygments .util
2327import os
2428
2529
@@ -40,6 +44,52 @@ def log(msg):
4044 with open ("/tmp/code-highlighter.log" , "a" ) as text_file :
4145 text_file .write (str (msg ) + "\r \n \r \n " )
4246
47+ def create_dialog ():
48+ # get_all_lexers() returns:
49+ # (longname, tuple of aliases, tuple of filename patterns, tuple of mimetypes)
50+ all_lexers = [lex [0 ] for lex in get_all_lexers ()]
51+ all_lexer_aliases = [lex [0 ] for lex in get_all_lexers ()]
52+ for lex in get_all_lexers ():
53+ all_lexer_aliases .extend (list (lex [1 ]))
54+ all_styles = list (get_all_styles ())
55+
56+ ctx = uno .getComponentContext ()
57+ smgr = ctx .ServiceManager
58+ dialog_provider = smgr .createInstance ("com.sun.star.awt.DialogProvider" )
59+ dialog = dialog_provider .createDialog ("vnd.sun.star.extension://javahelps.codehighlighter/dialogs/CodeHighlighter.xdl" )
60+
61+ cb_lang = dialog .getControl ('cb_lang' )
62+ cb_style = dialog .getControl ('cb_style' )
63+
64+ cb_lang .addItem ('automatic' , 0 )
65+ cb_lang .Text = 'automatic'
66+ for i , lex in enumerate (all_lexers ):
67+ cb_lang .addItem (lex , i + 1 )
68+
69+ if 'default' in all_styles :
70+ cb_style .Text = 'default'
71+ for i , style in enumerate (all_styles ):
72+ cb_style .addItem (style , i )
73+
74+ dialog .setVisible (True )
75+ # 0: canceled, 1: OK
76+ if dialog .execute () == 0 :
77+ return
78+
79+ lang = cb_lang .Text
80+ style = cb_style .Text
81+ if lang == 'automatic' :
82+ lang = None
83+ assert lang == None or (lang in all_lexer_aliases ), 'no valid language: ' + lang
84+ assert style in all_styles , 'no valid style: ' + style
85+
86+ highlightSourceCode (lang , style )
87+
88+ def key_pressed (event ):
89+ if event .KeyCode == 1280 :
90+ # enter
91+ dialog = event .Source .getContext ()
92+ dialog .endDialog (1 )
4393
4494def highlightSourceCode (lang , style ):
4595 ctx = XSCRIPTCONTEXT
@@ -75,7 +125,17 @@ def highlight_code(code, cursor, lang, style):
75125 if lang is None :
76126 lexer = guess_lexer (code )
77127 else :
78- lexer = get_lexer_by_name (lang )
128+ try :
129+ lexer = get_lexer_by_name (lang )
130+ except pygments .util .ClassNotFound :
131+ # get_lexer_by_name() only checks aliases, not the actual longname
132+ for lex in get_all_lexers ():
133+ if lex [0 ] == lang :
134+ # found the longname, use the first alias
135+ lexer = get_lexer_by_name (lex [1 ][0 ])
136+ break
137+ else :
138+ raise
79139 style = styles .get_style_by_name (style )
80140 for tok_type , tok_value in lexer .get_tokens (code ):
81141 cursor .goRight (len (tok_value ), True ) # selects the token's text
0 commit comments