From 46a3a4f528e10e5e6436ce8a40278c77a9b3420e Mon Sep 17 00:00:00 2001 From: tmont Date: Mon, 20 Jul 2020 22:31:02 -0700 Subject: [PATCH] added county page generation --- generate.js | 25 +++++++++++++++++++++++++ tmpl/county.pug | 14 ++++++++++++++ tmpl/master.pug | 9 ++++++++- tmpl/state.pug | 5 ++++- 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 tmpl/county.pug diff --git a/generate.js b/generate.js index 1f103c2..03c0954 100755 --- a/generate.js +++ b/generate.js @@ -690,9 +690,11 @@ const processRecords = async () => { const singleCountryTmpl = path.join(templatesDir, 'country.pug'); const singleStateTmpl = path.join(templatesDir, 'state.pug'); + const singleCountyTmpl = path.join(templatesDir, 'county.pug'); const countryFn = pug.compileFile(singleCountryTmpl); const stateFn = pug.compileFile(singleStateTmpl); + const countyFn = pug.compileFile(singleCountyTmpl); await Promise.all(countryArr.map(async (countryData) => { const start = Date.now(); @@ -721,6 +723,29 @@ const processRecords = async () => { await promiseMe(callback => fs.writeFile(targetFile, stateHtml, callback)); console.log(`wrote to ${targetFile} in ${Date.now() - start}ms`); })); + + for (const stateData of countryData.states) { + if (!stateData.name || !stateData.counties || !stateData.counties.length) { + continue; + } + await Promise.all(stateData.counties.map(async (countyData) => { + countyData.population = countyData.population || 0; + + const start = Date.now(); + const targetFile = path.join( + publicDir, + 'countries', + `${countryData.safeName}-${stateData.safeName}-${countyData.safeName}.html`, + ); + const countyHtml = countyFn({ + data: countyData, + $title: `${countyData.name}, ${countyData.state}, ${countyData.country}`, + lastUpdate, + }); + await promiseMe(callback => fs.writeFile(targetFile, countyHtml, callback)); + console.log(`wrote to ${targetFile} in ${Date.now() - start}ms`); + })); + } } })); diff --git a/tmpl/county.pug b/tmpl/county.pug new file mode 100644 index 0000000..5d66047 --- /dev/null +++ b/tmpl/county.pug @@ -0,0 +1,14 @@ +extends ./master.pug + +block main + h2.d-flex.justify-content-between + span= data.name + ', ' + data.state + a.align-self-center(href=("/countries/" + data.countrySafeName + "-" + data.stateSafeName + ".html") style="font-size: 50%") + | ◀ #{data.state} + + +heroChart + + mixin renderItemName(item) + = item.name + + +dataTable([ data ], 'County', 'county') diff --git a/tmpl/master.pug b/tmpl/master.pug index 54310f2..039919c 100644 --- a/tmpl/master.pug +++ b/tmpl/master.pug @@ -458,8 +458,15 @@ html const casesPerMillion = Math.round(data.casesPerMillion).toLocaleString() + '/1M'; const totalDeaths = data.total.toLocaleString(); const totalCases = data.cases.total.toLocaleString(); + let name = data.name; + if (data.county) { + name += ', ' + data.state; + } + if (data.country) { + name += ', ' + data.country; + } const heroTitle = [ - `Covid-19: ${data.name} (${population})`, + `Covid-19: ${name} (${population})`, `${totalCases} cases (${casesPerMillion})`, `${totalDeaths} deaths (${deathsPerMillion})`, ]; diff --git a/tmpl/state.pug b/tmpl/state.pug index e663cda..b9c289a 100644 --- a/tmpl/state.pug +++ b/tmpl/state.pug @@ -9,7 +9,10 @@ block main +heroChart mixin renderItemName(item) - if item.name + if item.name && item.country === 'United States' + a(href=("/countries/" + data.countrySafeName + "-" + data.safeName + "-" + item.safeName + ".html")) + = item.name + else if item.name = item.name else em.text-muted Other #{item.state}