Skip to content

Commit

Permalink
Add autoatic lexer guess
Browse files Browse the repository at this point in the history
  • Loading branch information
slgobinath committed Feb 23, 2018
1 parent 4c72d0c commit 57ac8b5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
22 changes: 18 additions & 4 deletions codehighlighter/Addons.xcu
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->

<oor:component-data oor:name="Addons" oor:package="org.openoffice.Office" xmlns:oor="http://openoffice.org/2001/registry" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<oor:component-data oor:name="Addons" oor:package="org.openoffice.Office"
xmlns:oor="http://openoffice.org/2001/registry"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<node oor:name="AddonUI">
<node oor:name="OfficeMenuBarMerging">
<node oor:name="javahelps.codehighlighter" oor:op="replace">
Expand All @@ -46,7 +47,21 @@
<value>Highlight Code</value>
</prop>
<node oor:name="Submenu">
<node oor:name="lang_1" oor:op="replace">
<node oor:name="lang_0" oor:op="replace">
<prop oor:name="Context" oor:type="xs:string">
<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>
</prop>
<prop oor:name="Title" oor:type="xs:string">
<value xml:lang="en">Automatic</value>
</prop>
<prop oor:name="URL" oor:type="xs:string">
<value>vnd.sun.star.script:codehighlighter.oxt|python|highlight.py$highlight_automatic_default?language=Python&amp;location=user:uno_packages</value>
</prop>
<prop oor:name="Target" oor:type="xs:string">
<value>_self</value>
</prop>
</node>
<node oor:name="lang_1" oor:op="replace">
<prop oor:name="Context" oor:type="xs:string">
<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>
</prop>
Expand Down Expand Up @@ -5352,7 +5367,6 @@
<value>_self</value>
</prop>
</node>

</node>
</node>
</node>
Expand Down
8 changes: 6 additions & 2 deletions codehighlighter/python/highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ def highlightSourceCode(lang, style):


def highlight_code(code, cursor, lang, style):
lexer = get_lexer_by_name(lang)
# lexer = guess_lexer(code)
if lang is None:
lexer = guess_lexer(code)
else:
lexer = get_lexer_by_name(lang)
style = styles.get_style_by_name(style)
for tok_type, tok_value in lexer.get_tokens(code):
cursor.goRight(len(tok_value), True) # selects the token's text
Expand All @@ -72,6 +74,8 @@ def highlight_code(code, cursor, lang, style):
finally:
cursor.goRight(0, False) # deselects the selected text

def highlight_automatic_default(*args):
highlightSourceCode(None, 'default')

def highlight_abap_default(*args):
highlightSourceCode('abap', 'default')
Expand Down

0 comments on commit 57ac8b5

Please sign in to comment.