Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to prettyprint document without adding newlines after a certain tag? #2141

Open
FishHawk opened this issue Jun 10, 2024 · 9 comments

Comments

@FishHawk
Copy link

I use jsoup to create epub. Here is the formatted xhtml file in the epub file. But some readers require that there should be no extra spaces or line breaks after <dc:language>. Is there a way to solve this?

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="pub-id">
 <metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
  <dc:identifier id="pub-id">
   id
  </dc:identifier>
  <dc:title>
   title
  </dc:title>
  <dc:language>
   ja
  </dc:language>
  <dc:description>
   balabala
  </dc:description>
 </metadata>
 <manifest></manifest>
 <spine toc="toc.ncx"></spine>
</package>
@jhy
Copy link
Owner

jhy commented Jul 1, 2024

Currently there's no configurable way to change this. The default for unknown tags is to parse them as blocks.

I think it might make sense to parse unknown tags as "block + format-as-inline". That would prevent the line wrap when pretty-printing.

Or, perhaps, try and infer it by checking if the children are defined block tags or not.

@FishHawk
Copy link
Author

FishHawk commented Jul 2, 2024

Got it. I'll just use string replacement for now.

@jhy
Copy link
Owner

jhy commented Jul 9, 2024

I took a pass at this (particularly, attempting to introduce a rule that only indents if an element contains other elements, but not if (all the other existing rules)). But it got too gnarly, as the rule needs to be implemented differently in each of Element outerHead, Element outerTail, and TextNode outerHead.

So, I'm declaring bankruptcy on the current pretty-printer implementation, and won't make other changes to it. I plan on refactoring it at some point to a strategy type object (which will provide more configurability) that will also maintain more state during the pass (a stack of what was indented, what requires white-space preservation, etc). With the goal of substantially simplifying the implementation and allowing better control, both by default and by users.

But for now, yeah, string replacement is your best bet...

@anonyein
Copy link

anonyein commented Jul 15, 2024

I took a pass at this (particularly, attempting to introduce a rule that only indents if an element contains other elements, but not if (all the other existing rules)). But it got too gnarly, as the rule needs to be implemented differently in each of Element outerHead, Element outerTail, and TextNode outerHead.

So, I'm declaring bankruptcy on the current pretty-printer implementation, and won't make other changes to it. I plan on refactoring it at some point to a strategy type object (which will provide more configurability) that will also maintain more state during the pass (a stack of what was indented, what requires white-space preservation, etc). With the goal of substantially simplifying the implementation and allowing better control, both by default and by users.

But for now, yeah, string replacement is your best bet...

I support removing pretty print as default, this feature makes lots of trouble, such as
org.jsoup.Jsoup.parse("<a>a a</a>").select("a").text()
which makes the doule whitespaces in "a a" joined into only single one,
and this may bring in big trouble in practical applications!

@jhy
Copy link
Owner

jhy commented Jul 15, 2024

@anonyein I have covered this before in other issues: I prefer the pretty printer being on by default for HTML, and don't intend on changing that. It is off by default when using the XML parser, as we don't know the whitespace significance in XML. But we do in HTML and it generally appropriate and safe when serializing for downstream HTML parsers.

Let's keep this issue for the specific output change noted.

@anonyein
Copy link

@anonyein I have covered this before in other issues: I prefer the pretty printer being on by default for HTML, and don't intend on changing that. It is off by default when using the XML parser, as we don't know the whitespace significance in XML. But we do in HTML and it generally appropriate and safe when serializing for downstream HTML parsers.

Let's keep this issue for the specific output change noted.

I see. THX

@anonyein
Copy link

anonyein commented Jul 15, 2024

@anonyein I have covered this before in other issues: I prefer the pretty printer being on by default for HTML, and don't intend on changing that. It is off by default when using the XML parser, as we don't know the whitespace significance in XML. But we do in HTML and it generally appropriate and safe when serializing for downstream HTML parsers.

Let's keep this issue for the specific output change noted.
@jhy
Jsoup.parse("<a>a a</a>", "", Parser.xmlParser()).select("a").text()
I still cannot get "a a"

@jhy
Copy link
Owner

jhy commented Jul 15, 2024

@anonyein like I said, please keep this issue focussed on the specific issue, not general pretty-print issues.

At any rate, .text() is completely unrelated from the pretty-printer. So any settings or discussions here are irrelevant. The pretty-printer is used only in the .html() methods.

If you read the Element.text() documentation you'll see it acts as described, and how to get non-normalized text.

@anonyein
Copy link

anonyein commented Jul 15, 2024

@anonyein like I said, please keep this issue focussed on the specific issue, not general pretty-print issues.

At any rate, .text() is completely unrelated from the pretty-printer. So any settings or discussions here are irrelevant. The pretty-printer is used only in the .html() methods.

If you read the Element.text() documentation you'll see it acts as described, and how to get non-normalized text.

I get it, using "wholeText" instead. Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants