Skip to content

Instantly share code, notes, and snippets.

@rebolek
Last active October 3, 2018 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rebolek/622bbd21250b474221a843ad6fcdc48f to your computer and use it in GitHub Desktop.
Save rebolek/622bbd21250b474221a843ad6fcdc48f to your computer and use it in GitHub Desktop.
Red[]
tests: [
[
"Normal **bold** preserve ** this *italic* normal **bold *bold italic*** normal"
["Normal " bold "bold" " preserve ** this " italic "italic" " normal " bold "bold " bold italic "bold italic" " normal"]
]
; -- end of tests
]
output: []
state: context [
bold?: false
italic?: false
bold-symbol: ""
italic-symbol: ""
string: ""
]
emit-string: does [
unless empty? state/string [
if state/bold? [append output 'bold]
if state/italic? [append output 'italic]
append output copy state/string
]
clear state/string
]
check-ending: [
mark: to symbol :mark ; make sure bold is closed properly
]
bold-start: [
copy symbol ["**" | "__"] not space
check-ending
(
state/bold-symbol: symbol
emit-string
state/bold?: true
)
]
bold-end: [
if (state/bold?)
(symbol: state/bold-symbol)
symbol (
emit-string
state/bold?: false
)
]
italic-start: [
copy symbol [#"*" | #"_"] not [space | symbol]
check-ending
(
state/italic-symbol: symbol
emit-string
state/italic?: true
)
]
italic-end: [
if (state/italic?)
(symbol: state/italic-symbol)
symbol (
emit-string
state/italic?: false
)
]
normal: [set value skip (append state/string value)]
rule: [
some [
bold-end
| bold-start
| italic-end
| italic-start
| normal
]
(emit-string)
]
md: func [value][
clear output
parse value rule
output
]
index: 1
foreach test tests [
print [
"Test" index
either equal? md test/1 test/2 ["passed."]["failed."]
]
index: index + 1
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment