Skip to content

Commit

Permalink
Merge pull request #8 from behringer24/feat/extend_parser
Browse files Browse the repository at this point in the history
Extended parser features
  • Loading branch information
behringer24 authored Oct 24, 2024
2 parents c21bdfb + 9a12a95 commit ba70e84
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
4 changes: 2 additions & 2 deletions example/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Testing a List:
-Item 6
// Comment

# This is a first $"chapter"$ // Kommentar 4
$"Lorem Ipsum"$ is $'simply dummy'$ text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
# This is a first %"chapter"% // Kommentar 4
%"Lorem Ipsum"% is %'simply dummy'% text of the printing and typesetting industry... Lorem Ipsum has been the industry's standard dummy text --- ever since the 1500s, when an unknown printer took a galley of type -- and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
___

## subheading
Expand Down
28 changes: 26 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func parseLine(book *epub.EPub, line string, baseDir string, insideBlock bool) s
// define regular expressions do detect commands
chapterRegex := regexp.MustCompile(`^\s*(#)\s*([^#]+)$`)
headlinesRegex := regexp.MustCompile(`^\s*(#{2,6})\s*([^#]+)$`)
dividerRegex := regexp.MustCompile(`^\s*([\*,\-]\s*)+$`)
dividerRegex := regexp.MustCompile(`^\s*([\*]\s*)+$`)
pagebreakRegex := regexp.MustCompile(`^\s*(_\s*)+$`)
metaRegex := regexp.MustCompile(`\$\[(title|author|series|set|entry|uuid|language|quotes)\]\(([^\)]+)\)`)
coverRegex := regexp.MustCompile(`\!\[cover\]\(([^ \)]+)\s*(\"([^\"]*)\")?\)`)
Expand All @@ -103,6 +103,9 @@ func parseLine(book *epub.EPub, line string, baseDir string, insideBlock bool) s
italicRegex := regexp.MustCompile(`\*([^\*]+)\*`)
commentRegex := regexp.MustCompile(`//.*$`)
ulListRegex := regexp.MustCompile(`^\s*-\s*(.*)$`)
longDashRegex := regexp.MustCompile(`\s+(---)\s+`)
midDashRegex := regexp.MustCompile(`\s+(--)\s+`)
threeDotsRegex := regexp.MustCompile(`(\.\.\.)`)

if inUlList && !ulListRegex.MatchString(line) && !insideBlock {
// End unordered List if open and no new list element
Expand Down Expand Up @@ -257,7 +260,7 @@ func parseLine(book *epub.EPub, line string, baseDir string, insideBlock bool) s
inUlList = true
}

log.Printf("Add LI Element %s", matches[1])
log.Print("Add LI Element")
newline = newline + " <li>" + parseLine(book, matches[1], baseDir, true) + "</li>\n"

return newline
Expand All @@ -283,6 +286,27 @@ func parseLine(book *epub.EPub, line string, baseDir string, insideBlock bool) s
return ""
})

return parseLine(book, line, baseDir, insideBlock)
} else if longDashRegex.MatchString(line) {
// Replace --- with long dash
line = longDashRegex.ReplaceAllStringFunc(line, func(match string) string {
return "&nbsp;&mdash;&nbsp;"
})

return parseLine(book, line, baseDir, insideBlock)
} else if midDashRegex.MatchString(line) {
// Replace -- with medium dash
line = midDashRegex.ReplaceAllStringFunc(line, func(match string) string {
return "&nbsp;&ndash;&nbsp;"
})

return parseLine(book, line, baseDir, insideBlock)
} else if threeDotsRegex.MatchString(line) {
// Replace ... with typographic sign
line = threeDotsRegex.ReplaceAllStringFunc(line, func(match string) string {
return "&hellip;"
})

return parseLine(book, line, baseDir, insideBlock)
} else if !insideBlock {
// Normal line just add if not empty
Expand Down
12 changes: 10 additions & 2 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@ p.firstparagraph {
}
p {
text-indent: 1em;
margin-block-start: 0.2em;
margin-block-end: 0.2em;
}
p, li {
margin-top: 0.3em;
margin-bottom: 0.3em;
}
hr {
border: 1px solid #444;
height: 2px;
width: 25%;
margin: 3em auto 3em auto;
}
`

Expand Down

0 comments on commit ba70e84

Please sign in to comment.