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

sile-typesetter / sile / 9304049654

30 May 2024 02:12PM UTC coverage: 60.021% (-14.7%) from 74.707%
9304049654

push

github

web-flow
Merge 1a26b4f22 into a1fd105f8

6743 of 12900 new or added lines in 186 files covered. (52.27%)

347 existing lines in 49 files now uncovered.

10311 of 17179 relevant lines covered (60.02%)

3307.34 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 ()
×
NEW
7
   base._init(self)
×
NEW
8
   self:loadPackage("raiselower")
×
NEW
9
   self:loadPackage("inputfilter")
×
10
end
11

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

21
function package:registerCommands ()
×
NEW
22
   self:registerCommand("ch", function (options, content)
×
NEW
23
      local chordBox = SILE.typesetter:makeHbox(function ()
×
NEW
24
         SILE.call("chordmode:chordfont", {}, { options.name })
×
25
      end)
NEW
26
      local origWidth = chordBox.width
×
NEW
27
      chordBox.width = SILE.types.length()
×
28

NEW
29
      SILE.call("raise", { height = SILE.settings:get("chordmode.offset") }, function ()
×
NEW
30
         SILE.typesetter:pushHbox(chordBox)
×
31
      end)
32

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

43
   local function _addChords (text, content)
NEW
44
      local result = {}
×
45
      local chordName
NEW
46
      local currentText = ""
×
47
      local process
48
      local processText, processChordName, processChordText
49

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

65
      local function insertText ()
NEW
66
         if #currentText > 0 then
×
NEW
67
            table.insert(result, currentText)
×
68
         end
NEW
69
         currentText = ""
×
70
      end
71

72
      local function ignore (separator)
NEW
73
         currentText = currentText .. separator
×
74
      end
75

NEW
76
      processText = {
×
77
         ["<"] = function (_)
NEW
78
            insertText()
×
NEW
79
            process = processChordName
×
80
         end,
81
      }
82

NEW
83
      processChordName = {
×
84
         [">"] = function (_)
NEW
85
            chordName = currentText
×
NEW
86
            currentText = ""
×
NEW
87
            process = processChordText
×
88
         end,
89
      }
90

NEW
91
      processChordText = {
×
92
         ["<"] = function (_)
NEW
93
            insertChord()
×
NEW
94
            currentText = ""
×
NEW
95
            process = processChordName
×
96
         end,
97
         ["\n"] = function (separator)
NEW
98
            insertChord()
×
NEW
99
            currentText = separator
×
NEW
100
            process = processText
×
101
         end,
102
      }
NEW
103
      process = processText
×
104

NEW
105
      for token in SU.gtoke(text, "[<\n>]") do
×
NEW
106
         if token.string then
×
NEW
107
            currentText = currentText .. token.string
×
108
         else
NEW
109
            (process[token.separator] or ignore)(token.separator)
×
110
         end
111
      end
112

NEW
113
      if chordName ~= nil then
×
NEW
114
         insertChord()
×
115
      else
NEW
116
         insertText()
×
117
      end
NEW
118
      return result
×
119
   end
120

NEW
121
   self:registerCommand("chordmode", function (_, content)
×
NEW
122
      SILE.process(self.class.packages.inputfilter:transformContent(content, _addChords))
×
NEW
123
   end, "Transform embedded chords to 'ch' commands")
×
124

NEW
125
   self:registerCommand("chordmode:chordfont", function (_, content)
×
NEW
126
      SILE.process(content)
×
NEW
127
   end, "Override this command to change chord style.")
×
128
end
129

130
package.documentation = [[
131
\begin{document}
132
\use[module=packages.chordmode]
133
This package provides the \autodoc:environment{chordmode} environment, which transforms lines like:
134

135
\begin{verbatim}
136
  I’ve be<G>en a wild rover for many’s a <C>year
137
\end{verbatim}
138

139
into:
140

141
\begin{autodoc:example}
142
\begin{chordmode}
143
  I’ve be<G>en a wild rover for many’s a <C>year
144
\end{chordmode}
145
\par
146
\end{autodoc:example}
147

148
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.
149
\end{document}
150
]]
×
151

152
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