python - 每次在文件中找到一个单词时,如何让我的文本编辑器 Python 插件执行?

标签 python html css twitter-bootstrap plugins

我为我的学校元素创建了一个简单的插件。它会寻找 Bootstrap 类名( col-1container 等)并自动在 style.css 中的这些类名后面写入 CSS 样式命令文件。

so the idea is that you don't actually need bootstrap's link in the header and your style.css file isn't full of unnecessary CSS commands as all the Bootstrap components are already in your text editor.

例如: <div class="shadow p-3 mb-5 bg-white rounded">Regular shadow</div>

因为上面的div有shadow p-3 mb-5 bg-white rounded作为其 Bootstrap 类名,我的插件将在 style.css 中写入这五个 CSS 样式。文件。

既然已经清楚了,我想问一下……如何让脚本真正起作用??我的意思是我有代码,当我在终端中运行它时它工作得很好 但是 我如何在我的文本编辑器中设置插件并让它在每次找到关键字时运行HTML 文件?

一段代码:

from collections import OrderedDict
keyword = {
"row": '''
    .row {
    display: -ms-flexbox;
    display: flex; 
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    margin-right: -15px;
    margin-left: -15px;
    }'''
    #etc
        }   

with open('index.html', 'r') as file:
   with open('style.css', 'a') as newfile:
      lines = file.readlines()
      for line in lines:
         if 'class="' in line:
            to_replace = line.split('"')[1].split()
            to_replace = OrderedDict.fromkeys(to_replace)
            for key in to_replace:
                if key in keyword:
                    newfile.write(keyword[key])
                    keyword[key] = ''

HTML 文件:

<div class="row"></div> etc

style.css 中输出:

.row {
        display: -ms-flexbox;
        display: flex; 
        -ms-flex-wrap: wrap;
        flex-wrap: wrap;
        margin-right: -15px;
        margin-left: -15px;
        }

此外,此脚本中是否有任何漏洞(我确定有)?你能指出它们吗?

最佳答案

如果您希望它在您键入时实时更新,答案与 Python 无关,更多的是为您的特定文本编辑器编写自定义插件。

但是,如果您对它只在每次保存时运行感到满意,您可以相当简单地使用类似 watchdog 的东西来获得该功能。模块。 on_modified事件处理程序可能就是您所追求的。

关于python - 每次在文件中找到一个单词时,如何让我的文本编辑器 Python 插件执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54453766/

相关文章:

javascript - 如何在 CSS 中获得鼠标移出效果

python - 让 python 生成器在后台运行

python - 获取 Google 群组成员

javascript - 如何缩小大于 100MB 的 Javascript 文件?

javascript - 为什么可以提交这个表单? (火狐)

javascript - 我得到了一个包含 JS、HTML 和 CSS3 的代码片段。我如何将它插入到 wordpress html-widget 中?

python - 如何将非字符串值分配给变量

python - 如何捕获子进程错误

html - 有没有办法让这个ribbon成为纯css的ribbon?又名 : can I just add a ribbon-updated class

javascript - 鼠标悬停时的元素按键