Skip to content

Commit

Permalink
export-defs: rename command and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jo3-l committed Dec 26, 2024
1 parent ce13081 commit b10fbed
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 29 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ members = [
"crates/yag-template-envdefs",
"crates/yag-template-lsp",
"crates/yag-template-syntax",
"def2json-bin",
"exportdefs-bin",
]
resolver = "2"
4 changes: 2 additions & 2 deletions def2json-bin/Cargo.toml → exportdefs-bin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "def2json-bin"
name = "exprtdefs-bin"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "def2json"
name = "exportdefs"
path = "src/main.rs"
doc = false
test = false
Expand Down
36 changes: 36 additions & 0 deletions exportdefs-bin/mk_yagdata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import json
from pathlib import Path

MAX_BATCH_LEN = 20000 - 500


def exportbatch(batch):
v = json.dumps(dict(batch))
quoted = json.dumps(v)
return "{{ $data := " + quoted + " }}"


data = json.load(open("out.json"))
entries = list(data.items())
batch_start = 0

batches = []
while batch_start < len(entries):
lo, hi = batch_start + 1, len(entries)
while lo < hi:
mid = (lo + hi + 1) // 2
export = exportbatch(entries[batch_start:mid])
if len(export) <= MAX_BATCH_LEN:
lo = mid
else:
hi = mid - 1

batch_end = lo
batches.append(exportbatch(entries[batch_start:batch_end]))

batch_start = batch_end

Path("yagdata").mkdir(exist_ok=True)
for n, out in enumerate(batches):
with open(f"yagdata/{n}.txt", "w") as f:
f.write(out)
31 changes: 17 additions & 14 deletions def2json-bin/src/discordmd.rs → exportdefs-bin/src/discordmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,28 @@ fn unwrap_markdown(s: &str) -> String {
.map(|line| if line.is_empty() { line } else { &line[common_indent..] });

let mut output = String::with_capacity(s.len());
let out_verbatim = |output: &mut String, s: &str| {
output.push('\n');
output.push_str(s);
};
let out_with_sep = |output: &mut String, sep: char, s: &str| {
if output.bytes().next_back().is_some_and(|c| c != b'\n') {
output.push(sep);
}
output.push_str(s);
};

let mut in_codeblock = false;
for line in dedented_lines {
let mut output_verbatim = |s: &str| {
output.push('\n');
output.push_str(s);
};

if heuristic::has_codefence(line) {
in_codeblock = !in_codeblock;
output_verbatim(line);
} else if in_codeblock || line.is_empty() || heuristic::is_list_item(line) {
output_verbatim(line);
out_with_sep(&mut output, '\n', line);
} else if in_codeblock {
out_verbatim(&mut output, line);
} else if line.is_empty() || heuristic::is_list_item(line) {
out_with_sep(&mut output, '\n', line);
} else {
match output.bytes().next_back() {
Some(b'\n') => output.push('\n'),
Some(_) => output.push(' '),
None => {}
}
output.push_str(line);
out_with_sep(&mut output, ' ', line);
}
}
output
Expand Down
33 changes: 28 additions & 5 deletions def2json-bin/src/main.rs → exportdefs-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct Cli {
files: Vec<PathBuf>,

/// Whether to pretty-print the output.
#[clap(short, long, default_value = "true")]
#[clap(short, long, default_value = "false")]
pretty: bool,
}

Expand All @@ -39,7 +39,7 @@ fn main() -> anyhow::Result<()> {
.collect::<anyhow::Result<Vec<_>>>()?;

let defs = yag_template_envdefs::parse(&sources).map_err(|err| anyhow!("failed to parse definitions: {err}"))?;
let processed_defs: HashMap<String, ProcessedFunc> =
let processed_defs: HashMap<String, ExportedFunc> =
defs.funcs.into_iter().map(|(name, func)| (name, func.into())).collect();

let serialized = if args.pretty {
Expand All @@ -54,18 +54,41 @@ fn main() -> anyhow::Result<()> {
}

#[derive(Debug, Serialize)]
struct ProcessedFunc {
struct ExportedFunc {
pub name: String,
pub signature: String,
pub doc: String,
}

impl From<yag_template_envdefs::Func> for ProcessedFunc {
impl From<yag_template_envdefs::Func> for ExportedFunc {
fn from(f: yag_template_envdefs::Func) -> Self {
Self {
name: f.name.clone(),
signature: f.signature(),
signature: doc_style_signature(&f),
doc: discordmd::render(&f.doc),
}
}
}

fn doc_style_signature(f: &yag_template_envdefs::Func) -> String {
let mut buf = String::new();
buf.push_str("{{ ");
buf.push_str(&f.name);
for param in &f.params {
buf.push(' ');
buf.push_str(&doc_style_param(param));
}
buf.push_str(" }}");
buf
}

fn doc_style_param(f: &yag_template_envdefs::Param) -> String {
let name = &f.name;
if f.is_optional {
format!("[{name}]")
} else if f.is_variadic {
format!("{name}...")
} else {
name.clone()
}
}

0 comments on commit b10fbed

Please sign in to comment.