mirror of
https://github.com/Einundzwanzig-Podcast/einundzwanzig.space.git
synced 2025-12-13 16:26:50 +00:00
Refactor feed generating and file revisioning
This commit is contained in:
@@ -1,29 +1,18 @@
|
||||
const { writeFileSync } = require('fs')
|
||||
const { join, resolve } = require('path')
|
||||
const { replacements, slugify, stripHTML, participantsWithAliases, participantToId, assetUrl } = require('../helpers')
|
||||
const { replacements, slugify, stripHTML, participantsWithAliases, participantToId, assetUrl, writeJSON } = require('../helpers')
|
||||
const { masterFeedUrl, publicFeedUrl, nodeId } = require('../content/meta.json')
|
||||
const participantsRaw = require('../content/participants.json')
|
||||
const request = require('sync-request')
|
||||
const { XMLParser, XMLBuilder, XMLValidator } = require('fast-xml-parser')
|
||||
const xmlFormat = require('xml-formatter')
|
||||
const { XMLParser } = require('fast-xml-parser')
|
||||
|
||||
const debug = process.env.CI
|
||||
const dir = resolve(__dirname, '..')
|
||||
const write = (name, data) => writeFileSync(join(dir, name), data)
|
||||
const writeJSON = (name, data) =>
|
||||
write(`generated/${name}.json`, JSON.stringify(data, null, 2))
|
||||
|
||||
const participants = participantsWithAliases(participantsRaw)
|
||||
|
||||
const commonOpts = {
|
||||
const xml2jsonOpts = {
|
||||
attributeNamePrefix: '',
|
||||
attributesGroupName: '__attr',
|
||||
ignoreAttributes: false,
|
||||
cdataPropName: '__cdata'
|
||||
}
|
||||
|
||||
const xml2jsonOpts = {
|
||||
...commonOpts,
|
||||
cdataPropName: '__cdata',
|
||||
parseTagValue: true,
|
||||
parseAttributeValue: false,
|
||||
trimValues: true,
|
||||
@@ -31,15 +20,9 @@ const xml2jsonOpts = {
|
||||
arrayMode: false
|
||||
}
|
||||
|
||||
const json2xmlOpts = {
|
||||
...commonOpts,
|
||||
indentBy: ' '
|
||||
}
|
||||
|
||||
const regexBlockzeit = /Blockzeit\s(\d+(?:\.)?\d+)/
|
||||
|
||||
const parser = new XMLParser(xml2jsonOpts, true)
|
||||
const builder = new XMLBuilder(json2xmlOpts)
|
||||
|
||||
const parseEpisode = e => {
|
||||
const guid = e.guid['#text']
|
||||
@@ -223,23 +206,10 @@ const parseEpisode = e => {
|
||||
return updated
|
||||
})
|
||||
|
||||
const outputXML = builder.build(feed)
|
||||
writeJSON('feed', feed)
|
||||
writeJSON('episodes', episodes)
|
||||
console.log('Neueste Episode:', episodes[0].title, '-', episodes[0].date)
|
||||
|
||||
const validation = XMLValidator.validate(outputXML)
|
||||
if (validation) {
|
||||
write(
|
||||
'dist/feed.xml',
|
||||
xmlFormat(outputXML, {
|
||||
indentation: json2xmlOpts.indentBy,
|
||||
collapseContent: true
|
||||
})
|
||||
)
|
||||
} else {
|
||||
console.error(validation.err)
|
||||
}
|
||||
|
||||
if (_noParticipants.length) {
|
||||
console.log('Keine Teilnehmerliste')
|
||||
console.table(_noParticipants)
|
||||
|
||||
29
tasks/generate_feed.js
Normal file
29
tasks/generate_feed.js
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
const { XMLBuilder, XMLValidator } = require('fast-xml-parser')
|
||||
const xmlFormat = require('xml-formatter')
|
||||
const { write } = require('../helpers')
|
||||
const feed = require('../generated/feed.json')
|
||||
|
||||
const json2xmlOpts = {
|
||||
attributeNamePrefix: '',
|
||||
attributesGroupName: '__attr',
|
||||
ignoreAttributes: false,
|
||||
cdataPropName: '__cdata',
|
||||
indentBy: ' '
|
||||
}
|
||||
const builder = new XMLBuilder(json2xmlOpts)
|
||||
|
||||
// Load and adapt feed
|
||||
const outputXML = builder.build(feed)
|
||||
const validation = XMLValidator.validate(outputXML)
|
||||
if (validation) {
|
||||
write(
|
||||
'dist/feed.xml',
|
||||
xmlFormat(outputXML, {
|
||||
indentation: json2xmlOpts.indentBy,
|
||||
collapseContent: true
|
||||
})
|
||||
)
|
||||
} else {
|
||||
console.error(validation.err)
|
||||
}
|
||||
16
tasks/replace_revs.js
Normal file
16
tasks/replace_revs.js
Normal file
@@ -0,0 +1,16 @@
|
||||
const { resolve } = require('path')
|
||||
const { replaceInFileSync } = require('replace-in-file')
|
||||
const rev = require('../generated/rev.json')
|
||||
const options = {
|
||||
files: [resolve(__dirname, '../dist/**/*.xml'), resolve(__dirname, '../dist/**/*.html')],
|
||||
from: Object.keys(rev).map(key => new RegExp(key, 'g')),
|
||||
to: Object.values(rev)
|
||||
}
|
||||
|
||||
try {
|
||||
const results = replaceInFileSync(options)
|
||||
console.log('Replacement results:', results.filter(result => result.hasChanged).length, 'files changed')
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Replacement error occurred:', error)
|
||||
}
|
||||
Reference in New Issue
Block a user