Refactors to support key transposition.
This commit is contained in:
@@ -3,6 +3,12 @@ import zero_functional
|
||||
|
||||
import ./ast
|
||||
|
||||
type
|
||||
FormatContext = ref object
|
||||
chart: ChordChart
|
||||
currentKey: ChordChartPitch
|
||||
sourceKey: ChordChartPitch
|
||||
|
||||
const DEFAULT_STYLESHEET* = """
|
||||
<style>
|
||||
* {
|
||||
@@ -51,22 +57,28 @@ h3 {
|
||||
</style>
|
||||
"""
|
||||
|
||||
proc toHtml(node: ChordChartNode, indent: string): string =
|
||||
proc transpose(ctx: FormatContext, chord: ChordChartChord): string =
|
||||
let distance = ctx.currentKey - ctx.sourceKey
|
||||
if distance != 0: return $(chord.rootPitch + distance) & chord.flavor
|
||||
else: return chord.original
|
||||
|
||||
proc toHtml(ctx: var FormatContext, node: ChordChartNode, indent: string): string =
|
||||
result = ""
|
||||
case node.kind
|
||||
of ccnkSection:
|
||||
result &= indent & "<section>\p" &
|
||||
indent & " " & "<h3>" & node.sectionName & "</h3>\p"
|
||||
|
||||
result &= join(
|
||||
node.sectionContents --> map(it.toHtml(indent & " ")),
|
||||
"\p")
|
||||
var contents = newSeq[string]()
|
||||
for contentNode in node.sectionContents:
|
||||
contents.add(ctx.toHtml(contentNode, indent & " "))
|
||||
result &= contents.join("\p")
|
||||
|
||||
result &= indent & "</section>"
|
||||
|
||||
of ccnkLine:
|
||||
result &= indent & "<div class=line>\p"
|
||||
result &= join(node.line --> map(it.toHtml(indent & " ")), "")
|
||||
for linePart in node.line: result &= ctx.toHtml(linePart, indent & " ")
|
||||
result &= indent & "</div>"
|
||||
|
||||
of ccnkWord:
|
||||
@@ -75,8 +87,8 @@ proc toHtml(node: ChordChartNode, indent: string): string =
|
||||
if node.spaceAfter: result&=" space-after"
|
||||
result &= "'>"
|
||||
|
||||
if node.chord.len > 0:
|
||||
result &= "<span class=chord>" & node.chord & "</span>"
|
||||
if node.chord.original.len > 0:
|
||||
result &= "<span class=chord>" & ctx.transpose(node.chord) & "</span>"
|
||||
|
||||
result &= "<span class=lyric>"
|
||||
if node.word.len > 0: result &= node.word
|
||||
@@ -90,14 +102,24 @@ proc toHtml(node: ChordChartNode, indent: string): string =
|
||||
|
||||
of ccnkColBreak: discard #FIXME
|
||||
of ccnkPageBreak: discard #FIXME
|
||||
of ccnkTransposeKey: discard #FIXME
|
||||
of ccnkRedefineKey: discard #FIXME
|
||||
|
||||
of ccnkTransposeKey:
|
||||
ctx.currentKey = ctx.currentKey + node.transposeSteps
|
||||
|
||||
of ccnkRedefineKey:
|
||||
ctx.sourceKey = ctx.currentKey + node.transposeSteps
|
||||
ctx.currentKey = ctx.sourceKey
|
||||
|
||||
proc toHtml*(
|
||||
cc: ChordChart,
|
||||
stylesheets = @[DEFAULT_STYLESHEET],
|
||||
scripts: seq[string] = @[]): string =
|
||||
|
||||
var ctx = FormatContext(
|
||||
chart: cc,
|
||||
currentKey: cc.metadata.key,
|
||||
sourceKey: cc.metadata.key)
|
||||
|
||||
result = """<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
@@ -124,6 +146,6 @@ proc toHtml*(
|
||||
if cc.metadata.contains("bpm"): result &= " " & cc.metadata["bpm"] & "bpm"
|
||||
result &= "</h2>\p"
|
||||
|
||||
result &= join(cc.nodes --> map(it.toHtml(indent & " ")), "\p")
|
||||
result &= join(cc.nodes --> map(ctx.toHtml(it, indent & " ")), "\p")
|
||||
|
||||
result &= " </body>\p</html>"
|
||||
|
||||
Reference in New Issue
Block a user