Preamble
In Stack Exchange markdown, we use ** to bold out text. For example, this markdown:
The **quick brown fox jumps over the lazy** dog.
Renders as:
The quick brown fox jumps over the lazy dog.
Of course, we use ** to close boldness as well. So less of the answer will be bold. For example:
The **quick** brown fox jumps over the **lazy** dog.
Renders as:
The quick brown fox jumps over the lazy dog.
However, if the boldness is not closed, it renders as not bold:
The **quick brown fox jumps over the lazy dog.
Renders as:
The **quick brown fox jumps over the lazy dog.
If the text has a single backslash \
, the boldness does not take effect, either:
The \**quick brown fox jumps over the lazy dog.**
Renders as:
The *quick brown fox jumps over the lazy dog.*
Trailing white space results in unbolded text (note, the white space after brown is a single tab):
The** quick** brown fox jumps over the lazy dog.**
Renders as:
The** quick** brown fox jumps over the lazy dog.**
We can also use __ for bold, too, but note that only one can be active at a time. Here's a more complicated example:
The __quick**__ brown **fox__ jumps** over__ the__ lazy **dog.
Renders as:
The quick** brown fox__ jumps over__ the__ lazy **dog.
The question:
Write a program or function that, given ASCII text either as an argument or on STDIN, where the only special characters are **
, __
, \
(for escaping) and trailing whitespace, determines how many bold characters there are. This value should be printed to STDOUT or returned from your function. You do not need to support very long strings; String length is guaranteed to be no more than \$30{,}000\$, which is the limit for a Stack Exchange post.
Fine print:
- Can I throw an exception / other error for one case, and return normally for the other?
- No. It must be either a clear, unambiguous, non errored return value for both cases. STDERR output will be ignored.
- Are spaces in between words considered bold?
- Yes.
**quick brown**
has 11 bold characters in it.
- Yes.
- Should the
\
in\**
, if bold, be counted?- No. It renders as **, so if it should be bolded it would only be 2 characters.
- Be completely clear: what do you mean by how many characters?
- Total characters that would render bold. This means that
**
is not rendered if it transforms text, but it is rendered if it does not. - Note that it's possible to make
**
be bolded in several ways, e.g.**\****
-> **. - Don't consider the possibility that some text could be converted to italics. The only markdown rule to consider is * = bold.**
- Total characters that would render bold. This means that
- On Stack Exchange, HTML Bold works too. i.e. <b></b>
- Yes, I am aware. Don't consider this case, this is normal text.
- What about HTML entites? e.g.
<
-><
- These also should be considered as normal text, there is no HTML entity conversion.
- I've thought of an example you didn't cover above!
- The rules function exactly as if the text were posted on Stack Exchange, in an answer (not a comment), except that code blocks are not considered special characters. Both the four space type and the backtick type. If you are not sure about how text should be rendered, just throw it into an answer box somewhere as a test, those are the rules you should follow.
Examples:
Input:
The **quick brown fox jumps over the lazy** dog.
Output:
35
Input:
The **quick brown fox jumps over the lazy dog.
Output:
0
Input:
The __quick**__ brown **fox__ jumps** over__ the__ lazy **dog.
Output:
18
Input:
The __quick\____ brown fox **jumps over\** the** lazy \**dog.
Output:
23
Input:
The****quick brown fox****jumps over **the****lazy** dog.
Output:
11
Standard Loopholes are banned.
18
correct for the third test case? \$\endgroup\$**fox__ jumps**
terminates that particular bolding. \$\endgroup\$\**
or\__
are three-character escape sequences, but in StackExchange there are only two-character escape sequences\*
or\_
. So\***a**
produces an asterisk followed by a bolda
. There is also another escape,\\
. Should we handle that one? \$\endgroup\$