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

Easier Interface for Creating XML Documents? #25

Closed
MarkNahabedian opened this issue May 18, 2024 · 7 comments
Closed

Easier Interface for Creating XML Documents? #25

MarkNahabedian opened this issue May 18, 2024 · 7 comments

Comments

@MarkNahabedian
Copy link

I use XML.jl to construct HTML files and SVG graphics programatically. I've found using just the XML.jl constructors to be cumbersome though.

It's a hack, but for several of my projects I've defined
https://github.com/MarkNahabedian/SquareDanceReasoning.jl/blob/main/src/xml/elt.jl.
the function elt allows using nesting of expressions to nest elements and attributes. It also exploits do syntax to allow one to run additional code that adds elements and attributes if one can't wedge them into the expression hierarchy.

I was wondering if XML.jl might eventually adopt some easier way to construct and nest XML elements. I don't think my implementation linked above has a clean enough interface for general use, but I have found it very convenient.

Thanks.

@joshday
Copy link
Member

joshday commented May 18, 2024

I just realized it's not released, but on main there is a simpler syntax

https://github.com/JuliaComputing/XML.jl?tab=readme-ov-file#writing-element-nodes-with-xmlh

Is that sufficient for you? I'll get a release out soon.

@MarkNahabedian
Copy link
Author

Could you point me at the source code for h? I looked for it earlier but couldn't find it. The documentation lacks detail. If I read the code maybe I could make a suggestion.

Thanks.

@joshday
Copy link
Member

joshday commented May 18, 2024

There's not much to add. The example demonstrates this:

h.tag(children...; attributes) creates <tag attributes...>children...</tag>

Here's the source:

h(tag::Union{Symbol, String}, children...; kw...) = Node(Element, tag, kw, nothing, children)

@eoteroe
Copy link

eoteroe commented May 20, 2024

I was hoping to have a fast XML creator with low allocation... but It has been really tough try to understand the API. Headache. Hope you can give more details or examples.

@MarkNahabedian
Copy link
Author

It's unfrtunate that there are no doc strings and the examples in the README aren't general enough.

I just updated what version of XML.jl I'm using and I wanted to give the new h thing a try, so here's an example:

Note that h is just a shorthand for Node(XML.Element, tagname, ...) (See the method list for arguments).

using XML
using XML: h

doc = h.html([
    h.head([
        h.title(["My Document"])
    ])
    h.body([
        h.h1(["My Document"]),
        h.p(["This is a paragraph."]; class="MaybeStyyles"),
        h.ul([
            h.li(["Item 1"]),
            h.li(["Item 2"])
            ])
    ])
])

XML.write(doc)

I have no clue why the document isn't getting written properly. It works in my package code. I'll leave that to the folks who are paid to work on this package.

@joshday
Copy link
Member

joshday commented May 21, 2024

@MarkNahabedian Almost there! You don't need to wrap children in a Vector.

using XML
using XML: h

doc = h.html(
    h.head(
        h.title("My Document")
    ),
    h.body(
        h.h1("My Document"),
        h.p("This is a paragraph."; class="MaybeStyyles"),
        h.ul(
            h.li("Item 1"),
            h.li("Item 2")
        )
    )
)

XML.write(doc)

Frankly I don't understand what's missing from the README example (https://github.com/JuliaComputing/XML.jl?tab=readme-ov-file#writing-element-nodes-with-xmlh) that makes this unclear, but I'll try to improve it.

@eoteroe In general, please make actionable requests (e.g. what part of the API is hard to understand?).

I'll close this issue as the original comment has been addressed. Open new issues for any additional problems you encounter.


I'll leave that to the folks who are paid to work on this package.

I should clarify here: Nobody is paid to work on this. I wrote XML.jl to tangentially support some paid work, but it wasn't necessary to do so. I simply wanted the project to be 100% Julia and the XML parser was the only non-Julia part. Maintaining XML.jl and the other packages I've written under the JuliaComputing org are (typically) outside of my duties at the company. It's just that me (and many others at JuliaHub) enjoy doing open source work on the side.

@joshday joshday closed this as completed May 21, 2024
@MarkNahabedian
Copy link
Author

MarkNahabedian commented May 21, 2024 via email

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