diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..07e6e472cc75fafa944e2a6d4b0f101bc476c060 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/node_modules diff --git a/format.js b/format.js new file mode 100644 index 0000000000000000000000000000000000000000..ddee48d31a5caba6358b760f90434e76cb9a0d52 --- /dev/null +++ b/format.js @@ -0,0 +1,38 @@ +'use strict'; +const fs = require('fs') +const path = require('path') +const mustache = require('mustache') +const moment = require('moment') + +const dataText = fs.readFileSync(path.join(__dirname, 'menu.json'), 'utf-8') +const tplText = fs.readFileSync(path.join(__dirname, 'mail.mustache'), 'utf-8') +//console.log('tpl: ', tplText) +//console.log('data:', dataText) +const data = JSON.parse(dataText) +const dayData = data.days[moment().format('YYYY-MM-DD')] +const usedAdditives = {} +dayData.menu.forEach(m => + m.description.forEach(d => + (d.notes || []).forEach( n => usedAdditives[n] = true) + ) +) +dayData.extras.forEach(e => + e.options.forEach(o => + (o.description.notes || []).forEach( n => usedAdditives[n] = true) + ) +) + +const view = { + today: dayData, + url: data.url, + fetchedAt: data.fetchedAt, + additives: Object.keys(data.additives) + .filter(k => usedAdditives[k]) + .map(k => ({ + symbol: k, + text: data.additives[k] + })) +} + +const rendered = mustache.render(tplText, view) +console.log(rendered) diff --git a/mail.mustache b/mail.mustache new file mode 100644 index 0000000000000000000000000000000000000000..668ba2bf519f4e7b6fe1dde2953ca89f5e12041d --- /dev/null +++ b/mail.mustache @@ -0,0 +1,21 @@ + ~ Menu for {{ today.date }} ~ + +{{#today.menu}} +{{ category }} ({{ price }}) {{#labels}}[{{.}}]{{/labels}}: +{{#description}} + - {{ text }} {{#notes}}[{{.}}]{{/notes}} +{{/description}} + +{{/today.menu}} +{{#today.extras}} +{{ category }}: +{{#options}} + - {{#description}} {{text}} {{#notes}}[{{.}}]{{/notes}}{{/description}} +{{/options}} + +{{/today.extras}} + +Additives: +{{#additives}} + [{{symbol}}]: {{text}} +{{/additives}} diff --git a/main.js b/main.js index 85bd2b42d614c66c930baad5d08453a75fe60265..e70d992bf5adc59ff2c9f4344f5b64ac3377939f 100644 --- a/main.js +++ b/main.js @@ -45,7 +45,7 @@ function parseDay(dayDom) { return { date: date.format('YYYY-MM-DD'), label: titleText, - today: isActive, + // today: isActive, menu: [...menues], extras, } @@ -60,6 +60,7 @@ function parseMenuEntry(row) { case 'Rind': return 'beef' case 'Geflügel': return 'poultry' case 'Fisch': return 'fish' + case 'Lamm': return 'lamb' case 'odd': return null case 'even': return null case 'bg-color': return null @@ -75,6 +76,11 @@ function parseMenuEntry(row) { const description = parseDescription(descDom) const nutrition = parseNutrition(nutrDom) + description.forEach(d => { + if (d.notes) + d.notes = d.notes.filter(n => !labels.includes(n)) + }) + return { labels, category, @@ -112,15 +118,36 @@ function parseDescription(descDom) { const parts = [] descDom.childNodes.forEach(node => { if (node.nodeName !== '#text') return; - + if (node.textContent.trim() === '') return; + + const textRaw = node.data.replace(/^\s*(\|\s*)|\s*$/g, '') + const textParts = textRaw.split('|').map(s => s.trim()) + if (textParts.length > 1) { + textParts + .slice(0,-1) + .map(t => ({ text: t })) + .forEach(p => parts.push(p)) + } let part = { - text: node.data.replace(/^\s*(\|\s*)|\s*$/g, '') + text: textParts[textParts.length - 1] } let nextNode = node.nextSibling - if (nextNode && nextNode.nodeName === 'SUP') { - part.notes = (nextNode.textContent || '?') - .split(',') - .map(s => s.trim()) + while (nextNode) { + if (nextNode.nodeName === 'SUP') { + const oldNotes = part.notes || [] + const newNotes = (nextNode.textContent || '?') + .split(',') + .map(s => s.trim()) + part.notes = [...oldNotes, ...newNotes] + + nextNode = nextNode.nextSibling + continue + } else if (nextNode.nodeName === '#text' + && nextNode.textContent.trim() === '') { + nextNode = nextNode.nextSibling + continue + } + break } parts.push(part) })