Skip to content

Commit

Permalink
More manual updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gucci-on-fleek committed May 11, 2024
1 parent d0a78c1 commit 1f47c55
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 26 deletions.
80 changes: 61 additions & 19 deletions documentation/luatools-manual.tex
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,39 @@

\environment lwc-manual

\let\ac=\acronym
\let\q=\quotation

\setuplayout[width=43em]

\begingroup
\tt
\ctxlua{tt_font = font.current()}
\endgroup

\definecolor[transparent][t=0.0, a=1]
\definecolor[code][x=e6ebe6]

\definetextbackground[code][
location=always,
background=color,
backgroundcolor=code,
backgroundoffset=1ex,
topoffset=1ex,
leftoffset=1ex,
rightoffset=1ex,
bottomoffset=1ex,
frame=on,
framecolor=transparent,
framecolor=code,
rulethickness=1pt,
corner=round,
radius=4ex,
]

\def\startcode[#1]{%
\def\codelanguage{#1}%
\blank[halfline]%
\starttextbackground[code]%
\interlinepenalty=10000%
\vskip-1ex%
\noindent{\ssx\sc example}\par%
\grabbufferdata[code][startcode][stopcode]%
}

Expand All @@ -52,17 +59,36 @@
actions = function()
local language = tokens.getters.macro("codelanguage")
local code = buffers.raw("code")
highlight(language, code, tt_font)
highlight(language, code, tt_font, tex.leftskip.width)
context.stoptextbackground()
context.blank { "halfline" }
end
}

local function is_comment(line)
local function is_tex(line)
line = line or ""
return line:match("^%-%-%- (.*) %-%-%-$") or
line:match("^%-%-%- (.*)$") or
(line:match("^%-+$") and "")
return line:match("^%-%-= (.*) %-%-%-$") or
line:match("^%-%-= (.*)$") or
(line:match("^[-=]+$") and "")
end
local function trim_dash(str)
if str and str:match("^[%s-]*$") then
return str:gsub("-", " ")
else
return str
end
end
local function is_type(line)
local cmd, name, extra, desc = line:match(
"^%-%-%- (@%S+%s+)(%S+)(%s+%S+%s+)(.*)$"
)
if not cmd then
desc = line:match("^%-%-%-%s%s+(.*)$")
end
return cmd, trim_dash(name), trim_dash(extra), trim_dash(desc)
end
local function is_blank(line)
Expand All @@ -76,19 +102,33 @@
actions = function(filename)
local lines <const> = io.loaddata(filename):splitlines()
context.stepwise(function()
context.stepwise(function() print(xpcall(function()
local text
for index, line in ipairs(lines) do
local prev_comment = is_comment(lines[index - 1])
local this_comment = is_comment(line)
local next_comment = is_comment(lines[index + 1])
local this_cmd, this_name, this_extra, this_desc = is_type(line)
local prev_tex = is_tex(lines[index - 1])
local this_tex = is_tex(line)
local next_tex = is_tex(lines[index + 1])
local prev_blank = is_blank(lines[index - 1])
local this_blank = is_blank(line)
local next_blank = is_blank(lines[index + 1])
if this_comment then
text = (text or [[\noindent ]]) .. this_comment .. "\n"
if this_cmd then
context.par()
context.noindent()
context.verbatim.typ(this_cmd)
context.bold(context.nested.typ(this_name))
context.verbatim.typ(this_extra)
end
if this_desc then
context(this_desc .. " ")
end
if this_tex then
text = (text or [[\noindent ]]) .. this_tex .. "\n"
elseif text then
buffers.assign("content", text)
context.getbuffer { "content" }
Expand All @@ -97,17 +137,17 @@
text = nil
end
if not this_comment then
if not this_tex and not this_desc then
if not this_blank or
(this_blank and not (prev_blank or next_blank or prev_comment))
(this_blank and not (prev_blank or prev_tex))
then
context.par()
context.step()
highlight("lua", line, tt_font)
end
end
end
end)
end, debug.traceback)) end)
end
}
\stopluacode
Expand All @@ -116,7 +156,6 @@
alternative=normal,
style=\ssbfa,
after={\blank[disable, penalty:10000]},
page=yes,
continue=yes,
]
Expand All @@ -126,5 +165,8 @@
version=0.0.0, %%version
github=https://github.com/gucci-on-fleek/luatools,
]
\subject{Contents}
\placecontent[criterium=all]
\processfile{../source/luatools.lua}
\stopdocument
23 changes: 16 additions & 7 deletions documentation/syntax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,11 @@ lexer.property = {
['scintillua.lexers'] = path
}

local function make_colourize(code, font)
local function make_colourize(code, font, left)
local last = 1
local out = node.new("kern")
local inner = node.new("kern")
inner.kern = left
return function(index, style)
local colour
if colours[style] then
Expand All @@ -150,13 +151,21 @@ local function make_colourize(code, font)
for char in code:sub(last, index-1):gmatch(".") do
if char == "\n" or char == "\r" then
local n = node.hpack(inner)
local prevdepth = node.new("glue")
prevdepth.width = tex.baselineskip.width - tex.prevdepth - n.height
prevdepth.next = n
out.next = prevdepth

local interlinepenalty = node.new("penalty")
interlinepenalty.penalty = tex.interlinepenalty

local baselineskip = node.new("glue")
baselineskip.width = tex.baselineskip.width - tex.prevdepth - n.height
tex.prevdepth = n.depth

interlinepenalty.next = baselineskip
baselineskip.next = n
out.next = interlinepenalty
out = n

inner = node.new("kern")
inner.kern = left
else
local n = node.new("glyph")
n.char = string.byte(char)
Expand All @@ -172,11 +181,11 @@ local function make_colourize(code, font)
end


local function highlight(lang, code, font)
local function highlight(lang, code, font, left)
code = code:gsub("%s*$", "\n")
local lexer = lexer.load(lang)
local tokens = lexer:lex(code)
colourize, out = make_colourize(code, font)
colourize, out = make_colourize(code, font, left or 0)

colourize(1, tokens[1])
for i = 2, #tokens, 2 do
Expand Down

0 comments on commit 1f47c55

Please sign in to comment.