From 6cc040fd0c733c6bc6e9608b7858ae723671d527 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Fri, 5 Oct 2018 16:08:35 +0100 Subject: [PATCH] Add word counter --- _conf_wc.yml | 1 + cat.html | 14 ++++++++++++++ utils/_count_words.js | 17 +++++++++++++++++ utils/count_words.sh | 4 ++++ 4 files changed, 36 insertions(+) create mode 100644 _conf_wc.yml create mode 100644 cat.html create mode 100755 utils/_count_words.js create mode 100755 utils/count_words.sh diff --git a/_conf_wc.yml b/_conf_wc.yml new file mode 100644 index 0000000..f4c2492 --- /dev/null +++ b/_conf_wc.yml @@ -0,0 +1 @@ +calc_word_count: true diff --git a/cat.html b/cat.html new file mode 100644 index 0000000..5b24d4b --- /dev/null +++ b/cat.html @@ -0,0 +1,14 @@ +--- +layout: none +--- + +{% if site.calc_word_count %} + {% assign chapters = site.en | sort: "idx" %} + {% for chapter in chapters %} + {% unless chapter.homepage %} + {{ chapter.content }} + {% endunless %} + {% endfor %} +{% else %} + Cat disabled. +{% endif %} diff --git a/utils/_count_words.js b/utils/_count_words.js new file mode 100755 index 0000000..81f2cc1 --- /dev/null +++ b/utils/_count_words.js @@ -0,0 +1,17 @@ +#!/usr/bin/node + +var wordCount = require('html-word-count'); +var fs = require("fs"); +var jsdom = require("jsdom"); + +var text = fs.readFileSync("_site/cat.html"); + +const { JSDOM } = jsdom; +const { window } = new JSDOM(text); +const { document } = (window).window; +global.document = document; +var $ = require('jquery')(window); + +$("pre").remove(); + +console.log(wordCount(document.documentElement.innerHTML)); // 2 diff --git a/utils/count_words.sh b/utils/count_words.sh new file mode 100755 index 0000000..15e80e0 --- /dev/null +++ b/utils/count_words.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +jekyll build --config _config.yml,_conf_wc.yml +./utils/_count_words.js