Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[add] native cross-ref syntax #34

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/template.typ
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
body
}

#let ref-fn(name) = link(label("tidy" + name), raw(name))
#let ref-fn(name) = link(label("tidy-" + name), raw(name))

#let file-code(filename, code) = pad(x: 4%, block(
width: 100%,
Expand Down
6 changes: 3 additions & 3 deletions src/helping.typ
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
if type(entry) != array {
entry = (entry,)
}
parse-module(entry.map(x => x()).join("\n"), old-parser: old-parser)
parse-module(entry.map(x => x()).join("\n"), old-parser: old-parser, label-prefix: "help-")
}

#let search-docs(search, searching, namespace, style, old-parser: false) = {
Expand Down Expand Up @@ -55,7 +55,7 @@
module.variables = variables
return help-box({
show search: highlight.with(fill: rgb("#FF28"))
show-module(module, style: style)
show-module(module, style: style, enable-cross-references: false)
})
}

Expand Down Expand Up @@ -173,7 +173,7 @@
style: style,
enable-cross-references: false,
enable-tests: false,
show-outline: false
show-outline: false,
)
}
return result
Expand Down
4 changes: 2 additions & 2 deletions src/parse-module.typ
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
/// - `types` (optional): A list of accepted argument types.
/// - `default` (optional): Default value for this argument.
///
/// See @@show-module() for outputting the results of this function.
/// See @show-module for outputting the results of this function.
#let parse-module(

/// Content of `.typ` file to analyze for docstrings.
Expand Down Expand Up @@ -137,7 +137,7 @@
/// -> boolean
old-parser: true
) = {
if label-prefix == auto { label-prefix = name }
if label-prefix == auto { label-prefix = name + "-" }

let docs = (
name: name,
Expand Down
23 changes: 21 additions & 2 deletions src/show-module.typ
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
#import "testing.typ"


#let def-state = state("tidy-definitions", (:))


/// Show given module in the given style.
/// This displays all (documented) functions in the module.
///
/// -> content
#let show-module(

/// Module documentation information as returned by @@parse-module().
/// Module documentation information as returned by @parse-module.
/// -> dictionary
module-doc,

Expand All @@ -27,7 +29,7 @@
/// -> int
first-heading-level: 2,

/// Whether to output the name of the module at the top.
/// Whether to output the name of the module at the top.
/// -> boolean
show-module-name: true,

Expand Down Expand Up @@ -126,6 +128,23 @@

style-args.scope = eval-scope

def-state.update(x => {
x + module-doc.functions.map(x => (x.name, 1)).to-dict() + module-doc.variables.map(x => (x.name, 0)).to-dict()
})

show ref: it => {
let target = str(it.target)
if target.starts-with(label-prefix){ return it }
if not enable-cross-references {
return raw(target)
}
let defs = def-state.final()
if defs.at(target, default: none) == 1 {
target += "()"
}
(eval-scope.tidy.show-reference)(label(label-prefix + target), target)
}


// Show the docs

Expand Down
12 changes: 7 additions & 5 deletions src/styles/default.typ
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,15 @@

// Create a parameter description block, containing name, type, description and optionally the default value.
#let show-parameter-block(
name, types, content, style-args,
function-name: none, name, types, content, style-args,
show-default: false,
default: none,
) = block(
inset: 10pt, fill: rgb("ddd3"), width: 100%,
breakable: style-args.break-param-descriptions,
[
#box(heading(level: style-args.first-heading-level + 3, name))
#if function-name != none and style-args.enable-cross-references { label(function-name + "." + name) }
#h(1.2em)
#types.map(x => (style-args.style.show-type)(x, style-args: style-args)).join([ #text("or",size:.6em) ])

Expand All @@ -142,11 +143,11 @@

if style-args.colors == auto { style-args.colors = colors }

if style-args.enable-cross-references [
#heading(fn.name, level: style-args.first-heading-level + 1)
#label(style-args.label-prefix + fn.name + "()")
] else [
[
#heading(fn.name, level: style-args.first-heading-level + 1)
#if style-args.enable-cross-references {
label(style-args.label-prefix + fn.name + "()")
}
]

eval-docstring(fn.description, style-args)
Expand All @@ -168,6 +169,7 @@
style-args,
show-default: "default" in info,
default: info.at("default", default: none),
function-name: style-args.label-prefix + fn.name
)
}
v(4.8em, weak: true)
Expand Down
3 changes: 2 additions & 1 deletion src/styles/help.typ
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
let prefix = module-doc.label-prefix
let items = ()
for fn in module-doc.functions {
items.push(link(label(prefix + fn.name + "()"), fn.name + "()"))
items.push(fn.name + "()")
// items.push(link(label(prefix + fn.name + "()"), fn.name + "()"))
}
list(..items)
}
Expand Down
17 changes: 9 additions & 8 deletions src/styles/minimal.typ
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@
name, types, content, style-args,
show-default: false,
default: none,
function-name: none
) = block(
inset: 0pt, width: 100%,
breakable: style-args.break-param-descriptions,
[
#[
#set text(fill: fn-color)
#raw(name, lang: none)
#if function-name != none and style-args.enable-cross-references { label(function-name + "." + name) }
]
(#h(-.2em)
#types.map(x => (style-args.style.show-type)(x)).join([ #text("or",size:.6em) ])
Expand All @@ -91,14 +93,12 @@
) = {
set par(justify: false, hanging-indent: 1em, first-line-indent: 0em)

block(breakable: style-args.break-param-descriptions,
if style-args.enable-cross-references [
#(style-args.style.show-parameter-list)(fn, style-args)
#label(style-args.label-prefix + fn.name + "()")
] else [
#(style-args.style.show-parameter-list)(fn, style-args)
]
)
block(breakable: style-args.break-param-descriptions)[
#(style-args.style.show-parameter-list)(fn, style-args)
#if style-args.enable-cross-references {
label(style-args.label-prefix + fn.name + "()")
}
]
pad(x: 0em, eval-docstring(fn.description, style-args))

let parameter-block
Expand All @@ -115,6 +115,7 @@
style-args,
show-default: "default" in info,
default: info.at("default", default: none),
function-name: style-args.label-prefix + fn.name
)
}

Expand Down