GithubHelp home page GithubHelp logo

Expose xmlTextWriterSetIndent about rust-libxml HOT 9 CLOSED

kwarc avatar kwarc commented on July 17, 2024 1
Expose xmlTextWriterSetIndent

from rust-libxml.

Comments (9)

dginev avatar dginev commented on July 17, 2024

Sounds quite reasonable to add, thanks for the request!

from rust-libxml.

dginev avatar dginev commented on July 17, 2024

Taking a look, the xmlTextWriter API is completely separate from the xmlSave API, so we can't really request indentation cleanly via the SetIndent method, while still using xmlSaveToBuffer.

I think the only viable approach is resorting to their global variable xmlIndentTreeOutput, in combination with setting SaveOptions.format to true. Using a global makes this a little iffy though, but worth taking another look...

from rust-libxml.

dginev avatar dginev commented on July 17, 2024

Actually, @matthew-nichols-westtel , could you offer an example/test where setting the format flag fails your expectations? A quick snippet from the tests is:

  let doc_str_formatted = doc.to_string_with_options(SaveOptions {
    format: true,
    ..SaveOptions::default()
  });

Which comes out nicely indented over the unformatted.xml test file. I have vague memories that there were a number of fine details in the way libxml handles spacing, but it has been way too long to remember them sharply.

from rust-libxml.

matthew-nichols-westtel avatar matthew-nichols-westtel commented on July 17, 2024

If you change the example to be slightly different it doesn't format entirely:

    let parser = Parser { format: ParseFormat::XML };
    let doc = parser.parse_string(r#"<r:root xmlns:h="http://example.com/ns/hello" xmlns:f="http://example.com/ns/farewell" xmlns:r="http://example.com/ns/root"><h:table><h:tr><h:td>col 1</h:td><h:td>col 2</h:td></h:tr> </h:table>
<f:mock><f:doublemock /></f:mock><f:footer><h:table><h:tr><h:td>col 3</h:td><f:footer> nested f</f:footer></h:tr></h:table></f:footer></r:root>"#).unwrap();
    println!("{}", doc.to_string_with_options(SaveOptions{format: true, no_declaration: true, ..Default::default()}));

Aka if there are existing spaces/newlines/etc it doesn't format as expected, this creates

<r:root xmlns:h="http://example.com/ns/hello" xmlns:f="http://example.com/ns/farewell" xmlns:r="http://example.com/ns/root"><h:table><h:tr><h:td>col 1</h:td><h:td>col 2</h:td></h:tr> </h:table>
<f:mock><f:doublemock/></f:mock><f:footer><h:table><h:tr><h:td>col 3</h:td><f:footer> nested f</f:footer></h:tr></h:table></f:footer></r:root>

whereas xmllint --format still creates

<?xml version="1.0"?>
<r:root xmlns:h="http://example.com/ns/hello" xmlns:f="http://example.com/ns/farewell" xmlns:r="http://example.com/ns/root">
  <h:table>
    <h:tr>
      <h:td>col 1</h:td>
      <h:td>col 2</h:td>
    </h:tr>
  </h:table>
  <f:mock>
    <f:doublemock/>
  </f:mock>
  <f:footer>
    <h:table>
      <h:tr>
        <h:td>col 3</h:td>
        <f:footer> nested f</f:footer>
      </h:tr>
    </h:table>
  </f:footer>
</r:root>

from rust-libxml.

dginev avatar dginev commented on July 17, 2024

Thanks for the example! Will give it another try soon.

from rust-libxml.

dginev avatar dginev commented on July 17, 2024

Alright, found out the little detail that makes xmllint --format work. It sets xmlKeepBlanksDefault(0), immediately, which importantly has effect on parsing the input rather than serializing the output. Once the input spaces are discarded, the regular format serialization (that we already support in the wrapper) indents successfully.

So, defying intuition, in order to get an indented output, a libxml2 user must already prepare that before initializing the parser responsible for manipulating the input. I also have a vague memory that there are good "whitespaces are sometimes meaningful in XML" reasons for doing this.

So maybe we can reframe this issue to:

  1. Allow for passing in options to Parser
  2. Document how to indent difficult cases.

Not sure we can do much else with the way libxml2 is... Whether we use the xmlsave or xmlwriter serialization API, they both reach the same code path, and both require the blanks to have been discarded as part of parsing the input.

from rust-libxml.

matthew-nichols-westtel avatar matthew-nichols-westtel commented on July 17, 2024

That's fine.

Oddly enough I can confirm that

unsafe {
    libxml::bindings::xmlKeepBlanksDefault(0);
}

works with parser.parse_string and not parser.parse_file.

Also looking at the kind of files I'm working with I'd like to keep blank lines that were in the original (much like many code formatters do) though I don't currently know how to achieve that.

from rust-libxml.

dginev avatar dginev commented on July 17, 2024

Right, the parser code is still in a "minimal viable product" shape, you can't customize it or do anything advanced with it. And as you mention, parse_file even goes out of its way to force the blanks to always be there (code here).

So evolving that to accept some options will allow getting the xmllint behaviour. If you want to do something even better than libxml2's algorithm however, you're probably best off writing your own custom serializer, which may end up less confusing in the long run. The main project I've been porting to Rust which uses libxml2 does exactly that, to ensure it formats the XML exactly per the author's taste (which is what the whitespace considerations largely are).

from rust-libxml.

matthew-nichols-westtel avatar matthew-nichols-westtel commented on July 17, 2024

Yeah that's probably what I'll have to do eventually.

Thanks for the help!

from rust-libxml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.