///|
/// Serialise this text construct into an already-open parent element.
fn @xml.Writer::write_text_construct(
self : @xml.Writer,
name : String,
text : Text,
) -> Unit {
match text.typ {
TextType::Text => {
self.open(name)
self.text(text.value)
self.close(name)
}
TextType::Html => {
self.open_with(name, attrs=[("type", "html")])
self.text(text.value)
self.close(name)
}
TextType::Xhtml => {
self.open_with(name, attrs=[("type", "xhtml")])
self.raw("")
self.raw(text.value)
self.raw("
")
self.close(name)
}
}
}
///|
/// Serialise entry content into an already-open parent element.
fn @xml.Writer::write_content(self : @xml.Writer, content : Content) -> Unit {
match content {
Content::Text(value) => {
self.open("content")
self.text(value)
self.close("content")
}
Content::Html(value) => {
self.open_with("content", attrs=[("type", "html")])
self.text(value)
self.close("content")
}
Content::Xhtml(value) => {
self.open_with("content", attrs=[("type", "xhtml")])
self.raw("")
self.raw(value)
self.raw("
")
self.close("content")
}
Content::Src(src~, media_type~) => {
let attrs : Array[(String, String)] = [("src", src)]
match media_type {
Some(ty) => attrs.push(("type", ty))
None => ()
}
self.empty("content", attrs~)
}
}
}
///|
fn @xml.Writer::write_person(
self : @xml.Writer,
name : String,
person : Person,
) -> Unit {
self.open(name)
self.open("name")
self.text(person.name)
self.close("name")
if person.email is Some(email) {
self.open("email")
self.text(email)
self.close("email")
}
if person.uri is Some(uri) {
self.open("uri")
self.text(uri)
self.close("uri")
}
self.close(name)
}
///|
fn @xml.Writer::write_link(self : @xml.Writer, link : Link) -> Unit {
let attrs : Array[(String, String)] = []
if link.rel is Some(rel) {
attrs.push(("rel", rel))
}
if link.media_type is Some(media_type) {
attrs.push(("type", media_type))
}
if link.hreflang is Some(hreflang) {
attrs.push(("hreflang", hreflang))
}
if link.title is Some(title) {
attrs.push(("title", title))
}
if link.length is Some(length) {
attrs.push(("length", length))
}
attrs.push(("href", link.href))
self.empty("link", attrs~)
}
///|
fn @xml.Writer::write_category(self : @xml.Writer, category : Category) -> Unit {
let attrs : Array[(String, String)] = [("term", category.term)]
if category.scheme is Some(scheme) {
attrs.push(("scheme", scheme))
}
if category.label is Some(label) {
attrs.push(("label", label))
}
self.empty("category", attrs~)
}
///|
fn @xml.Writer::write_entry(self : @xml.Writer, entry : Entry) -> Unit {
self.open("entry")
self.write_text_element("id", entry.id)
self.write_text_construct("title", entry.title)
self.write_text_element("updated", entry.updated)
for author in entry.authors {
self.write_person("author", author)
}
for category in entry.categories {
self.write_category(category)
}
for contributor in entry.contributors {
self.write_person("contributor", contributor)
}
for link in entry.links {
self.write_link(link)
}
if entry.published is Some(published) {
self.write_text_element("published", published)
}
if entry.rights is Some(rights) {
self.write_text_construct("rights", rights)
}
if entry.source is Some(source) {
self.open("source")
if source.id is Some(id) {
self.write_text_element("id", id)
}
if source.title is Some(title) {
self.write_text_construct("title", title)
}
if source.updated is Some(updated) {
self.write_text_element("updated", updated)
}
self.close("source")
}
if entry.summary is Some(summary) {
self.write_text_construct("summary", summary)
}
if entry.content is Some(content) {
self.write_content(content)
}
self.close("entry")
}
///|
fn @xml.Writer::write_text_element(
self : @xml.Writer,
name : String,
content : String,
) -> Unit {
self.open(name)
self.text(content)
self.close(name)
}
///|
///|
/// Serialise the feed to an XML string.
pub fn Feed::to_xml(self : Feed) -> String {
feed_to_xml(self)
}
///|
impl Show for Feed with fn output(self, logger) -> Unit {
logger.write_string(feed_to_xml(self))
}
///|
fn feed_to_xml(feed : Feed) -> String {
let w = @xml.Writer::new()
let self = feed
w.write_decl()
w.open_with("feed", attrs=[("xmlns", ATOM_NAMESPACE)])
w.write_text_element("id", self.id)
w.write_text_construct("title", self.title)
w.write_text_element("updated", self.updated)
if self.icon is Some(icon) {
w.write_text_element("icon", icon)
}
for link in self.links {
w.write_link(link)
}
if self.logo is Some(logo) {
w.write_text_element("logo", logo)
}
for category in self.categories {
w.write_category(category)
}
if self.generator is Some(generator) {
let attrs : Array[(String, String)] = []
if generator.uri is Some(uri) {
attrs.push(("uri", uri))
}
if generator.version is Some(version) {
attrs.push(("version", version))
}
if attrs.length() > 0 {
w.open_with("generator", attrs~)
} else {
w.open("generator")
}
if generator.value is Some(value) {
w.text(value)
}
w.close("generator")
}
if self.subtitle is Some(subtitle) {
w.write_text_construct("subtitle", subtitle)
}
if self.rights is Some(rights) {
w.write_text_construct("rights", rights)
}
for author in self.authors {
w.write_person("author", author)
}
for contributor in self.contributors {
w.write_person("contributor", contributor)
}
for entry in self.entries {
w.write_entry(entry)
}
w.close("feed")
w.to_string()
}