Skip to content
Snippets Groups Projects
Commit 911383d3 authored by Raven Z.'s avatar Raven Z. :cat2:
Browse files

some clean up and formatting

parent 4eef9954
No related branches found
No related tags found
No related merge requests found
/node_modules
'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)
~ 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}}
...@@ -45,7 +45,7 @@ function parseDay(dayDom) { ...@@ -45,7 +45,7 @@ function parseDay(dayDom) {
return { return {
date: date.format('YYYY-MM-DD'), date: date.format('YYYY-MM-DD'),
label: titleText, label: titleText,
today: isActive, // today: isActive,
menu: [...menues], menu: [...menues],
extras, extras,
} }
...@@ -60,6 +60,7 @@ function parseMenuEntry(row) { ...@@ -60,6 +60,7 @@ function parseMenuEntry(row) {
case 'Rind': return 'beef' case 'Rind': return 'beef'
case 'Geflügel': return 'poultry' case 'Geflügel': return 'poultry'
case 'Fisch': return 'fish' case 'Fisch': return 'fish'
case 'Lamm': return 'lamb'
case 'odd': return null case 'odd': return null
case 'even': return null case 'even': return null
case 'bg-color': return null case 'bg-color': return null
...@@ -75,6 +76,11 @@ function parseMenuEntry(row) { ...@@ -75,6 +76,11 @@ function parseMenuEntry(row) {
const description = parseDescription(descDom) const description = parseDescription(descDom)
const nutrition = parseNutrition(nutrDom) const nutrition = parseNutrition(nutrDom)
description.forEach(d => {
if (d.notes)
d.notes = d.notes.filter(n => !labels.includes(n))
})
return { return {
labels, labels,
category, category,
...@@ -112,15 +118,36 @@ function parseDescription(descDom) { ...@@ -112,15 +118,36 @@ function parseDescription(descDom) {
const parts = [] const parts = []
descDom.childNodes.forEach(node => { descDom.childNodes.forEach(node => {
if (node.nodeName !== '#text') return; 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 = { let part = {
text: node.data.replace(/^\s*(\|\s*)|\s*$/g, '') text: textParts[textParts.length - 1]
} }
let nextNode = node.nextSibling let nextNode = node.nextSibling
if (nextNode && nextNode.nodeName === 'SUP') { while (nextNode) {
part.notes = (nextNode.textContent || '?') if (nextNode.nodeName === 'SUP') {
const oldNotes = part.notes || []
const newNotes = (nextNode.textContent || '?')
.split(',') .split(',')
.map(s => s.trim()) .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) parts.push(part)
}) })
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment