Skip to content

Commit

Permalink
Allow user to set his own prefix for character styles.
Browse files Browse the repository at this point in the history
See issue #8, specifically this comment:
#8 (comment)
  • Loading branch information
jmzambon committed Jul 5, 2022
1 parent 073f6ef commit 4dcf28c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 23 deletions.
1 change: 1 addition & 0 deletions codehighlighter/AddonRegistry.xcs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<prop oor:name="Language" oor:type="xs:string"/>
<prop oor:name="Style" oor:type="xs:string"/>
<prop oor:name="UseCharStyles" oor:type="xs:short"/>
<prop oor:name="CharStylePrefix" oor:type="xs:string"/>
<prop oor:name="ColourizeBackground" oor:type="xs:short"/>
<prop oor:name="ShowLineNumbers" oor:type="xs:short"/>
<prop oor:name="LineNumberStart" oor:type="xs:long"/>
Expand Down
3 changes: 3 additions & 0 deletions codehighlighter/AddonRegistry.xcu
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<prop oor:name="UseCharStyles" oor:type="xs:short">
<value>0</value>
</prop>
<prop oor:name="CharStylePrefix" oor:type="xs:string">
<value></value>
</prop>
<prop oor:name="ColourizeBackground" oor:type="xs:short">
<value>1</value>
</prop>
Expand Down
2 changes: 1 addition & 1 deletion codehighlighter/description.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<identifier value="javahelps.codehighlighter"/>

<version value="2.3.2.1"/>
<version value="2.3.2.2"/>

