【发布时间】:2020-07-03 06:57:42
【问题描述】:
我最近一直在写 markdown 文件,并且每天都在使用很棒的 table of content generator (github-markdown-toc) 工具/脚本,但我希望它能够自动重新生成每次我按下 Ctrl+s 时,就在我的 sublime3 环境中保存 md 文件之前。
到目前为止,我所做的是手动从 shell 生成它,使用:
gh-md-toc --insert my_file.md
所以我写了一个简单的插件,但由于某种原因我看不到我想要的结果。 我看到我的打印,但没有生成目录。 有人有什么建议吗?怎么了?
import sublime, sublime_plugin
import subprocess
class AutoRunTOCOnSave(sublime_plugin.EventListener):
""" A class to listen for events triggered by ST. """
def on_post_save_async(self, view):
"""
This is called after a view has been saved. It runs in a separate thread
and does not block the application.
"""
file_path = view.file_name()
if not file_path:
return
NOT_FOUND = -1
pos_dot = file_path.rfind(".")
if pos_dot == NOT_FOUND:
return
file_extension = file_path[pos_dot:]
if file_extension.lower() == ".md": #
print("Markdown TOC was invoked: handling with *.md file")
subprocess.Popen(["gh-md-toc", "--insert ", file_path])
【问题讨论】:
-
你写的插件?!那是我为回答 StackOverflow Auto-run a command on saving in Sublime Text 问题而编写和发布的插件。充其量这是非常糟糕的网络礼仪 JammingThebBits。
标签: python sublimetext3 sublime-text-plugin