Skip to content

Commit

Permalink
Switch to new imprint service
Browse files Browse the repository at this point in the history
  • Loading branch information
simar0at committed Jun 19, 2024
1 parent 828b3ba commit b81d206
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 59 deletions.
2 changes: 1 addition & 1 deletion build.fetch_imprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const imprintVue = './src/imprint/Imprint.vue'
module.exports = (api, options) => {
api.registerCommand('build:fetchImprint', async (args) => {
try {
var snippet = await getImprintHTML(16630, "py-10"),
var snippet = await getImprintHTML(18319, "py-10"),
htmlTemplate = await (await fs.readFile(imprintHTML)).toString('UTF-8'),
html = htmlTemplate.replace(/\{\{\s*imprint\s*\}\}/g, snippet),
vueTemplate = await (await fs.readFile(imprintVue)).toString('UTF-8'),
Expand Down
60 changes: 2 additions & 58 deletions src/functions/fetchImprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const sax = require('sax')

const localFunctions = {
getImprintHTML: async function(id, addClasses) {
const data = await httpGet(`https://shared.acdh.oeaw.ac.at/acdh-common-assets/api/imprint.php?serviceID=${id}`, (rawData, onlyAttr, val, onlyTopTag) => {processResult(rawData, onlyAttr, val, onlyTopTag, addClasses)})
const data = await httpGet(`https://imprint.acdh.oeaw.ac.at/${id}/?fromat=html`, (rawData, onlyAttr, val, onlyTopTag) => {processResult(rawData, onlyAttr, val, onlyTopTag, addClasses)})
return data
}
}
Expand Down Expand Up @@ -37,7 +37,7 @@ return new Promise ((resolve, reject) => {
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
try {
resolve(processResult(rawData, 'lang', 'en', 'div'));
resolve(rawData);
} catch (e) {
console.error(e.message);
reject(e)
Expand All @@ -50,60 +50,4 @@ return new Promise ((resolve, reject) => {
})
}

function processResult(rawData, onlyAttr, val, onlyTopTag, addClasses) {
const parser = sax.parser(false, {lowercase: true})
let res = ''
let tagname = []
let ignore = false
let isSelfClosing = false
parser.onerror = function (e) {
throw new Error(e)
};
parser.ontext = function (t) {
if (ignore) {return}
res += t
};
parser.onopentag = function (node) {
isSelfClosing = node.isSelfClosing
ignore = (node.attributes[onlyAttr] && node.attributes[onlyAttr] !== val) ||
(tagname.length === 1 && node.name !== onlyTopTag) ? `${node.name}_${tagname.length}` : ignore
tagname.push(node.name)
if (ignore || node.name === '____') {return}
if (isSelfClosing) {
res += `<${node.name}${serializeAttr(node.attributes)}/>`
} else if (tagname.length === 1) {
if (node.attributes.class) {
res += `<${node.name}${serializeAttr(node.attributes, addClasses)}>`
} else {
res += `<${node.name}${serializeAttr(node.attributes)} class="${addClasses}">`
}
} else {
res += `<${node.name}${serializeAttr(node.attributes)}>`
}
};
parser.onclosetag = function () {
const nodeName = tagname.pop(),
endIgnore = ignore || isSelfClosing
ignore = ignore === `${nodeName}_${tagname.length}` ? false : ignore
isSelfClosing = false
if (endIgnore || nodeName === '____') {return}
res += `</${nodeName}>`
};

parser.write(`<____>${rawData}</____>`).close()
return res
}

function serializeAttr(attributes, addClasses) {
let res = ''
for (let attr of Object.keys(attributes)) {
if (attr == 'class' && addClasses) {
res += ` ${attr}="${attributes[attr]} ${addClasses}"`
} else {
res += ` ${attr}="${attributes[attr]}"`
}
}
return res
}

module.exports = localFunctions.getImprintHTML

0 comments on commit b81d206

Please sign in to comment.