-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add XML.build and XML::Writer #3806
Conversation
call SetQuoteChar, char.ord | ||
end | ||
|
||
# TODO: mark as private |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With #3747 merged and part of 0.20.3
, cannot this macro be marked as private already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last thing I knew, the binaries that travis uses for linux weren't up to date. I'll try to change this and see if it's already fixed :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It failed... jhass used to maintain these, I guess we should take that over
e40f382
to
a6d9323
Compare
Pedantic: I'm not sure about the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noted some typos. Code looks good, and feature is 👍
end | ||
|
||
# Emits the start attribute of an attribute with namespace info. | ||
def start_attribute(prefix : String?, name : String, namesapce_uri : String?) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: namespace_uri
end_element | ||
end | ||
|
||
# Emits the start attribute of an attribute. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "start attribute of an attribute"? Did you mean the "start of an attribute"? Same below.
end | ||
|
||
# Emits the start of the document, invokes the block, | ||
# and the emits the end of the document. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"then emits"
@@ -0,0 +1,312 @@ | |||
module XML | |||
# Returns the resulting of writing XML to the yielded `XML::Writer`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"returns the resulting String"?
end | ||
|
||
# Emits the start of a comment, invokes the block | ||
# and the emits the end of the comment. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"then emits"
end | ||
|
||
# Emits the start of a CDATA section, invokes the block | ||
# and then emits the enf of the CDATA section. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"emits the end" or maybe "closes the CDATA section"? Same for other occurrences.
|
||
# Emits text content. | ||
# | ||
# Text content can happen inside of an element, attribute value, cdata, dtd, etc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe have backtics and hash to link to mentioned method?
a6d9323
to
8c96b62
Compare
8c96b62
to
6bc552d
Compare
Shouldn't the class |
@splattael we could make that extensive to @asterite, since this was already merged, think will accept a PR with that rename? 😄 |
Sounds good. Also YAML. I wasn't sure because it doesn't build xml nodes, it writes an xml string to an IO, but I don't think we'll have a separate builder for something else, so having consistent names is good. |
@luislavena Don't worry, I already started this refactor. I decided to do it myself because there's no |
Right now it's impossible to generate correct XML in an easy way. This PR fixes that. It wraps a libxml2 writer. Writing to the writer directly writes to the underlying IO.
Example:
Output:
As you can see, there's no "magic" in the writer. You have to call
element
to emit elements. You also haveattribute
,text
and other methods to emit these individually.I thought about either adding a separate
XML::Builder
class that usesmethod_missing
and is similar to Nokogiri::XML::Builder, or either adding that functionality toXML::Writer
, but I'm not sure about that. For example in Nokogiri you have to usetype_
orobject_id_
(with an ending underscore) if you need these names so they don't conflict with existing methods. Also you basically have to learn a new mini-language for emitting stuff, and it gets a bit more complex when namespaces and attributes are involved. So I prefer to keep things simple and straight-forward here (though a builder usingmethod_missing
is definitely possible (in master)).