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

sile-typesetter / sile / 9304060604

30 May 2024 02:07PM UTC coverage: 74.124% (-0.6%) from 74.707%
9304060604

push

github

alerque
style: Reformat Lua with stylua

8104 of 11995 new or added lines in 184 files covered. (67.56%)

15 existing lines in 11 files now uncovered.

12444 of 16788 relevant lines covered (74.12%)

7175.1 hits per line

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

71.43
/packages/url/init.lua
1
local base = require("packages.base")
1✔
2

3
local package = pl.class(base)
1✔
4
package._name = "url"
1✔
5

6
local pdf
7

8
-- URL escape sequence, URL fragment:
9
local preferBreakBefore = "%#"
1✔
10
-- URL path elements, URL query arguments, acceptable extras:
11
local preferBreakAfter = ":/.;?&=!_-"
1✔
12
-- URL scheme:
13
local alwaysBreakAfter = ":" -- Must have only one character here!
1✔
14

15
local escapeRegExpMinimal = function (str)
16
   -- Minimalist = just what's needed for the above strings
17
   return string.gsub(str, "([%.%?%-%%])", "%%%1")
13✔
18
end
19

20
local breakPattern = "[" .. escapeRegExpMinimal(preferBreakBefore .. preferBreakAfter .. alwaysBreakAfter) .. "]"
2✔
21

22
function package:_init ()
1✔
23
   base._init(self)
1✔
24
   self:loadPackage("verbatim")
1✔
25
   self:loadPackage("inputfilter")
1✔
26
   pdf = SILE.outputter._name == "libtexpdf"
1✔
27
   if pdf then
1✔
28
      self:loadPackage("pdf")
1✔
29
   end
30
end
31

32
function package.declareSettings (_)
1✔
33
   SILE.settings:declare({
1✔
34
      parameter = "url.linebreak.primaryPenalty",
35
      type = "integer",
36
      default = 100,
37
      help = "Penalty for breaking lines in URLs at preferred breakpoints",
38
   })
39

40
   SILE.settings:declare({
1✔
41
      parameter = "url.linebreak.secondaryPenalty",
42
      type = "integer",
43
      default = 200,
44
      help = "Penalty for breaking lines in URLs at tolerable breakpoints (should be higher than url.linebreak.primaryPenalty)",
45
   })
46
end
47

48
function package:registerCommands ()
1✔
49
   self:registerCommand("href", function (options, content)
2✔
NEW
50
      if not pdf then
×
NEW
51
         if options.src then
×
NEW
52
            SILE.process(content)
×
53
         else
NEW
54
            SILE.call("url", { language = options.language }, content)
×
55
         end
NEW
56
         return -- DONE.
×
57
      end
58

UNCOV
59
      if options.src then
×
NEW
60
         SILE.call("pdf:link", {
×
61
            dest = options.src,
62
            external = true,
63
            borderwidth = options.borderwidth,
64
            borderstyle = options.borderstyle,
65
            bordercolor = options.bordercolor,
66
            borderoffset = options.borderoffset,
NEW
67
         }, content)
×
68
      else
NEW
69
         options.src = content[1]
×
NEW
70
         SILE.call("pdf:link", {
×
71
            dest = options.src,
72
            external = true,
73
            borderwidth = options.borderwidth,
74
            borderstyle = options.borderstyle,
75
            bordercolor = options.bordercolor,
76
            borderoffset = options.borderoffset,
77
         }, function (_, _)
NEW
78
            SILE.call("url", { language = options.language }, content)
×
79
         end)
80
      end
81
   end, "Inserts a PDF hyperlink.")
1✔
82

83
   local urlFilter = function (node, content, options)
84
      if type(node) == "table" then
2✔
NEW
85
         return node
×
86
      end
87
      local result = {}
2✔
88
      for token in SU.gtoke(node, breakPattern) do
24✔
89
         if token.string then
