简体   繁体   English

jTidy漂亮的打印自定义HTML标记

[英]jTidy pretty print custom HTML tag

I'm trying to use JTidy to pretty print a well formed HTML generated by the user: 我正在尝试使用JTidy来打印由用户生成的格式良好的HTML:

<div class="component-holder ng-binding ng-scope ui-draggable ui-draggable-handle" data-component="cronos-datasource" id="cronos-datasource-817277">
    <datasource name="" entity="" key="" endpoint="" rows-per-page="">
        <i class="cpn cpn-datasource"></i>
    </datasource>
</div>

This is my config: 这是我的配置:

Tidy tidy = new Tidy();
tidy.setXHTML(true);
tidy.setIndentContent(true);
tidy.setPrintBodyOnly(true);
tidy.setTidyMark(false);
tidy.setWraplen(2000);
tidy.setDropProprietaryAttributes(false);
tidy.setDropEmptyParas(false);
tidy.setTrimEmptyElements(false);

But jTidy is removing my AngularJS datasource directive. 但是jTidy正在删除我的AngularJS datasource指令。 Is there a way to workarround this issue? 有没有办法解决这个问题?

I'm getting this from the log: 我从日志中得到这个:

line 1 column 191 - Error: <datasource> is not recognized!
line 1 column 191 - Warning: discarding unexpected <datasource>

Removing tidy.setXHTML(true) or setting it to false and adding tidy.setXmlTags(true) actually solve this issue and it start to consider user defined tags, but this is not a good solution because JTidy starts trying to close self enclosing tags. 删除tidy.setXHTML(true)或将其设置为false并添加tidy.setXmlTags(true)实际上解决了这个问题并开始考虑用户定义的标记,但这不是一个好的解决方案,因为JTidy开始尝试关闭自封闭标记。

 <!-- this code -->
 <img src="anythig.jpg"/>
 <div id="anyid"></div> 

 <!-- will become -->
 <img src="anythig.jpg">
     <div id="anyid"></div>
 </img>

I need a formatter for a text editor. 我需要一个用于文本编辑器的格式化程序。 I can't assure what directives our users will define and use. 我不能保证我们的用户将定义和使用哪些指令。 It must be a generic solution which works for any user defined directive 它必须是适用于任何用户定义指令的通用解决方案

Try setting the following property after your current configuration: 尝试在当前配置后设置以下属性:

Properties props = new Properties();
props.setProperty("new-blocklevel-tags", "datasource");
tidy.getConfiguration().addProps(props);

See http://tidy.sourceforge.net/docs/quickref.html#new-blocklevel-tags . 请参见http://tidy.sourceforge.net/docs/quickref.html#new-blocklevel-tags

I have solved this problem by make some changes in JTidy source 我通过在JTidy源中进行一些更改来解决这个问题

https://github.com/nanndoj/jtidy https://github.com/nanndoj/jtidy

I have added a new configuration called dropProprietaryTags 我添加了一个名为dropProprietaryTags的新配置

tidy.setDropProprietaryTags(false);

Now it's working fine for me. 现在它对我来说很好。 It's set to true by default so JTidy can work in the old way if the new property is not set to false 它默认设置为true ,因此如果new属性未设置为false ,JTidy可以以旧方式工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM