• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sile-typesetter / sile / 5079084651

pending completion
5079084651

push

github

GitHub
docs(manual): Try to clarify that SILE doesn't require a custom input language (#1805)

11722 of 15754 relevant lines covered (74.41%)

6986.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/packages/chordmode/init.lua
1
local base = require("packages.base")
×
2

3
local package = pl.class(base)
×
4
package._name = "chordmode"
×
5

6
function package:_init ()
×
7
  base._init(self)
×
8
  self:loadPackage("raiselower")
×
9
  self:loadPackage("inputfilter")
×
10
end
11

12
function package.declareSettings (_)
×
13

14
  SILE.settings:declare({
×
15
    parameter = "chordmode.offset",
16
    type = "length",
17
    default = SILE.length("2ex"),
18
    help = "Vertical offset between the chord name and the text."
×
19
  })
20

21
end
22

23
function package:registerCommands ()
×
24

25
  self:registerCommand("ch", function (options, content)
×
26
    local chordBox = SILE.typesetter:makeHbox(function ()
×
27
      SILE.call("chordmode:chordfont", {}, { options.name })
×
28
    end)
29
    local origWidth = chordBox.width
×
30
    chordBox.width = SILE.length()
×
31

32
    SILE.call("raise", { height = SILE.settings:get("chordmode.offset") }, function ()
×
33
      SILE.typesetter:pushHbox(chordBox)
×
34
    end)
35

36
    local lyricBox = SILE.call("hbox", {}, content)
×
37
    if lyricBox.width < origWidth then
×
38
      lyricBox.width = origWidth + SILE.length("0.5em"):absolute()
×
39
    end
40
    local chordLineHeight = chordBox.height + SILE.settings:get("chordmode.offset"):absolute()
×
41
    if chordLineHeight > lyricBox.height then
×
42
      lyricBox.height = chordLineHeight
×
43
    end
44
  end, "Insert a chord name above the text")
×
45

46
  local function _addChords (text, content)
47
    local result = {}
×
48
    local chordName
49
    local currentText = ""
×
50
    local process
51
    local processText, processChordName, processChordText
52

53
    local function insertChord()
54
      table.insert(result, self.class.packages.inputfilter:createCommand(
×
55
      content.pos, content.col, content.lno,
×
56
      "ch", { name = chordName }, currentText
×
57
      ))
58
      chordName = nil
×
59
    end
60

61
    local function insertText()
62
      if #currentText > 0 then table.insert(result, currentText) end
×
63
      currentText = ""
×
64
    end
65

66
    local function ignore(separator)
67
      currentText = currentText .. separator
×
68
    end
69

70
    processText = {
×
71
      ["<"] = function (_)
72
        insertText()
×
73
        process = processChordName
×
74
      end
75
    }
76

77
    processChordName = {
×
78
      [">"] = function (_)
79
        chordName = currentText
×
80
        currentText = ""
×
81
        process = processChordText
×
82
      end
83
    }
84

85
    processChordText = {
×
86
      ["<"] = function (_)
87
        insertChord()
×
88
        currentText = ""
×
89
        process = processChordName
×
90
      end,
91
      ["\n"] = function (separator)
92
        insertChord()
×
93
        currentText = separator
×
94
        process = processText
×
95
      end,
96
    }
97
    process = processText
×
98

99
    for token in SU.gtoke(text, "[<\n>]") do
×
100
      if(token.string) then
×
101
        currentText = currentText .. token.string
×
102
      else
103
        (process[token.separator] or ignore)(token.separator)
×
104
      end
105
    end
106

107
    if (chordName ~= nil) then
×
108
      insertChord()
×
109
    else
110
      insertText()
×
111
    end
112
    return result
×
113
  end
114

115
  self:registerCommand("chordmode", function (_, content)
×
116
    SILE.process(self.class.packages.inputfilter:transformContent(content, _addChords))
×
117
  end, "Transform embedded chords to 'ch' commands")
×
118

119
  self:registerCommand("chordmode:chordfont", function (_, content)
×
120
    SILE.process(content)
×
121
  end, "Override this command to change chord style.")
×
122

123
end
124

125
package.documentation = [[
126
\begin{document}
127
\use[module=packages.chordmode]
128
This package provides the \autodoc:environment{chordmode} environment, which transforms lines like:
129

130
\begin{verbatim}
131
  I’ve be<G>en a wild rover for many’s a <C>year
132
\end{verbatim}
133

134
into:
135

136
\begin{autodoc:example}
137
\begin{chordmode}
138
  I’ve be<G>en a wild rover for many’s a <C>year
139
\end{chordmode}
140
\par
141
\end{autodoc:example}
142

143
The chords can be styled by redefining the \autodoc:command{\chordmode:chordfont} command, and the offset between the chord name and text adjusted with the \autodoc:setting{chordmode.offset} setting.
144
\end{document}
145
]]
×
146

147
return package
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc