Skip to content

Commit

Permalink
new plugin to render inline latex on hover
Browse files Browse the repository at this point in the history
  • Loading branch information
315234 committed Jun 30, 2016
1 parent 6694d8d commit 162b95a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
43 changes: 43 additions & 0 deletions InlineLatexHover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import sublime
import sublime_plugin

import urllib.parse
import requests
from base64 import b64encode

class InlineLatexHover(sublime_plugin.EventListener):
@classmethod
def is_applicable(cls, settings):
return settings.get('syntax').contains("Fortran")

def on_hover(self, view, point, hover_zone):
if hover_zone == sublime.HOVER_TEXT:
scope = view.scope_name(point)
score = sublime.score_selector(scope, "meta.inline_latex.fortran")
if score > 0:
# We are hovering over some embedded latex
region = InlineLatexHover.extract_inline_latex_scope(view, point)
latex = view.substr(region)
# todo: get colors from theme popupCss
bg, fg = 'ffffff', '222222'
params = urllib.parse.urlencode({'cht': "tx", 'chl': latex, 'chf': 'bg,s,'+bg, 'chco': fg})
imgurl = "http://chart.googleapis.com/chart?"+params
response = requests.get(imgurl)
imgdata = b64encode(response.content).decode()
html_str = '<img src="data:image/png;base64,%s" />' % imgdata
view.show_popup(html_str, sublime.HIDE_ON_MOUSE_MOVE, point)

@staticmethod
def extract_inline_latex_scope(view, point):
"""Like extract_scope(), but extracts the extent of meta.inline_latex.fortran."""
ltxscope = "meta.inline_latex.fortran"
istart = point
iend = point
while istart > 0 and sublime.score_selector(view.scope_name(istart-1), ltxscope) > 0:
istart = istart - 1
while iend < view.size() and sublime.score_selector(view.scope_name(iend), ltxscope) > 0:
iend = iend + 1
r = sublime.Region(istart, iend)
if r.size() > 1000:
r = sublime.Region(point, point)
return r
5 changes: 3 additions & 2 deletions grammars/FortranModern.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,9 @@ contexts:
- match: (?=$)
pop: true
comment_inline_latex:
- meta_scope: meta.inline_latex.fortran
- meta_content_scope: markup.bold
# disable meta_scope to distinguish between adjacent inline latex regions
# - meta_scope: meta.inline_latex.fortran
- meta_content_scope: meta.inline_latex.fortran markup.bold
- match: \\\w+
scope: markup.italic
- match: \$
Expand Down

0 comments on commit 162b95a

Please sign in to comment.