20✔
90
            result[#result + 1] = token.string
8✔
91
         else
92
            if string.find(preferBreakBefore, escapeRegExpMinimal(token.separator)) then
24✔
93
               -- Accepts breaking before, and at the extreme worst after.
NEW
94
               result[#result + 1] = self.class.packages.inputfilter:createCommand(
×
NEW
95
                  content.pos,
×
NEW
96
                  content.col,
×
NEW
97
                  content.lno,
×
98
                  "penalty",
NEW
99
                  { penalty = options.primaryPenalty }
×
100
               )
NEW
101
               result[#result + 1] = token.separator
×
NEW
102
               result[#result + 1] = self.class.packages.inputfilter:createCommand(
×
NEW
103
                  content.pos,
×
NEW
104
                  content.col,
×
NEW
105
                  content.lno,
×
106
                  "penalty",
NEW
107
                  { penalty = options.worsePenalty }
×
108
               )
109
            elseif token.separator == alwaysBreakAfter then
12✔
110
               -- Accept breaking after (only).
111
               result[#result + 1] = token.separator
2✔
112
               result[#result + 1] = self.class.packages.inputfilter:createCommand(
4✔
113
                  content.pos,
2✔
114
                  content.col,
2✔
115
                  content.lno,
2✔
116
                  "penalty",
2✔
117
                  { penalty = options.primaryPenalty }
2✔
118
               )
2✔
119
            else
120
               -- Accept breaking after, but tolerate breaking before.
121
               result[#result + 1] = self.class.packages.inputfilter:createCommand(
20✔
122
                  content.pos,
10✔
123
                  content.col,
10✔
124
                  content.lno,
10✔
125
                  "penalty",
10✔
126
                  { penalty = options.secondaryPenalty }
10✔
127
               )
10✔
128
               result[#result + 1] = token.separator
10✔
129
               result[#result + 1] = self.class.packages.inputfilter:createCommand(
20✔
130
                  content.pos,
10✔
131
                  content.col,
10✔
132
                  content.lno,
10✔
133
                  "penalty",
10✔
134
                  { penalty = options.primaryPenalty }
10✔
135
               )
10✔
136
            end
137
         end
138
      end
139
      return result
2✔
140
   end
141

142
   self:registerCommand("url", function (options, content)
2✔
143
      SILE.settings:temporarily(function ()
4✔
144
         local primaryPenalty = SILE.settings:get("url.linebreak.primaryPenalty")
2✔
145
         local secondaryPenalty = SILE.settings:get("url.linebreak.secondaryPenalty")
2✔
146
         local worsePenalty = primaryPenalty + secondaryPenalty
2✔
147

148
         if options.language then
2✔
NEW
149
            SILE.languageSupport.loadLanguage(options.language)
×
NEW
150
            if options.language == "fr" then
×
151
               -- Trick the engine by declaring a "fake"" language that doesn't apply
152
               -- the typographic rules for punctuations
NEW
153
               SILE.hyphenator.languages["_fr_noSpacingRules"] = SILE.hyphenator.languages.fr
×
154
               -- Not needed (the engine already defaults to SILE.nodeMakers.unicode if
155
               -- the language is not found):
156
               -- SILE.nodeMakers._fr_noSpacingRules = SILE.nodeMakers.unicode
NEW
157
               SILE.settings:set("document.language", "_fr_noSpacingRules")
×
158
            else
NEW
159
               SILE.settings:set("document.language", options.language)
×
160
            end
161
         else
162
            SILE.settings:set("document.language", "und")
2✔
163
         end
164

165
         local result = self.class.packages.inputfilter:transformContent(content, urlFilter, {
4✔
166
            primaryPenalty = primaryPenalty,
2✔
167
            secondaryPenalty = secondaryPenalty,
2✔
168
            worsePenalty = worsePenalty,
2✔
169
         })
170
         SILE.call("urlstyle", {}, result)
2✔
171
      end)
172
   end, "Inserts penalties in an URL so it can be broken over multiple lines at appropriate places.")
3✔
173

174
   self:registerCommand("urlstyle", function (options, content)
2✔
175
      SILE.call("code", options, content)
1✔
176
   end, "Hook that may be redefined to change the styling of URLs")
2✔
177
end
178

179
package.documentation = [[
180
\begin{document}
181
\use[module=packages.url]
182
This package enhances the typesetting of URLs in two ways.
183
First, it provides the \autodoc:command{\href[src=<url>]{<content>}} command which inserts PDF hyperlinks, \href[src=http://www.sile-typesetter.org/]{like this}.
184

185
The \autodoc:command{\href} command accepts the same \autodoc:parameter{borderwidth}, \autodoc:parameter{bordercolor}, \autodoc:parameter{borderstyle}, and \autodoc:parameter{borderoffset} styling options as the \autodoc:command[check=false]{\pdf:link} command from the \autodoc:package{pdf} package, for instance \href[src=http://www.sile-typesetter.org/, borderwidth=0.4pt, bordercolor=blue, borderstyle=underline]{like this}.
186

187
Nowadays, it is a common practice to have URLs in print articles (whether it is a good practice or not is yet \em{another} topic).
188
Therefore, the package also provides the \autodoc:command{\url} command, which will automatically insert breakpoints into unwieldy URLs like \url{https://github.com/sile-typesetter/sile-typesetter.github.io/tree/master/examples} so that they can be broken up over multiple lines.
189

190
It allows line breaks after the colon, and before or after appropriate segments of an URL (path elements, query parts, fragments, etc.).
191
By default, the \autodoc:command{\url} command ignores the current language, as one would not want hyphenation to occur in URL segments.
192
If you have no other choice, however, you can pass it a \autodoc:parameter{language} option to enforce a language to be applied.
193
Note that if French (\code{fr}) is selected, the special typographic rules applying to punctuations in this language are disabled.
194

195
To typeset a URL and also make it an active hyperlink, use the \autodoc:command{\href} command without the \autodoc:parameter{src} option,
196
but with the URL passed as argument.
197

198
The breaks are controlled by two penalty settings: \autodoc:setting{url.linebreak.primaryPenalty} for preferred breakpoints and, for less acceptable but still tolerable breakpoints, \autodoc:setting{url.linebreak.secondaryPenalty}—its value should logically be higher than the previous one.
199

200
The \autodoc:command{\urlstyle} command hook may be overridden to change the style of URLs.
201
By default, they are typeset as “code”.
202

203
\end{document}
204
]]
1✔
205

206
return package
1✔
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