如何使用Eclipse插件修改HTML源代码?

时间:2023-01-24 16:49:01

I want to write an Eclipse plugin to modify HTML source files, eg. find and change an href or an src depending on certain circumstances. I'm fairly new to Eclipse, but I searched far and wide for something that can accomplish this and couldn't find anything of the sort, the only APIs I could find were to modify Java code.

我想编写一个Eclipse插件来修改HTML源文件,例如。根据特定情况查找并更改href或src。我是Eclipse的新手,但我搜索的东西可以做到这一点并且找不到任何类型的东西,我能找到的唯一的API就是修改Java代码。

Is there any API or maybe an open source Eclipse plugin that I could modify to accomplish this?

有没有API或者我可以修改的开源Eclipse插件来完成这个?

2 个解决方案

#1


2  

If you're writing an Eclipse plug-in, you can make use of APIs (caveats below) from the Web Tools Platform (WTP) HTML plug-ins. A headless plug-in will require:

如果您正在编写Eclipse插件,则可以使用Web Tools Platform(WTP)HTML插件中的API(下面的警告)。无头插件需要:

  • org.eclipse.wst.html.core
  • org.eclipse.wst.xml.core
  • org.eclipse.wst.sse.core
  • org.eclipse.text
  • org.eclipse.core.resources

It's a lot of dependencies to take on, but these are the same models that run under WTP's HTML Editor (and most of the editors provided by WTP, aside from the JavaScript tools).

这需要很多依赖,但这些是在WTP的HTML编辑器下运行的相同模型(以及WTP提供的大多数编辑器,除了JavaScript工具)。