<update-information>
<src xlink:href="https://raw.githubusercontent.com/jmzambon/libreoffice-code-highlighter/master/codehighlighter.update.xml" />
Expand Down
29 changes: 16 additions & 13 deletions codehighlighter/dialogs/CodeHighlighter2.xdl
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@
<dlg:checkbox dlg:id="check_col_bg" dlg:tab-index="4" dlg:left="15" dlg:top="74" dlg:width="100" dlg:height="10" dlg:page="1" dlg:help-text="Use the background color provided by the style." dlg:value="Set background color" dlg:valign="center" dlg:checked="true"/>
<dlg:checkbox dlg:id="check_linenb" dlg:tab-index="5" dlg:left="15" dlg:top="85" dlg:width="142" dlg:height="10" dlg:page="1" dlg:checked="false"/>
<dlg:checkbox dlg:id="check_charstyles" dlg:tab-index="6" dlg:left="15" dlg:top="96" dlg:width="142" dlg:height="10" dlg:page="1" dlg:checked="false"/>
<dlg:numericfield dlg:style-id="2" dlg:id="nb_start" dlg:tab-index="7" dlg:left="72" dlg:top="30" dlg:width="57" dlg:height="15" dlg:page="2" dlg:valign="center" dlg:strict-format="true" dlg:decimal-accuracy="0" dlg:value="1" dlg:value-min="1" dlg:spin="true" dlg:repeat="50"/>
<dlg:numericfield dlg:style-id="2" dlg:id="nb_ratio" dlg:tab-index="8" dlg:left="72" dlg:top="47" dlg:width="57" dlg:height="15" dlg:page="2" dlg:valign="center" dlg:strict-format="true" dlg:decimal-accuracy="0" dlg:value-min="50" dlg:value-max="100" dlg:spin="true" dlg:repeat="50"/>
<dlg:textfield dlg:style-id="2" dlg:id="nb_sep" dlg:tab-index="9" dlg:left="72" dlg:top="64" dlg:width="34" dlg:height="15" dlg:page="2" dlg:valign="center"/>
<dlg:button dlg:id="btn_ok" dlg:tab-index="10" dlg:left="15" dlg:top="116" dlg:width="50" dlg:height="16" dlg:default="true" dlg:button-type="ok"/>
<dlg:button dlg:id="btn_cancel" dlg:tab-index="11" dlg:left="72" dlg:top="116" dlg:width="50" dlg:height="16" dlg:button-type="cancel"/>
<dlg:button dlg:id="topage1" dlg:tab-index="12" dlg:left="130" dlg:top="116" dlg:width="50" dlg:height="16" dlg:page="2">
<dlg:numericfield dlg:style-id="2" dlg:id="nb_start" dlg:tab-index="7" dlg:left="72" dlg:top="25" dlg:width="57" dlg:height="15" dlg:page="2" dlg:valign="center" dlg:strict-format="true" dlg:decimal-accuracy="0" dlg:value="1" dlg:value-min="1" dlg:spin="true" dlg:repeat="50"/>
<dlg:numericfield dlg:style-id="2" dlg:id="nb_ratio" dlg:tab-index="8" dlg:left="72" dlg:top="42" dlg:width="57" dlg:height="15" dlg:page="2" dlg:valign="center" dlg:strict-format="true" dlg:decimal-accuracy="0" dlg:value-min="50" dlg:value-max="100" dlg:spin="true" dlg:repeat="50"/>
<dlg:textfield dlg:style-id="2" dlg:id="nb_sep" dlg:tab-index="9" dlg:left="72" dlg:top="59" dlg:width="34" dlg:height="15" dlg:page="2" dlg:valign="center"/>
<dlg:textfield dlg:style-id="2" dlg:id="cs_prefix" dlg:tab-index="10" dlg:left="92" dlg:top="95" dlg:width="50" dlg:height="15" dlg:page="2" dlg:valign="center"/>
<dlg:button dlg:id="btn_ok" dlg:tab-index="11" dlg:left="15" dlg:top="116" dlg:width="50" dlg:height="16" dlg:default="true" dlg:button-type="ok"/>
<dlg:button dlg:id="btn_cancel" dlg:tab-index="12" dlg:left="72" dlg:top="116" dlg:width="50" dlg:height="16" dlg:button-type="cancel"/>
<dlg:button dlg:id="topage1" dlg:tab-index="13" dlg:left="130" dlg:top="116" dlg:width="50" dlg:height="16" dlg:page="2">
<script:event script:event-name="on-performaction" script:macro-name="vnd.sun.star.UNO:topage1" script:language="UNO"/>
</dlg:button>
<dlg:text dlg:style-id="1" dlg:id="pygments_ver" dlg:tab-index="13" dlg:left="18" dlg:top="140" dlg:width="85" dlg:height="8" dlg:value="Build upon pygments {}"/>
<dlg:img dlg:style-id="2" dlg:id="pygments_logo" dlg:tab-index="14" dlg:left="5" dlg:top="139" dlg:width="11" dlg:height="9" dlg:scale-mode="isotropic" dlg:src="vnd.sun.star.extension://javahelps.codehighlighter/images/pygments.png"/>
<dlg:fixedline dlg:id="nb_line" dlg:tab-index="15" dlg:left="15" dlg:top="11" dlg:width="144" dlg:height="10" dlg:page="2"/>
<dlg:text dlg:id="lbl_nb_start" dlg:tab-index="16" dlg:left="15" dlg:top="30" dlg:width="47" dlg:height="15" dlg:page="2" dlg:align="right" dlg:valign="center"/>
<dlg:text dlg:id="lbl_nb_sep" dlg:tab-index="17" dlg:left="15" dlg:top="64" dlg:width="47" dlg:height="15" dlg:page="2" dlg:align="right" dlg:valign="center"/>
<dlg:button dlg:id="topage2" dlg:tab-index="19" dlg:left="130" dlg:top="116" dlg:width="50" dlg:height="16" dlg:page="1">
<dlg:text dlg:style-id="1" dlg:id="pygments_ver" dlg:tab-index="14" dlg:left="18" dlg:top="140" dlg:width="85" dlg:height="8" dlg:value="Build upon pygments {}"/>
<dlg:img dlg:style-id="2" dlg:id="pygments_logo" dlg:tab-index="15" dlg:left="5" dlg:top="139" dlg:width="11" dlg:height="9" dlg:scale-mode="isotropic" dlg:src="vnd.sun.star.extension://javahelps.codehighlighter/images/pygments.png"/>
<dlg:fixedline dlg:id="nb_line" dlg:tab-index="16" dlg:left="15" dlg:top="11" dlg:width="144" dlg:height="10" dlg:page="2"/>
<dlg:text dlg:id="lbl_nb_start" dlg:tab-index="17" dlg:left="15" dlg:top="25" dlg:width="47" dlg:height="15" dlg:page="2" dlg:align="right" dlg:valign="center"/>
<dlg:text dlg:id="lbl_nb_ratio" dlg:tab-index="18" dlg:left="15" dlg:top="42" dlg:width="47" dlg:height="15" dlg:page="2" dlg:align="right" dlg:valign="center"/>
<dlg:text dlg:id="lbl_nb_sep" dlg:tab-index="19" dlg:left="15" dlg:top="59" dlg:width="47" dlg:height="15" dlg:page="2" dlg:align="right" dlg:valign="center"/>
<dlg:fixedline dlg:id="cs_line" dlg:tab-index="20" dlg:left="15" dlg:top="81" dlg:width="144" dlg:height="10" dlg:page="2"/>
<dlg:text dlg:id="lbl_styleprefix" dlg:tab-index="21" dlg:left="15" dlg:top="95" dlg:width="75" dlg:height="15" dlg:page="2" dlg:align="right" dlg:valign="center"/>
<dlg:button dlg:id="topage2" dlg:tab-index="22" dlg:left="130" dlg:top="116" dlg:width="50" dlg:height="16" dlg:page="1">
<script:event script:event-name="on-performaction" script:macro-name="vnd.sun.star.UNO:topage2" script:language="UNO"/>
</dlg:button>
<dlg:text dlg:id="lbl_nb_ratio" dlg:tab-index="20" dlg:left="15" dlg:top="47" dlg:width="47" dlg:height="15" dlg:page="2" dlg:align="right" dlg:valign="center"/>
</dlg:bulletinboard>
</dlg:window>
21 changes: 16 additions & 5 deletions codehighlighter/python/highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ def create_dialog(self):
logger.debug("--> creating dialog ok.")

