diff options
Diffstat (limited to 'generator/atomgen.js')
-rw-r--r-- | generator/atomgen.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/generator/atomgen.js b/generator/atomgen.js new file mode 100644 index 0000000..ede9771 --- /dev/null +++ b/generator/atomgen.js @@ -0,0 +1,46 @@ +//Copyright Chris Xiong 2024 +//License: Expat (MIT) +module.exports = { + gen_atom:function(list, config) { _gen_atom(list, config); } +}; +const jsdom=require('jsdom'); +const path=require('path'); +const fs=require('fs'); +const htmlescape=require('./util').htmlescape; + +function _gen_atom(list, config) +{ + const sorted_list = list.toSorted((a, b) => (new Date(b.date)).getTime() - (new Date(a.date)).getTime()); + let xmlbuf = `<?xml version="1.0" encoding="utf-8"?>`; + xmlbuf += `<feed xmlns="http://www.w3.org/2005/Atom">`; + xmlbuf += `<generator uri="https://cgit.chrisoft.org/sbs.git" version="${config.version_string}">SSBS</generator>`; + xmlbuf += `<updated>${new Date().toISOString()}</updated>`; + xmlbuf += `<link href="${config.atom_url}" rel="self" type="application/atom+xml" title="Atom"/>`; + xmlbuf += `<link href="${config.published_url}" rel="alternate" type="text/html"/>`; + xmlbuf += `<id>${config.atom_url}</id>`; + xmlbuf += `<title>${config.atom_title}</title>`; + xmlbuf += `<subtitle>${config.atom_subtitle}</subtitle>`; + xmlbuf += `<icon>${config.atom_icon}</icon>`; + xmlbuf += `<logo>${config.atom_icon}</logo>`; + for (let i = 0; i < sorted_list.length && i < config.atom_feed_nposts; ++i) + { + const p = sorted_list[i]; + const postpath = path.join(path.join(config.dest_dir,'post'), `${p.file}.html`); + const contfull = fs.readFileSync(postpath, 'utf8'); + const doc = new jsdom.JSDOM(contfull).window.document; + const content = doc.querySelector('article').innerHTML; + const footnotes = doc.getElementById('notediv').outerHTML; + const link = `https://chrisoft.org/blog/post/${p.file}.html`; + xmlbuf += `<entry>`; + xmlbuf += `<title>${htmlescape(p.title)}</title>`; + xmlbuf += `<link href="${link}" title="${htmlescape(p.title)}"/>`; + xmlbuf += `<published>${new Date(p.date).toISOString()}</published>`; + xmlbuf += `<updated>${new Date(p.mdate).toISOString()}</updated>`; + xmlbuf += `<id>${link}</id>`; + xmlbuf += `<author><name>${config.atom_author}</name></author>` + xmlbuf += `<content type="html" xml:base="${link}"><![CDATA[${content}<br><hr>${footnotes}]]></content>`; + xmlbuf += `</entry>`; + } + xmlbuf += `</feed>`; + fs.writeFileSync(path.join(config.dest_dir, "atom.xml"), xmlbuf ,'utf8'); +} |