r/dailyprogrammer • u/nint22 1 2 • Nov 20 '12
[11/20/2012] Challenge #113 [Intermediate] Text Markup
Description:
Many technologies, notably user-edited websites, take a source text with a special type of mark-up and output HTML code. As an example, Reddit uses a special formatting syntax to turn user texts into bulleted lists, web-links, quotes, etc.
Your goal is to write a function that specifically implements the Reddit markup language, and returns all results in appropriate HTML source-code. The actual HTML features you would like to implement formatting (i.e. using CSS bold vs. the old <b> tag) is left up to you, though "modern-and-correct" output is highly desired!
Reddit's markup description is defined here. You are required to implement all 9 types found on that page's "Posting" reference table.
Formal Inputs & Outputs:
Input Description:
String UserText - The source text to be parsed, which may include multiple lines of text.
Output Description:
You must print the HTML formatted output.
Sample Inputs & Outputs:
The string literal *Test* should print <b>Test</b> or <div style="font-weight:bold;">Test</div>
7
u/skeeto -9 8 Nov 20 '12
This was also #97 difficult.
4
u/eagleeye1 0 1 Nov 20 '12 edited Nov 20 '12
Python. This might work, I couldn't get the multiline code blocks to completely work. I swapped escaped blocks with 15 "|", rather than checking each of the lines in the RE for a commented block, it seems a little easier.
Output:
Too bad HTML is blocked out, or else we could see if it really works!
italic bold superscriptscript
strikethroughreddit! blah blahinline code text!blah blah * escape formatting *6
u/prophile Nov 21 '12
To be sure, this is provably not a regular language. Regexes are the wrong tool for the job.
2
u/Riddlerforce Nov 20 '12
With all the text-parsing challenges we get on a regular basis, people in here should really start learning Bison/Flex
2
u/KinkMcGink Dec 14 '12 edited Dec 14 '12
This was a lot of fun. Here's my attempt in Ruby (I'm a programming newb). The only little issue is with the line spacing for preformatted text. I couldn't get the RegEx perfect so I tired using
string#strip!instead.My output:
I'd love any feedback!
2
u/marekkpie Jan 09 '13
Lua. There are two things that are kind of cheesy. I use a global variable for the codeblock section, since Lua doesn't seem to have the concept of static variables. I also use that boolean to append the end tags of the code block if it is the last line in the text.
I don't escape any of the codes, both with backslashes and in code blocks.
I think underscore also counts as either a bold or italic marker, which I do check for.
Regardless, I found this more fun than I thought I would:
2
u/Boolean_Cat Nov 20 '12
C++
1
u/Davorak Dec 02 '12 edited Dec 02 '12
Haskell has a great library call Pandoc for text format conversiont you can read more about it at is website or read teh documentation at the hackage page.
The markdown syntax is slightly different howver then reddits. For super script you need to '' on both sides of the superscript.
"super^script^" -> super<sup>script</sup>
Similarly with subscript with '~' replacing _
"sub~script~" -> sub<sub>script</sub>
<em> is also used in place of <i> and <strong> in place of <b>.
-13
u/__circle Nov 25 '12
Cbf to do this challenge because it's so easy.