# set localized strings
controlnames = ("label_lang", "label_style", "check_col_bg", "check_charstyles", "check_linenb", "nb_line",
"lbl_nb_start", "lbl_nb_ratio", "lbl_nb_sep", "pygments_ver", "topage1", "topage2")
controlnames = ("label_lang", "label_style", "check_col_bg", "check_charstyles", "check_linenb", "nb_line", "cs_line",
"lbl_nb_start", "lbl_nb_ratio", "lbl_nb_sep", "lbl_styleprefix", "pygments_ver", "topage1", "topage2")
for controlname in controlnames:
dialog.getControl(controlname).Model.setPropertyValues(("Label", "HelpText"), self.strings[controlname])
# dialog.getControl("nb_sep").Model.HelpText = self.strings["nb_sep"][1]
Expand All @@ -285,6 +285,7 @@ def create_dialog(self):
nb_start = dialog.getControl('nb_start')
nb_ratio = dialog.getControl('nb_ratio')
nb_sep = dialog.getControl('nb_sep')
cs_prefix = dialog.getControl('cs_prefix')
pygments_ver = dialog.getControl('pygments_ver')

cb_lang.Text = self.options['Language']
Expand All @@ -303,6 +304,7 @@ def create_dialog(self):
nb_start.Value = self.options['LineNumberStart']
nb_ratio.Value = self.options['LineNumberRatio']
nb_sep.Text = self.options['LineNumberSeparator']
cs_prefix.Text = self.options['CharStylePrefix']
logger.debug("--> filling controls ok.")

def getextver():
Expand Down Expand Up @@ -337,6 +339,7 @@ def choose_options(self):
nb_start = int(dialog.getControl('nb_start').Value)
nb_ratio = int(dialog.getControl('nb_ratio').Value)
nb_sep = dialog.getControl('nb_sep').Text
cs_prefix = dialog.getControl('cs_prefix').Text

