def test_code_block(self): text = textwrap.dedent(""" ``` test ``` """) self.assertEqual(count_words_in_markdown(text), 1)
def test_nested_bullet_points(self): text = textwrap.dedent(""" - foo - bar - test """) self.assertEqual(count_words_in_markdown(text), 3)
def test_indented_code_block(self): text = textwrap.dedent(""" foo bar test code """) self.assertEqual(count_words_in_markdown(text), 2)
def test_enumeration(self): text = textwrap.dedent(""" 1. foo 2. bar #. smart item """) self.assertEqual(count_words_in_markdown(text), 4)
def test_custom_header_tags(self): text = textwrap.dedent(""" ## header1 {#header1} foo bar ## header2 {#header2} """) self.assertEqual(count_words_in_markdown(text), 4)
def test_inline(self): text = textwrap.dedent(""" **bold text** *italicized text* `test` ~~test~~ """) self.assertEqual(count_words_in_markdown(text), 6)
def test_html_tags(self): text = textwrap.dedent(""" test <br> <span>test</span> test """) self.assertEqual(count_words_in_markdown(text), 3)
def test_footnote(self): text = textwrap.dedent(""" MWC is great [1]. [1] source footnote [1](do count this one please) Followup text """) self.assertEqual(count_words_in_markdown(text), 10)
def test_image(self): text = textwrap.dedent(""" test ![test](images1) ![blah](images2) test """) self.assertEqual(count_words_in_markdown(text), 2)
def test_comments(self): text = textwrap.dedent(""" <!-- Test --> <!-- > Test --> <!-- Test --> Test """) self.assertEqual(count_words_in_markdown(text), 1)
def test_headings(self): text = textwrap.dedent(""" # H1 ## H2 ### H3 H1 ----- H1 ===== ### My Great Heading {#custom-id} """) self.assertEqual(count_words_in_markdown(text), 8)
def _calculate_reading_time(self, content): """Calculate time taken to read content.""" reading_speed = self.settings.get("READING_SPEED", DEFAULT_READING_SPEED) wordcount = count_words_in_markdown(content) time_unit = "minutes" try: reading_time = math.ceil(float(wordcount) / float(reading_speed)) if reading_time == 1: time_unit = "minute" reading_time = "{} {}".format(str(reading_time), time_unit) except ValueError as words_per_minute_nan: raise ValueError("READING_SPEED setting must be a number." ) from words_per_minute_nan return reading_time
def main(): if sys.version_info < (3, ): print( 'Python 3 is required. You are using Python 2. You should probably run this script as follows:' ) print('python3 mwc.py') sys.exit(1) if len(sys.argv) < 2: print('Provide the Markdown file to parse as first argument') sys.exit(1) if not os.path.isfile(sys.argv[1]): print('The file at the given location could not be opened') sys.exit(1) with open(sys.argv[1], 'r', encoding='utf8') as f: print(count_words_in_markdown(f.read()))
def test_simple_text(self): text = textwrap.dedent(""" test a b c """) self.assertEqual(count_words_in_markdown(text), 4)
def test_quote(self): text = textwrap.dedent(""" > blockquote """) self.assertEqual(count_words_in_markdown(text), 1)
def test_link(self): text = textwrap.dedent(""" Some [linked text](https://google.com/). """) self.assertEqual(count_words_in_markdown(text), 3)