From 1ec31426dfb0f2d5796e48a2e1b43a4b26d15045 Mon Sep 17 00:00:00 2001 From: Artem Tanyhin Date: Thu, 29 Sep 2022 00:06:39 -0400 Subject: [PATCH] basic implementation of inline code - fixes #11 --- test/Markdown File.md | 4 ++++ utils/utils.go | 41 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/test/Markdown File.md b/test/Markdown File.md index 09b5075..25b5208 100644 --- a/test/Markdown File.md +++ b/test/Markdown File.md @@ -2,6 +2,10 @@ This is the title of the file This is the first paragraph + +This is a paragraph with `inline code`, isn't it `cool`??`` + + # Heading 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc sit amet tincidunt nulla. Maecenas nec felis vel nulla faucibus ornare. Integer vitae ipsum fringilla, dapibus enim ac, imperdiet felis. Fusce placerat tortor vitae tortor laoreet vehicula. Donec dictum sit amet quam ut faucibus. Mauris vel tellus tristique, congue sem ut, condimentum erat. Aliquam erat volutpat. Interdum et malesuada fames ac ante ipsum primis in faucibus. Mauris volutpat erat lacus, egestas viverra enim mattis sed. diff --git a/utils/utils.go b/utils/utils.go index ec160bb..cf11508 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -250,6 +250,10 @@ func GenerateHTML(input string, output string, name string) { markdownValid := false if filepath.Ext(input) == ".md" { + // Turn inline Markdown features to HTML + text = GenerateInlineMarkdownHtml(text) + // If prefix is a heading or similar markdown feature + // that overwrites regular paragraph, change it if prefix, validPrefix := CheckMarkdownPrefix(text); validPrefix { // We don't want to put headers inside

tags if paragraphOpen { @@ -261,12 +265,11 @@ func GenerateHTML(input string, output string, name string) { // This is set to true so for next non markdown text a new

is written first paragraphDelimiterFound = true } - _, werr = writer.WriteString(GenerateMarkdownHtml(prefix, text)) + _, werr = writer.WriteString(GeneratePrefixMarkdownHtml(prefix, text)) if werr != nil { log.Fatal("Error writing to new file!") - } else { - markdownValid = true - } + } + markdownValid = true } } @@ -323,7 +326,35 @@ func CheckMarkdownPrefix(text string) (string, bool) { return "", false } -func GenerateMarkdownHtml(prefix string, text string) string { +func GenerateInlineMarkdownHtml(text string) string { + // Idea of set + stack is good until we meet bold text... + // Bold text consists of 2 characters instead of 1, so this will need to be reworked + acceptedDelimiters := map[rune]string{ + '`': "%s", + } + var delimiterStack []rune + + for _, character := range text { + // If the character is in the set + if _, found := acceptedDelimiters[character]; found { + // Opening delimiter - add to stack + if len(delimiterStack) == 0 || delimiterStack[len(delimiterStack)-1] != character { + delimiterStack = append(delimiterStack, character) + } else { + // Closing delimiter - remove from stack and append to HTML + delimiterStack = delimiterStack[:len(delimiterStack)-1] + if character == '`' { + text = strings.Replace(text, string(character), "", 1) + text = strings.Replace(text, string(character), "", 1) + } + } + } + } + + return text +} + +func GeneratePrefixMarkdownHtml(prefix string, text string) string { prefixesHtmlFormatStrings := map[string]string{ "# ": "

%s

", "## ": "

%s

",