if lang != 'automatic' and lang.lower() not in self.all_lexer_aliases:
self.msgbox(self.strings["errlang"])
Expand All @@ -346,7 +349,7 @@ def choose_options(self):
return False
self.save_options(Style=style, Language=lang, ColourizeBackground=colorize_bg, ShowLineNumbers=show_linenb,
LineNumberStart=nb_start, LineNumberRatio=nb_ratio, LineNumberSeparator=nb_sep,
UseCharStyles=use_charstyles)
UseCharStyles=use_charstyles, CharStylePrefix=cs_prefix)
logger.debug("Dialog validated and options saved.")
logger.info(f"Updated options = {self.options}.")
return True
Expand Down Expand Up @@ -386,7 +389,13 @@ def addstyle(ttype):
try:
charstyles.insertByName(ttypename, newcharstyle)
except ElementExistException:
return
# redefine style only if user has given his own generic prefix
if self.options["CharStylePrefix"].strip():
newcharstyle = charstyles.getByName(ttypename)
newcharstyle.setPropertiesToDefault(("CharColor", "CharBackColor",
"CharWeight", "CharPosture", "CharUnderline"))
else:
return
if ttype.parent is not None:
parent = ttypename.rsplit('.', 1)[0]
if parent not in charstyles:
Expand Down Expand Up @@ -713,7 +722,9 @@ def highlight_code(self, code, cursor, lexer, style):
# create character styles if requested
# (this happens here to stay synched with undo context)
if self.options["UseCharStyles"]:
styleprefix = CHARSTYLEID + style.__name__.lower()[:-5]
styleprefix = self.options["CharStylePrefix"].strip()
if styleprefix == "":
styleprefix = CHARSTYLEID + style.__name__.lower()[:-5]
self.createcharstyles(style, styleprefix)
# caching consecutive tokens with same token type
logger.debug(f"Starting code block highlighting (lexer: {lexer}, style: {style}).")
Expand Down
12 changes: 8 additions & 4 deletions codehighlighter/python/pythonpath/ch2_i18n/__init__.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
CH2_STRINGS = {"en": { # dialog controls (label text + help text)
CH2_STRINGS = {"en": { # dialog controls (label text + help text)
"label_lang": ("Language", ""),
"label_style": ("Style", ""),
"check_charstyles": ("Use character ~styles in Writer", "When possible, code highlighting is based on character styles."),
"check_col_bg": ("Set ~background color", "Use the background color provided by the style."),
"check_linenb": ("Add ~line numbering", "Active or deactivate line numbers."),
"nb_line": ("Line numbering", ""),
"cs_line": ("Character styles", ""),
"lbl_nb_start": ("Start at", ""),
"lbl_nb_ratio": ("Height (%)", ""),
"lbl_nb_sep": ("Separator", ""),
"lbl_styleprefix": ("Custom prefix:", "Leave empty to use internal style names."),
"nb_sep": ("", ""),
"pygments_ver": ("Build upon Pygments {}", ""),
"topage1": ("Back", ""),
"topage2": ("More...", ""),
# messages
# messages
"errlang": "Unsupported language.",
"errstyle": "Unknown style.",
"errsel1": "Unsupported selection.",
"errsel2": "Nothing to highlight.",
},

"fr": { # contrôles de dilaogue (texte + texte d'aide)
"fr": { # contrôles de dilaogue (texte + texte d'aide)
"label_lang": ("Langage", ""),
"label_style": ("Style", ""),
"check_charstyles": ("Utiliser les ~styles de caractère dans Writer", "Si possible, le code ser colorisé sur base des styles de caractère."),
"check_col_bg": ("Utiliser la couleur de ~fond", "Utiliser l'arrière-plan fourni par le style."),
"check_linenb": ("Numéroter les ~lignes", "Affiche ou masque les numéros de ligne."),
"nb_line": ("Numéros de ligne", ""),
"cs_line": ("Styles de caractères", ""),
"lbl_nb_start": ("Démarrer à", ""),
"lbl_nb_ratio": ("Taille (%)", ""),
"lbl_nb_sep": ("Séparateur", ""),
"lbl_styleprefix": ("Préfixe personnalisé :", "Laisser vide pour utiliser les noms de style internes."),
"nb_sep": ("", ""),
"pygments_ver": ("Basé sur Pygments {}", ""),
"topage1": ("Retour", ""),
"topage2": ("Plus...", ""),
# messages
# messages
"errlang": "Langage non supporté.",
"errstyle": "Style inconnu.",
"errsel1": "Sélection invalide.",
Expand Down
Binary file modified codehighlighter2.oxt
Binary file not shown.

1 comment on commit 4dcf28c

@flywire
Copy link
Contributor

@flywire flywire commented on 4dcf28c Jul 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleancharstyles will not work with CHARSTYLEID for UserStylePrefix

Please sign in to comment.