import org.eclipse.wst.sse.core.internal.provisional.IModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.eclipse.jface.text.IDocument;
import org.w3c.dom.Element;
...
IModelManager modelManager = StructuredModelManager.getModelManager();
IDOMModel model = null;
try {
    model = (IDOMModel) modelManager.getModelForEdit(anIFile);
    // W3C-like DOM manipulation
    IDOMDocument doc = model.getDocument();
    Element ele = doc.createElement(HTML40Namespace.ElementName.P);
    doc.appendChild(ele);
    // JFace IDocument compatibility
    IDocument textDocument = model.getStructuredDocument();
    textDocument .replace(0, textDocument .getLength(), "<tag>some text</tag>");
    Element ele2 = doc.createElement(HTML40Namespace.ElementName.P);
    doc.appendChild(ele2);
    /* You can do more with either, or both, mechanisms here. DOM
     * changes are reflected in the text immediately and vice versa,
     * with a best effort by the DOM side if the source itself is
     * "broken".
     */
}
finally {
    if (model != null) {
        model.save();
        model.releaseFromEdit();
    }
}
  • Once loaded, you can modify the file contents using Eclipse's JFace Document APIs or something very close to the W3C DOM API. Our models are fully modifiable as text documents, and some of the W3C APIs weren't built with that in mind. There are also a few historic implementation mistakes on our part, e.g. the XML declaration has the wrong node type in the XML DOM.
  • 加载后,您可以使用Eclipse的JFace Document API或非常接近W3C DOM API的内容修改文件内容。我们的模型可以完全修改为文本文档,而一些W3C API并没有考虑到这些。我们还有一些历史性的实施错误,例如: XML声明在XML DOM中具有错误的节点类型。

  • Some required classes are still in internal or provisional packages for legacy binary compatibility. Changing them would break an unknown amount of downstream plug-ins.
  • 一些必需的类仍然是内部或临时包,用于传统二进制兼容性。更改它们会破坏未知数量的下游插件。

  • Neon handles AngularJS-style attribute names better than prior releases, if that matters.
  • 如果重要的话,Neon比以前的版本更好地处理AngularJS样式的属性名称。

  • CSS and JavaScript sections and attribute values should automatically be handled and out of your way.
  • CSS和JavaScript部分以及属性值应该自动处理并且不受影响。

  • If you put org.eclipse.wst.sse.ui/actioncontributor/debugstatusfields=true (it's a trace option) into a file and use the file as the argument value to -debug when launching Eclipse, supported file types will have an extra field in the status bar that shows the selected text offset in the open editor. Double-clicking it will open some nerdy info about that selection. You can even set this up for your actual installation; aside from the numbers being shown, the only performance impact is when you double-click there. 如何使用Eclipse插件修改HTML源代码?
  • 如果将org.eclipse.wst.sse.ui / actioncontributor / debugstatusfields = true(它是一个跟踪选项)放入文件并在启动Eclipse时将该文件用作-debug的参数值,则支持的文件类型将具有额外的字段在状态栏中,在打开的编辑器中显示所选的文本偏移量。双击它将打开一些有关该选择的书呆子信息。您甚至可以为实际安装进行设置;除了显示的数字外,唯一的性能影响是双击那里。

When launching from Eclipse to test your plug-in, you can just set it from the launch config dialog:

从Eclipse启动以测试插件时,您只需从启动配置对话框中进行设置:

如何使用Eclipse插件修改HTML源代码?

#2


-1  

It seems that you didn't do your due diligence in respect to searching before asking a question.

在提问之前,您似乎没有对搜索进行尽职调查。

There already is a plugin for html in eclipse called HTML Editor.

在eclipse中已经有一个名为HTML Editor的html插件。

This link is a previous stack overflow question that is the same as yours.

此链接是先前的堆栈溢出问题,与您的相同。

#1


2  

If you're writing an Eclipse plug-in, you can make use of APIs (caveats below) from the Web Tools Platform (WTP) HTML plug-ins. A headless plug-in will require:

如果您正在编写Eclipse插件,则可以使用Web Tools Platform(WTP)HTML插件中的API(下面的警告)。无头插件需要:

  • org.eclipse.wst.html.core
  • org.eclipse.wst.xml.core
  • org.eclipse.wst.sse.core
  • org.eclipse.text
  • org.eclipse.core.resources

It's a lot of dependencies to take on, but these are the same models that run under WTP's HTML Editor (and most of the editors provided by WTP, aside from the JavaScript tools).

这需要很多依赖,但这些是在WTP的HTML编辑器下运行的相同模型(以及WTP提供的大多数编辑器,除了JavaScript工具)。

import org.eclipse.wst.sse.core.internal.provisional.IModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.eclipse.jface.text.IDocument;
import org.w3c.dom.Element;
...
IModelManager modelManager = StructuredModelManager.getModelManager();
IDOMModel model = null;
try {
    model = (IDOMModel) modelManager.getModelForEdit(anIFile);
    // W3C-like DOM manipulation
    IDOMDocument doc = model.getDocument();
    Element ele = doc.createElement(HTML40Namespace.ElementName.P);
    doc.appendChild(ele);
    // JFace IDocument compatibility
    IDocument textDocument = model.getStructuredDocument();
    textDocument .replace(0, textDocument .getLength(), "<tag>some text</tag>");
    Element ele2 = doc.createElement(HTML40Namespace.ElementName.P);
    doc.appendChild(ele2);
    /* You can do more with either, or both, mechanisms here. DOM
     * changes are reflected in the text immediately and vice versa,
     * with a best effort by the DOM side if the source itself is
     * "broken".
     */
}
finally {
    if (model != null) {
        model.save();
        model.releaseFromEdit();
    }
}
  • Once loaded, you can modify the file contents using Eclipse's JFace Document APIs or something very close to the W3C DOM API. Our models are fully modifiable as text documents, and some of the W3C APIs weren't built with that in mind. There are also a few historic implementation mistakes on our part, e.g. the XML declaration has the wrong node type in the XML DOM.
  • 加载后,您可以使用Eclipse的JFace Document API或非常接近W3C DOM API的内容修改文件内容。我们的模型可以完全修改为文本文档,而一些W3C API并没有考虑到这些。我们还有一些历史性的实施错误,例如: XML声明在XML DOM中具有错误的节点类型。

  • Some required classes are still in internal or provisional packages for legacy binary compatibility. Changing them would break an unknown amount of downstream plug-ins.
  • 一些必需的类仍然是内部或临时包,用于传统二进制兼容性。更改它们会破坏未知数量的下游插件。

  • Neon handles AngularJS-style attribute names better than prior releases, if that matters.
  • 如果重要的话,Neon比以前的版本更好地处理AngularJS样式的属性名称。

  • CSS and JavaScript sections and attribute values should automatically be handled and out of your way.
  • CSS和JavaScript部分以及属性值应该自动处理并且不受影响。

  • If you put org.eclipse.wst.sse.ui/actioncontributor/debugstatusfields=true (it's a trace option) into a file and use the file as the argument value to -debug when launching Eclipse, supported file types will have an extra field in the status bar that shows the selected text offset in the open editor. Double-clicking it will open some nerdy info about that selection. You can even set this up for your actual installation; aside from the numbers being shown, the only performance impact is when you double-click there. 如何使用Eclipse插件修改HTML源代码?
  • 如果将org.eclipse.wst.sse.ui / actioncontributor / debugstatusfields = true(它是一个跟踪选项)放入文件并在启动Eclipse时将该文件用作-debug的参数值,则支持的文件类型将具有额外的字段在状态栏中,在打开的编辑器中显示所选的文本偏移量。双击它将打开一些有关该选择的书呆子信息。您甚至可以为实际安装进行设置;除了显示的数字外,唯一的性能影响是双击那里。

When launching from Eclipse to test your plug-in, you can just set it from the launch config dialog:

从Eclipse启动以测试插件时,您只需从启动配置对话框中进行设置:

如何使用Eclipse插件修改HTML源代码?

#2


-1  

It seems that you didn't do your due diligence in respect to searching before asking a question.

在提问之前,您似乎没有对搜索进行尽职调查。

There already is a plugin for html in eclipse called HTML Editor.

在eclipse中已经有一个名为HTML Editor的html插件。

This link is a previous stack overflow question that is the same as yours.

此链接是先前的堆栈溢出问题,与您的相同。