chore: cleanup word count code
This commit is contained in:
parent
8f806d5cb9
commit
45bc0558e8
2 changed files with 37 additions and 24 deletions
|
@ -1,7 +1,6 @@
|
|||
package article;
|
||||
|
||||
import js.html.Text;
|
||||
import js.html.Element;
|
||||
import js.html.Node;
|
||||
import js.Browser.document;
|
||||
import js.Browser.navigator;
|
||||
|
@ -17,19 +16,18 @@ function main() {
|
|||
final wordCount = WordCount.inElement(content);
|
||||
final wpm = 200;
|
||||
|
||||
topright.appendChild(new InfoRowElement("Words", new HighlightedInfoElement(Std.string(wordCount))));
|
||||
|
||||
var codeblockCount = 0;
|
||||
var child = content.firstChild;
|
||||
while (child != null) {
|
||||
if (child.nodeType == Node.ELEMENT_NODE && cast(child, Element).classList.contains("codeblock"))
|
||||
codeblockCount++;
|
||||
|
||||
child = child.nextSibling;
|
||||
var wordCountEls:Array<Node> = [];
|
||||
wordCountEls.push(new HighlightedInfoElement(Std.string(wordCount.words)));
|
||||
if (wordCount.codeblocks > 0) {
|
||||
wordCountEls.push(new Text(" + "));
|
||||
wordCountEls.push(new HighlightedInfoElement(Std.string(wordCount.codeblocks)));
|
||||
wordCountEls.push(new Text(" codeblocks"));
|
||||
}
|
||||
|
||||
topright.appendChild(new InfoRowElement("Words", ...wordCountEls));
|
||||
|
||||
var readTimeEls:Array<Node> = [];
|
||||
final mins = wordCount / wpm;
|
||||
final mins = wordCount.words / wpm;
|
||||
final hours = Std.int(mins / 60);
|
||||
|
||||
if (hours > 0) {
|
||||
|
@ -41,12 +39,6 @@ function main() {
|
|||
readTimeEls.push(new Text(" @ "));
|
||||
readTimeEls.push(new HighlightedInfoElement('${wpm}wpm'));
|
||||
|
||||
if (codeblockCount > 0) {
|
||||
readTimeEls.push(new Text(" + "));
|
||||
readTimeEls.push(new HighlightedInfoElement(Std.string(codeblockCount)));
|
||||
readTimeEls.push(new Text(" codeblocks"));
|
||||
}
|
||||
|
||||
topright.appendChild(new InfoRowElement("Time to Read", ...readTimeEls));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,29 +1,50 @@
|
|||
package article;
|
||||
|
||||
import js.html.Element;
|
||||
import js.html.CharacterData;
|
||||
import js.html.Node;
|
||||
|
||||
using Lambda;
|
||||
using StringTools;
|
||||
|
||||
// TODO: return both word and codeblock count
|
||||
function inElement(el:Node):Int {
|
||||
var words = 0;
|
||||
@:forward()
|
||||
abstract Counts({
|
||||
words:Int,
|
||||
codeblocks:Int,
|
||||
}) {
|
||||
public function new() {
|
||||
this = {
|
||||
words: 0,
|
||||
codeblocks: 0,
|
||||
};
|
||||
}
|
||||
|
||||
@:op(A += B)
|
||||
public function addset(other:Counts):Void {
|
||||
this.words += other.words;
|
||||
this.codeblocks += other.codeblocks;
|
||||
}
|
||||
}
|
||||
|
||||
function inElement(el:Node):Counts {
|
||||
var counts = new Counts();
|
||||
var child = el.firstChild;
|
||||
while (child != null) {
|
||||
switch (child.nodeType) {
|
||||
case Node.TEXT_NODE:
|
||||
words += inString(cast(child, CharacterData).data);
|
||||
counts.words += inString(cast(child, CharacterData).data);
|
||||
|
||||
case Node.ELEMENT_NODE:
|
||||
if (!["pre", "code"].contains(child.nodeName.toLowerCase()))
|
||||
words += inElement(child);
|
||||
if (cast(child, Element).classList.contains("codeblock"))
|
||||
counts.codeblocks += 1;
|
||||
else if (!["pre", "code"].contains(child.nodeName.toLowerCase()))
|
||||
counts += inElement(child);
|
||||
}
|
||||
|
||||
child = child.nextSibling;
|
||||
}
|
||||
|
||||
return words;
|
||||
return counts;
|
||||
}
|
||||
|
||||
function inString(s:String):Int {
|
||||
|
|
Loading…
Reference in a new issue