HomeForumSourceResearchGuide
Sign in to contribute to source. how it works
Component parsing.MarkdownToHTML by barry
expand copy to clipboardexpand
component provides MarkdownToHTML requires data.IntUtil iu, data.StringBuilder, data.query.Search search, data.StringUtil stringUtil, parsing.Tokeniser {
	
	void styleCode(char src[], char lang[], MDCodeStyle styles[], StringBuilder bld)
		{
		//if lang exists in styles, use it, otherwise use styles[0] (if any)
		MDCodeStyle style = null
		if (((style = styles.findFirst(MDCodeStyle.[languageName], new MDCodeStyle(lang))) != null) || styles.arrayLength > 0)
			{
			if (style == null) style = styles[0]
			
			Tokeniser tokeniser = new Tokeniser(style.syntaxTokens)
			
			if (style.lineComment != null)
				tokeniser.setLineComment(style.lineComment)
			
			if (style.blockComment.arrayLength == 2)
				tokeniser.setBlockComment(style.blockComment[0].string, style.blockComment[1].string)
			
			//use parsing.Tokeniser to help...
			ParseToken tokens[] = tokeniser.tokenise(src).tokens
			
			int offset = 0
			for (int i = 0; i < tokens.arrayLength; i++)
				{
				bld.add(stringUtil.subString(src, offset, (tokens[i].sourceStart - offset)))
				
				if (tokens[i].type == ParseToken.TYPE_PARTICLE)
					{
					//possible keyword
					bool found = false
					for (int j = 0; j < style.keywords.arrayLength; j ++)
						{
						if (style.keywords[j].keywords.findFirst(String.[string], new String(tokens[i].content)) != null)
							{
							bld.add("$(tokens[i].content)")
							found = true
							break
							}
						}
					
					if (!found) bld.add(tokens[i].content)
					}
					else if (tokens[i].type == ParseToken.TYPE_LINE_COMMENT)
					{
					bld.add("//$(tokens[i].content)")
					}
					else if (tokens[i].type == ParseToken.TYPE_BLOCK_COMMENT)
					{
					bld.add("/*$(tokens[i].content)*/")
					}
					else if (tokens[i].type == ParseToken.TYPE_LITERAL_STRING)
					{
					bld.add("\"$(tokens[i].content)\"")
					}
					else
					{
					bld.add(tokens[i].content)
					}
				
				offset = tokens[i].sourceStart + tokens[i].sourceLength
				}
			}
			else
			{
			bld.add(src)
			}
		}
	
	int docToHTML(MDElement doc, MDCodeStyle codeStyling[], StringBuilder bld, int anchorCount)
		{
		int aci = 0

		if (doc.type == MDElement.PARAGRAPH)
			{
			bld.add("

") } else if (doc.type == MDElement.PLAIN) { if (doc.extType & MDElement.BOLD == MDElement.BOLD) { bld.add("") } if (doc.extType & MDElement.ITALIC == MDElement.ITALIC) { bld.add("") } if (doc.extType & MDElement.CODE == MDElement.CODE) { bld.add("") } bld.add(doc.content) if (doc.extType & MDElement.CODE == MDElement.CODE) { bld.add("") } if (doc.extType & MDElement.ITALIC == MDElement.ITALIC) { bld.add("") } if (doc.extType & MDElement.BOLD == MDElement.BOLD) { bld.add("") } } else if (doc.type == MDElement.CODE_BLOCK) { bld.add("

")
			styleCode(doc.content, doc.extInfo, codeStyling, bld)
			bld.add("
") } else if (doc.type == MDElement.HEADING) { bld.add("") bld.add(doc.content) bld.add("") anchorCount ++ aci ++ } else if (doc.type == MDElement.LIST_UNORDERED) { bld.add("") } else if (doc.type == MDElement.LIST_ORDERED) { bld.add("") } else if (doc.type == MDElement.LIST_ITEM) { bld.add("") } else if (doc.type == MDElement.LINK) { bld.add("") } else if (doc.type == MDElement.QUOTE_BLOCK) { bld.add("") } return aci } int makeTOC(MDElement doc, StringBuilder bld, int anchorCount) { int aci = 0 if (doc.type == MDElement.HEADING) { bld.add("

$(doc.content)

") anchorCount ++ aci ++ } for (int i = 0; i < doc.children.arrayLength; i++) { anchorCount += makeTOC(doc.children[i], bld, anchorCount) } return aci } void makeTOCHTML(MDElement doc, StringBuilder sbld, char tocStyling[]) { StringBuilder tocBuild = new StringBuilder() makeTOC(doc, tocBuild, 0) char toc[] = tocBuild.get() if (toc.arrayLength > 0) { sbld.add("
") sbld.add(toc) sbld.add("
") } } char[] MarkdownToHTML:process(MDElement doc, opt char mainDivClass[], MDCodeStyle codeStyling[], bool generateTOC, bool tocFirst, char tocDivClass[]) { StringBuilder sbld = new StringBuilder() if (generateTOC && tocFirst) { makeTOCHTML(doc, sbld, tocDivClass) } if (mainDivClass != null) sbld.add("
") docToHTML(doc, codeStyling, sbld, 0) if (mainDivClass != null) sbld.add("
") if (generateTOC && !tocFirst) { makeTOCHTML(doc, sbld, tocDivClass) } return sbld.get() } }
Revision history
To propose a new revision to this entity, use dana source put -uc your/new/version.dn -n parsing.MarkdownToHTML -m "reason for update" -u yourUsername
Version 1 (this version) by barry
Notes for this version: Standard Library Initialisation