Skip to content

Commit

Permalink
feat(scripts): added the wordcount script
Browse files Browse the repository at this point in the history
This script gets statistics for the book.
  • Loading branch information
dwmkerr committed Feb 27, 2020
1 parent 970dacb commit 1ca56cc
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions scripts/wordcount.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
#
# wordcount
#
# Given a set of markdown files, counts the words, approximates
# the pages and summarises. Outputs in CSV.
#
# Example:
# ./scripts/wordcount.sh ./website/content/docs/section*/**/_index.md > statistics.csv

# Write out the CSV column titles.
echo -e '"Path","Section","Title","Wordcount","Pagecount"'

# Go through each file we'll get statistics for.
for file in $@; do
# Get the section from the folder structure - perhaps better to put it in
# the frontmatter in the future?
section=$(basename $(dirname $(dirname "$file")))

# Rip out the title from the frontmatter.
title=$(cat $file |\
grep 'title: ' |\
sed -E 's/.*title:[[:space:]]*["]?([^"]*)["]?$/\1/')

# Get the wordcount.
wc=$(wc -w < "$file" | tr -d ' ')

# Get the pagecount.
pc=$(( (wc/500) + 1 ))

# Write the results as a line of CSV.
echo -e "\"$file\",\"$section\",\"$title\",\"$wc\",\"$pc\""

done

0 comments on commit 1ca56cc

Please sign in to comment.