/**
* Your implmentation of the Markdown exercise
*/
component {
/**
* @returns
*/
function parse(required string markdown) {
var lines = listToArray( arguments.markdown, chr( 10 ) );
var output = [];
var isInList = false;
for (var line in lines) {
var matches = reMatchNoCase( "^[##]+(.*)", line);
if ( matches.len() ) {
var hLevel = reMatchNoCase( "[##]+", matches[1])[1];
line = "" & trim( replace( matches[1], hLevel, "" ) ) & "";
}
matches = reMatchNoCase( "\*(.*)", line);
if ( matches.len() ) {
if ( !isInList ) {
isInList = true;
var isBold = false;
var isItalic = false;
var matches2 = reFindNoCase( "(.*)__(.*)__(.*)", matches[1], 0, true );
if ( matches2.len[1] ) {
var match1 = mid(line, matches2.pos[2], matches2.len[2]);
var match2 = mid(line, matches2.pos[3], matches2.len[3]);
var match3 = mid(line, matches2.pos[4], matches2.len[4]);
matches[1] = match1 & "" & match2 & "" & match3;
isBold = true;
}
var matches3 = reFindNoCase( "(.*)_(.*)_(.*)", matches[1], 0, true );
if ( matches3.len[1] ) {
var match1 = mid(line, matches3.pos[2], matches3.len[2]);
var match2 = mid(line, matches3.pos[3], matches3.len[3]);
var match3 = mid(line, matches3.pos[4], matches3.len[4]);
matches[1] = match1 & "" & match2 & "" & match3;
isItalic = true;
}
if ( isItalic || isBold ) {
line = "
- " & trim( replace( matches[1], "*", "", "all" ) ) & "
";
}
else {
line = "- " & trim( replace( matches[1], "*", "", "all" ) ) & "
";
}
}
else {
var isBold = false;
var isItalic = false;
var matches2 = reFindNoCase( "(.*)__(.*)__(.*)", matches[1], 0, true );
if ( matches2.len[1] ) {
var match1 = mid(line, matches2.pos[2], matches2.len[2]);
var match2 = mid(line, matches2.pos[3], matches2.len[3]);
var match3 = mid(line, matches2.pos[4], matches2.len[4]);
matches[1] = match1 & "" & match2 & "" & match3;
isBold = true;
}
var matches3 = reFindNoCase( "(.*)_(.*)_(.*)", matches[1], 0, true );
if ( matches3.len[1] ) {
var match1 = mid(line, matches3.pos[2], matches3.len[2]);
var match2 = mid(line, matches3.pos[3], matches3.len[3]);
var match3 = mid(line, matches3.pos[4], matches3.len[4]);
matches[1] = match1 & "" & match2 & "" & match3;
isItalic = true;
}
if ( isItalic || isBold ) {
line = "- " & trim( replace( matches[1], "*", "", "all" ) ) & "
";
}
else {
line = "- " & trim( replace( matches[1], "*", "", "all" ) ) & "
";
}
}
}
else {
if ( isInList ) {
line = "
" & line;
isInList = false;
}
}
if ( !reMatchNoCase( "" & match2 & "" & match3;
}
var matches = reFindNoCase( "(.*)_(.*)_(.*)", line, 0, true );
if ( matches.len[1] ) {
var match1 = mid(line, matches.pos[2], matches.len[2]);
var match2 = mid(line, matches.pos[3], matches.len[3]);
var match3 = mid(line, matches.pos[4], matches.len[4]);
line = match1 & "" & match2 & "" & match3;
}
output.append( line );
}
var html = arrayToList( output, "" );
if ( isInList ) {
html &= "
";
}
return html;
}
}