def _run_queued_item(self, buffer_id, item): try: # Render text and save to cache filename = os.path.basename(item.fullpath) dirname = os.path.dirname(item.fullpath) html_part = RendererManager.render_text(item.fullpath, item.lang, item.text) entry = RenderedMarkupCacheEntry(filename=filename, dirname=dirname, html_part=html_part) RenderedMarkupCache.instance().set_entry(buffer_id, entry) except NotImplementedError: pass except: log.exception("")
def handler_api_query(): """ Querying for updates """ entry = None try: obj = request.json buffer_id = obj['buffer_id'] timestamp = str(obj['timestamp']) storage = RenderedMarkupCache.instance() entry = storage.get_entry(buffer_id) except: pass if entry is None: return { 'timestamp': -1, 'filename': '', 'dirname': '', 'html_part': None } if entry.timestamp == timestamp: # Keep old entry return { 'timestamp': entry.timestamp, 'filename': None, 'dirname': None, 'html_part': None } else: return { 'timestamp': entry.timestamp, 'filename': entry.filename, 'dirname': entry.dirname, 'html_part': entry.html_part }
def run(self, remove_all=False): storage = RenderedMarkupCache.instance() if remove_all: storage.clean() return keep_ids_list = [] for window in sublime.windows(): for view in window.views(): keep_ids_list.append(view.buffer_id()) storage.clean(keep_ids=set(keep_ids_list))
def queue_view(cls, view, only_exists=False, immediate=False): buffer_id = view.buffer_id() settings = view.settings() if only_exists and not RenderedMarkupCache.instance().exists(buffer_id): # If current view is previously rendered, then ignore 'only_exists' if not settings.get('omnimarkup_enabled', False): return settings.set('omnimarkup_enabled', True) region = sublime.Region(0, view.size()) text = view.substr(region) lang = cls.get_lang_by_scope_name(view.scope_name(0)) cls.WORKER.queue(buffer_id, view.file_name(), lang, text, immediate=immediate)
def render_text_by_buffer_id(buffer_id): valid_view = None for window in sublime.windows(): if valid_view is not None: break for view in window.views(): if view.buffer_id() == buffer_id: valid_view = view break if valid_view is None: return None RendererManager.queue_view(valid_view, immediate=True) return RenderedMarkupCache.instance().get_entry(buffer_id)
def handler_view(buffer_id): # A browser refresh always get the latest result f = Future(render_text_by_buffer_id, buffer_id) sublime.set_timeout(f, 0) entry = f.result() entry = entry or RenderedMarkupCache.instance().get_entry(buffer_id) if entry is None: return bottle.HTTPError( 404, 'buffer_id(%d) is not valid (not open or unsupported file format)' % buffer_id ) setting = Setting.instance() return template(setting.html_template_name, buffer_id=buffer_id, ajax_polling_interval=setting.ajax_polling_interval, mathjax_enabled=setting.mathjax_enabled, **entry)
def queue_view(cls, view, only_exists=False, immediate=False): buffer_id = view.buffer_id() settings = view.settings() if only_exists and not RenderedMarkupCache.instance().exists( buffer_id): # If current view is previously rendered, then ignore 'only_exists' if not settings.get('omnimarkup_enabled', False): return settings.set('omnimarkup_enabled', True) region = sublime.Region(0, view.size()) text = view.substr(region) lang = cls.get_lang_by_scope_name(view.scope_name(0)) cls.WORKER.queue(buffer_id, view.file_name(), lang, text, immediate=immediate)
def handler_view(buffer_id): # A browser refresh always get the latest result f = Future(render_text_by_buffer_id, buffer_id) sublime.set_timeout(f, 0) entry = f.result() entry = entry or RenderedMarkupCache.instance().get_entry(buffer_id) if entry is None: return bottle.HTTPError( 404, 'buffer_id(%d) is not valid (not open or unsupported file format)' % buffer_id) setting = Setting.instance() return template(setting.html_template_name, buffer_id=buffer_id, ajax_polling_interval=setting.ajax_polling_interval, mathjax_enabled=setting.mathjax_enabled, **entry)