Skip to content
This repository was archived by the owner on Aug 20, 2023. It is now read-only.

Commit 74d89df

Browse files
authored
Merge pull request #38 from jolange/gui
Dialog to select language and style
2 parents 437ed5d + 59b39e7 commit 74d89df

File tree

5 files changed

+102
-6
lines changed

5 files changed

+102
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
codehighlighter.oxt
2+
13
# Byte-compiled / optimized / DLL files
24
__pycache__/
35
*.py[cod]

codehighlighter/Addons.xcu

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@
3535
<value>AddLast</value>
3636
</prop>
3737
<node oor:name="MenuItems">
38+
<node oor:name="highlight_code" oor:op="replace">
39+
<prop oor:name="Context" oor:type="xs:string">
40+
<value>com.sun.star.text.TextDocument,com.sun.star.sdb.TextReportDesign,com.sun.star.text.WebDocument,com.sun.star.xforms.XMLFormDocument,com.sun.star.text.GlobalDocument,com.sun.star.sdb.FormDesign,com.sun.star.sheet.SpreadsheetDocument,com.sun.star.drawing.DrawingDocument,com.sun.star.presentation.PresentationDocument,com.sun.star.sdb.OfficeDatabaseDocument</value>
41+
</prop>
42+
<prop oor:name="Title" oor:type="xs:string">
43+
<value xml:lang="en">Highlight Code</value>
44+
</prop>
45+
<prop oor:name="URL" oor:type="xs:string">
46+
<value>vnd.sun.star.script:codehighlighter.oxt|python|highlight.py$create_dialog?language=Python&amp;location=user:uno_packages</value>
47+
</prop>
48+
<prop oor:name="Target" oor:type="xs:string">
49+
<value>_self</value>
50+
</prop>
51+
</node>
3852
<node oor:name="codehighlighter_menu" oor:op="replace">
3953
<prop oor:name="Target" oor:type="xs:string">
4054
<value>_self</value>
@@ -43,8 +57,8 @@
4357
<value>com.sun.star.text.TextDocument,com.sun.star.sdb.TextReportDesign,com.sun.star.text.WebDocument,com.sun.star.xforms.XMLFormDocument,com.sun.star.text.GlobalDocument,com.sun.star.sdb.FormDesign,com.sun.star.sheet.SpreadsheetDocument,com.sun.star.drawing.DrawingDocument,com.sun.star.presentation.PresentationDocument,com.sun.star.sdb.OfficeDatabaseDocument</value>
4458
</prop>
4559
<prop oor:name="Title" oor:type="xs:string">
46-
<value xml:lang="en-US">Highlight Code</value>
47-
<value>Highlight Code</value>
60+
<value xml:lang="en-US">Highlight Code (default style)</value>
61+
<value>Highlight Code (default style)</value>
4862
</prop>
4963
<node oor:name="Submenu">
5064
<node oor:name="lang_0" oor:op="replace">
@@ -5778,4 +5792,4 @@
57785792
</node>
57795793
</node>
57805794
</node>
5781-
</oor:component-data>
5795+
</oor:component-data>

codehighlighter/description.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns:d = "http://openoffice.org/extensions/description/2006"
44
xmlns:l = "http://libreoffice.org/extensions/description/2011"
55
xmlns:xlink="http://www.w3.org/1999/xlink">
6-
<version value="1.5"/>
6+
<version value="1.6"/>
77
<identifier value="javahelps.codehighlighter"/>
88
<icon>
99
<default xlink:href="images/icon_48.png" />
@@ -25,4 +25,4 @@
2525
<license-text xlink:href="license/license.txt" lang="en-US" />
2626
</simple-license>
2727
</registration>
28-
</description>
28+
</description>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE dlg:window PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "dialog.dtd">
3+
<dlg:window xmlns:dlg="http://openoffice.org/2000/dialog" xmlns:script="http://openoffice.org/2000/script" dlg:id="CodeHighlighter" dlg:left="203" dlg:top="169" dlg:width="175" dlg:height="148" dlg:closeable="true" dlg:moveable="true">
4+
<dlg:bulletinboard>
5+
<dlg:text dlg:id="Label1" dlg:tab-index="2" dlg:left="20" dlg:top="6" dlg:width="60" dlg:height="10" dlg:value="Language">
6+
<script:event script:event-name="on-keydown" script:macro-name="vnd.sun.star.script:codehighlighter.oxt|python|highlight.py$key_pressed?language=Python&amp;location=user:uno_packages" script:language="Script"/>
7+
</dlg:text>
8+
<dlg:text dlg:id="Label2" dlg:tab-index="3" dlg:left="20" dlg:top="60" dlg:width="60" dlg:height="10" dlg:value="Style">
9+
<script:event script:event-name="on-keydown" script:macro-name="vnd.sun.star.script:codehighlighter.oxt|python|highlight.py$key_pressed?language=Python&amp;location=user:uno_packages" script:language="Script"/>
10+
</dlg:text>
11+
<dlg:button dlg:id="CommandButton1" dlg:tab-index="4" dlg:left="102" dlg:top="125" dlg:width="52" dlg:height="16" dlg:value="OK" dlg:button-type="ok"/>
12+
<dlg:button dlg:id="CommandButton2" dlg:tab-index="5" dlg:left="20" dlg:top="125" dlg:width="52" dlg:height="16" dlg:value="Cancel" dlg:button-type="cancel"/>
13+
<dlg:combobox dlg:id="cb_lang" dlg:tab-index="0" dlg:left="20" dlg:top="24" dlg:width="100" dlg:height="12" dlg:spin="true">
14+
<script:event script:event-name="on-keydown" script:macro-name="vnd.sun.star.script:codehighlighter.oxt|python|highlight.py$key_pressed?language=Python&amp;location=user:uno_packages" script:language="Script"/>
15+
</dlg:combobox>
16+
<dlg:combobox dlg:id="cb_style" dlg:tab-index="1" dlg:left="20" dlg:top="78" dlg:width="100" dlg:height="12" dlg:spin="true">
17+
<script:event script:event-name="on-keydown" script:macro-name="vnd.sun.star.script:codehighlighter.oxt|python|highlight.py$key_pressed?language=Python&amp;location=user:uno_packages" script:language="Script"/>
18+
</dlg:combobox>
19+
</dlg:bulletinboard>
20+
</dlg:window>

codehighlighter/python/highlight.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
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+
1921
from pygments import styles
22+
from pygments.lexers import get_all_lexers
2023
from pygments.lexers import get_lexer_by_name
2124
from pygments.lexers import guess_lexer
2225
from pygments.styles import get_all_styles
26+
import pygments.util
2327
import 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

4494
def 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

Comments
 (0)