Skip to content

Count words in a markdown document

License

Notifications You must be signed in to change notification settings

Arcesilas/md-word-count

Repository files navigation

GitHub license PHP from Packagist Packagist Version Scrutinizer build Scrutinizer coverage Scrutinizer code quality

MdWordCount

This package is a port in PHP of Markdown Word Count for Python3. Regular expressions comme from there (except for images) and tests too.

This package helps you count words in a markdown document. It excludes punctuation, footnotes, and special Markdown or HTML tag syntax.

You may use it, for example, to get an estimation of the reading time of an article written in markdown.

The package provides a class with static methods, function helpers and an executable file.

Installation

Install with Composer:

composer require arcesilas/md-word-count

Usage

Pass a string:

use Arcesilas\MdWordCount;

$count = MdWordCount::fromString($markdown);

Pass a file path:

use Arcesilas\MdWordCount;

$count = MdWordCount::fromFile($file);

if (false === $count) {
    // File not found or directory
}

Use the helpers:

$count = md_word_count($markdown);
$count = md_file_word_count($file);

if (false === $count) {
    // File not found or directory
}

Use the executable:

bin/mdwc path/to/document.md

The executable echoes the word count from given file and exits with code 0.

If the file does not exists, it exits with code 1.

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

Run:

$ composer test
$ composer check-style

Contributing

See CONTRIBUTING

TODO

  • Improve the executable with features:
    • accept arguments (see man wc for examples)
    • read files from list (or from stdin)
    • read content from stdin

Without using symfony/console for such a small command.

About

Count words in a markdown document

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages