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

sile-typesetter / sile / 14684598788

26 Apr 2025 07:55PM UTC coverage: 29.023% (-36.3%) from 65.328%
14684598788

push

github

alerque
test(fonts): Update test to work on new ICU instead of old

5840 of 20122 relevant lines covered (29.02%)

379.34 hits per line

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

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

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

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

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

18
local breakPattern = "[" .. escapeRegExpMinimal(preferBreakBefore .. preferBreakAfter .. alwaysBreakAfter) .. "]"
×
19

20
function package:_init ()
×
21
   base._init(self)
×
22
   self:loadPackage("inputfilter")
×
23
   self:loadPackage("pdf")
×
24
end
25

26
function package:declareSettings ()
×
27
   SILE.settings:declare({
×
28
      parameter = "url.linebreak.primaryPenalty",
29
      type = "integer",
30
      default = 100,
31
      help = "Penalty for breaking lines in URLs at preferred breakpoints",
32
   })
33
   SILE.settings:declare({
×
34
      parameter = "url.linebreak.secondaryPenalty",
35
      type = "integer",
36
      default = 200,
37
      help = "Penalty for breaking lines in URLs at tolerable breakpoints (should be higher than url.linebreak.primaryPenalty)",
38
   })
39
end
40

41
function package:registerCommands ()
×
42
   self:registerCommand("href", function (options, content)
×
43
      if options.src then
×
44
         SILE.call("pdf:link", {
×
45
            dest = options.src,
46
            external = true,
47
            borderwidth = options.borderwidth,
48
            borderstyle = options.borderstyle,
49
            bordercolor = options.bordercolor,
50
            borderoffset = options.borderoffset,
51
         }, content)
×
52
      else
53
         options.src = content[1]
×
54
         SILE.call("pdf:link", {
×
55
            dest = options.src,
56
            external = true,
57
            borderwidth = options.borderwidth,
58
            borderstyle = options.borderstyle,
59
            bordercolor = options.bordercolor,
60
            borderoffset = options.borderoffset,
61
         }, function (_, _)
62
            SILE.call("url", { language = options.language }, content)
×
63
         end)
64
      end
65
   end, "Inserts a PDF hyperlink.")
×
66

67
   local urlFilter = function (node, content, options)
68
      if type(node) == "table" then
×
69
         return node
×
70
      end
71
      local result = {}
×
72
      for token in SU.gtoke(node, breakPattern) do
×
73
         if token.string then
×
74
            result[#result + 1] = token.string
×
75
         else
76
            if string.find(preferBreakBefore, escapeRegExpMinimal(token.separator)) then
×
77
               -- Accepts breaking before, and at the extreme worst after.
78
               result[#result + 1] = self.class.packages.inputfilter:createCommand(
×
79
                  content.pos,
×
80
                  content.col,
×
81
                  content.lno,
×
82
                  "penalty",
83
                  { penalty = options.primaryPenalty }
×
84
               )
85
               result[#result + 1] = token.separator
×
86
               result[#result + 1] = self.class.packages.inputfilter:createCommand(
×
87
                  content.pos,
×
88
                  content.col,
×
89
                  content.lno,
×
90
                  "penalty",
91
                  { penalty = options.worsePenalty }
×
92
               )
93
            elseif token.separator == alwaysBreakAfter then
×
94
               -- Accept breaking after (only).
95
               result[#result + 1] = token.separator
×
96
               result[#result + 1] = self.class.packages.inputfilter:createCommand(
×
97
                  content.pos,
×
98
                  content.col,
×
99
                  content.lno,
×
100
                  "penalty",
101
                  { penalty = options.primaryPenalty }
×
102
               )
103
            else
104
               -- Accept breaking after, but tolerate breaking before.
105
               result[#result + 1] = self.class.packages.inputfilter:createCommand(
×
106
                  content.pos,
×
107
                  content.col,
×
108
                  content.lno,
×
109
                  "penalty",
110
                  { penalty = options.secondaryPenalty }
×
111
               )
112
               result[#result + 1] = token.separator
×
113
               result[#result + 1] = self.class.packages.inputfilter:createCommand(
×
114
                  content.pos,
×
115
                  content.col,
×
116
                  content.lno,
×
117
                  "penalty",
118
                  { penalty = options.primaryPenalty }
×
119
               )
120
            end
121
         end
122
      end
123
      return result
×
124
   end
125

126
   self:registerCommand("url", function (options, content)
×
127
      SILE.settings:temporarily(function ()
×
128
         local primaryPenalty = SILE.settings:get("url.linebreak.primaryPenalty")
×
129
         local secondaryPenalty = SILE.settings:get("url.linebreak.secondaryPenalty")
×
130
         local worsePenalty = primaryPenalty + secondaryPenalty
×
131

132
         if options.language then
×
133
            SILE.languageSupport.loadLanguage(options.language)
×
134
            if options.language == "fr" then
×
135
               -- Trick the engine by declaring a "fake"" language that doesn't apply
136
               -- the typographic rules for punctuations
137
               SILE.hyphenator.languages["_fr_noSpacingRules"] = SILE.hyphenator.languages.fr
×
138
               -- Not needed (the engine already defaults to SILE.nodeMakers.unicode if
139
               -- the language is not found):
140
               -- SILE.nodeMakers._fr_noSpacingRules = SILE.nodeMakers.unicode
141
               SILE.settings:set("document.language", "_fr_noSpacingRules")
×
142
            else
143
               SILE.settings:set("document.language", options.language)
×
144
            end
145
         else
146
            SILE.settings:set("document.language", "und")
×
147
         end
148

149
         local result = self.class.packages.inputfilter:transformContent(content, urlFilter, {
×
150
            primaryPenalty = primaryPenalty,
151
            secondaryPenalty = secondaryPenalty,
152
            worsePenalty = worsePenalty,
153
         })
154
         SILE.call("urlstyle", {}, result)
×
155
      end)
156
   end, "Inserts penalties in an URL so it can be broken over multiple lines at appropriate places.")
×
157

158
   self:registerCommand("urlstyle", function (options, content)
×
159
      SILE.call("code", options, content)
×
160
   end, "Hook that may be redefined to change the styling of URLs")
×
161
end
162

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

169
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}.
170

171
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).
172
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.
173

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

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

182
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.
183

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

187
\end{document}
188
]]
×
189

190
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