jTidy pretty print custom HTML tag
Asked Answered
P

2

10

I'm trying to use JTidy to pretty print a well formed HTML generated by the user:

<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. 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.

 <!-- 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

Psychotechnics answered 21/5, 2015 at 12:47 Comment(2)
What version of JTidy are you using? Are you sure there is no other configuration? AFAIK setXmlTags(true) should solve this.Acarus
setXmlTags(true) actually solves this, but it starts trying to close self enclosing tags example <img></img>Psychotechnics
P
0

I have solved this problem by make some changes in JTidy source

https://github.com/nanndoj/jtidy

I have added a new configuration called 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

Psychotechnics answered 9/7, 2015 at 18:29 Comment(0)
A
3

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.

Acarus answered 2/7, 2015 at 21:1 Comment(1)
Thanks for your answer @manouti, but i'm writing a pretty formatter for a text editor. I can't assure what directives our users will define and use. It must be a generic solutionPsychotechnics
P
0

I have solved this problem by make some changes in JTidy source

https://github.com/nanndoj/jtidy

I have added a new configuration called 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

Psychotechnics answered 9/7, 2015 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.