Skip to content
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
33 changes: 27 additions & 6 deletions Cargo.lock

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

13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@ comline-core = { git = "https://github.com/ComlineProject/core", rev = "da1157c9
# `comline-codegen`'s types, so the rev below must be byte-identical to the
# one *they* pin - Cargo only unifies a git dependency across identical rev
# strings, and two copies of `GenRequest` are two incompatible types.
comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "4a42cc1c14ace568f3589643696e4526a6364b0f" }
comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "c634f01441dbd28a4607fe3b49ff2b54a0d81395" }
# The official Markdown and HTML documentation renderers (`comline doc`),
# linked in like the official generators. Same generation rev as
# `comline-codegen`.
comline-doc-markdown = { git = "https://github.com/ComlineProject/generation", rev = "4a42cc1c14ace568f3589643696e4526a6364b0f" }
comline-doc-html = { git = "https://github.com/ComlineProject/generation", rev = "4a42cc1c14ace568f3589643696e4526a6364b0f" }
comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "bcebb787d35c3f35912e79e5c68b78b3b6f1cafd", optional = true }
comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "8ed8880ac5a5605fecce97e53818256ece7d153f", optional = true }
comline-doc-markdown = { git = "https://github.com/ComlineProject/generation", rev = "c634f01441dbd28a4607fe3b49ff2b54a0d81395" }
comline-doc-html = { git = "https://github.com/ComlineProject/generation", rev = "c634f01441dbd28a4607fe3b49ff2b54a0d81395" }
comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "4bbae4cb9473c6f50b35fa120d6627ed19b8c36a", optional = true }
comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "f779044be6ae79be44237c0a9fbade5feaed1cec", optional = true }
# `comline conformance`: the conformance runner (corpus loading, judging,
# profile proofs) and its case format, a public spec. Pinned by git rev.
comline-runtime-conformance = { git = "https://github.com/ComlineProject/runtime", rev = "4378c91b74ee1ef1debc800a3daf75c874198e70" }
# `comline doc open` / `--open`: the system's default handler for a file (a
# browser for HTML). Exact pin; no default features (`reveal` pulls in D-Bus).
opener = { version = "=0.9.0", default-features = false }
# Runs a `generator = "<path>.wasm"` target (see src/external.rs): no
# preopened directories, no environment, no network. Exact pins, bumped
# together.
Expand Down
30 changes: 29 additions & 1 deletion docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@ argument matches no stored version (the error lists the versions that exist).
### `comline doc`

```
comline doc [--out <dir>] [--version <v>] [--changelog-from <v>] [--format <f>[,<f>...]]
comline doc [--out <dir>] [--version <v>] [--changelog-from <v>] [--format <f>[,<f>...]] [--open]
comline doc open [<format>] [--out <dir>]
```

Write the package's documentation for every version: each release, each dev
Expand Down Expand Up @@ -494,6 +495,30 @@ version carries the changelog from the version before it, computed by core's
| `--version <v>` | every version | document only this built version (a version string, a commit hash, or `HEAD`), into `<out>/<format>/<v>/` |
| `--changelog-from <v>` | `[doc] changelog_from` | with `--version`: add what changed from built version `<v>` |
| `--format <f>` | `[doc] formats`, else `markdown` | `markdown`, `html`, `json`, or another id the `[doc] renderer` offers; repeat it or separate with commas |
| `--open` | off | after rendering, open the first format's index (see `comline doc open`); with `--version`, that version's own index |

A module's summary table lists each unit with its summary, then a **Type**
column (a constant's type, a type alias's target; linked where it can be) and
a **Value** column (a constant's value), each only when some unit in the module
has one. Structs, enums, protocols and the like leave both cells empty.

#### `comline doc open [<format>]`

Open the generated documentation with the system's default handler:
`<out>/<format>/index.html`, `index.md` or `index.json`, for `html`,
`markdown` or `json`. `--out` is read as `comline doc` reads it.

- With no `<format>`, `html` if it is among `[doc] formats`, else the first
configured format.
- If that file isn't there, the format is rendered first, exactly as
`comline doc --format <format>` would. If it is, it is opened as it is,
without re-rendering; the command says so in one line and how to refresh
(`comline doc --format <format>`).
- HTML opens in the browser named by `$BROWSER`, else the system's default
(`xdg-open`, `open`, or the Windows shell); Markdown and JSON open in
whatever the system associates with the file.
- It prints the absolute path it opens. With no display, or when nothing
opens, it prints that path and a hint instead, and exits `0`.

**Caching.** A built version never changes, so its folder is kept between
runs. `<out>/<format>/.comline-doc.json` records the key each folder was
Expand Down Expand Up @@ -545,6 +570,9 @@ comline doc --format html,markdown
comline doc --version 0.2.0 --changelog-from 0.1.0
comline doc --format html --out site
comline doc --format json
comline doc --open
comline doc open
comline doc open markdown
```

### `comline conformance`
Expand Down
42 changes: 40 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,12 @@ Examples:
comline doc --format html,markdown
comline doc --version 0.2.0 --changelog-from 0.1.0
comline doc --format html --out site
comline doc --format json")]
comline doc --format json
comline doc --open
comline doc open html")]
Doc {
/// Output directory; default from `[doc] out`, else `docs/api`
#[arg(long, value_name = "DIR")]
#[arg(long, value_name = "DIR", global = true)]
out: Option<std::path::PathBuf>,

/// Document only this built version, into `<out>/<format>/<VERSION>/`
Expand All @@ -154,6 +156,13 @@ Examples:
/// `markdown` (pages), `html` (a static site) or `json` (the model), repeatable or comma-separated; default from `[doc] formats`
#[arg(long = "format", value_name = "FORMAT", value_delimiter = ',')]
format: Vec<String>,

/// After rendering, open the first format's index with the system's default handler (HTML honours `$BROWSER`)
#[arg(long)]
open: bool,

#[command(subcommand)]
command: Option<DocCommand>,
},

/// Validate the project and write generated code for each configured target
Expand Down Expand Up @@ -483,3 +492,32 @@ Examples:
json: bool,
},
}

/// `comline doc <subcommand>`.
#[derive(Debug, clap::Subcommand)]
pub enum DocCommand {
/// Open the generated documentation with the system's default handler
#[command(long_about = "\
Open the generated documentation: `<out>/<format>/index.html` (HTML), \
`index.md` (Markdown) or `index.json` (JSON). `--out` is read as in \
`comline doc`.

With no FORMAT, `html` is opened when it is among `[doc] formats`, else the \
first configured format.

If that file doesn't exist, the format is rendered first (as \
`comline doc --format FORMAT` would). If it does, it is opened as it is, \
without re-rendering; run `comline doc` to refresh it. HTML opens in the \
browser named by `$BROWSER`, else the system's default. With no display, or \
when nothing opens, the absolute path is printed and the exit status is 0.

Examples:
comline doc open
comline doc open markdown
comline doc open html --out site")]
Open {
/// `html`, `markdown` or `json`; default `html` if configured, else the first configured format
#[arg(value_name = "FORMAT")]
format: Option<String>,
},
}
110 changes: 103 additions & 7 deletions src/commands/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub struct Flags {
pub version: Option<String>,
pub changelog_from: Option<String>,
pub formats: Vec<String>,
/// `--open`: open the first format's index once rendered.
pub open: bool,
}

/// The cache manifest's file name, in each format's folder.
Expand Down Expand Up @@ -99,10 +101,9 @@ struct Version {
notes: Vec<String>,
}

pub fn run(work_dir: &Path, flags: Flags) -> Result<()> {
ensure_project(work_dir)?;
let cfg = ComlineToml::load(work_dir)?.doc;

/// Where the docs go and in which formats: the command line, else `[doc]`,
/// else the defaults.
fn out_and_formats(work_dir: &Path, cfg: &Doc, flags: &Flags) -> Result<(PathBuf, Vec<String>)> {
let out = match (&flags.out, &cfg.out) {
(Some(out), _) => work_dir.join(out),
(None, Some(out)) => work_dir.join(out),
Expand All @@ -122,6 +123,83 @@ pub fn run(work_dir: &Path, flags: Flags) -> Result<()> {
"`[doc] formats` is empty: name at least one format"
));
}
Ok((out, formats))
}

/// The file `comline doc open` opens for a format, under `<out>/<format>/`.
fn index_file(format: &str) -> Result<&'static str> {
match format {
"html" => Ok("index.html"),
"markdown" => Ok("index.md"),
"json" => Ok("index.json"),
other => Err(miette!(
"can't open `{other}` documentation: only `html`, `markdown` and `json` have an index to open"
)),
}
}

/// Opens `path` with the system's default handler, HTML through `$BROWSER`
/// first. It always says which file it is, and when nothing opens (no
/// display, no handler) says so and points at the file instead of failing.
fn open_file(path: &Path, format: &str) {
let path = std::path::absolute(path).unwrap_or_else(|_| path.to_path_buf());
ui::step(format!("Opening {}", path.display()));
let opened = if format == "html" {
opener::open_browser(&path)
} else {
opener::open(&path)
};
if let Err(e) = opened {
ui::warn(format!(
"couldn't open it ({e}); open {} yourself{}",
path.display(),
if format == "html" {
", or set `$BROWSER`"
} else {
""
}
));
}
}

/// `comline doc open [FORMAT]`: open the generated docs, rendering the
/// format first only when its index isn't there.
pub fn open(work_dir: &Path, out: Option<PathBuf>, format: Option<String>) -> Result<()> {
ensure_project(work_dir)?;
let cfg = ComlineToml::load(work_dir)?.doc;
let flags = Flags {
out,
..Flags::default()
};
let (out, configured) = out_and_formats(work_dir, &cfg, &flags)?;
let format = match format {
Some(format) => format,
None if configured.iter().any(|f| f == "html") => "html".to_string(),
None => configured[0].clone(),
};
let index = out.join(&format).join(index_file(&format)?);
if index.is_file() {
ui::note(format!(
"Opening the existing {format} docs as they are; run `comline doc --format {format}` to refresh them"
));
} else {
run(
work_dir,
Flags {
out: flags.out,
formats: vec![format.clone()],
..Flags::default()
},
)?;
}
open_file(&index, &format);
Ok(())
}

pub fn run(work_dir: &Path, flags: Flags) -> Result<()> {
ensure_project(work_dir)?;
let cfg = ComlineToml::load(work_dir)?.doc;
let (out, formats) = out_and_formats(work_dir, &cfg, &flags)?;
let changelog_from = flags.changelog_from.or(cfg.changelog_from.clone());
let renderers = Renderers::new(work_dir, &cfg)?;

Expand All @@ -138,7 +216,16 @@ pub fn run(work_dir: &Path, flags: Flags) -> Result<()> {
&version.schemas,
));
}
return document_one(&out, &formats, &renderers, &cfg, version);
let first = document_one(&out, &formats, &renderers, &cfg, version)?;
if flags.open {
// A single version has no site index: open its own.
let (format, dir) = first;
if let Ok(file) = index_file(&format) {
let file = if format == "json" { "model.json" } else { file };
open_file(&dir.join(file), &format);
}
}
return Ok(());
}
if changelog_from.is_some() {
ui::warn(
Expand All @@ -161,6 +248,13 @@ pub fn run(work_dir: &Path, flags: Flags) -> Result<()> {
dir.display()
));
}
if flags.open {
let format = &formats[0];
match index_file(format) {
Ok(file) => open_file(&out.join(format).join(file), format),
Err(e) => ui::warn(format!("{e}")),
}
}
Ok(())
}

Expand All @@ -172,7 +266,8 @@ fn document_one(
renderers: &Renderers,
cfg: &Doc,
version: Version,
) -> Result<()> {
) -> Result<(String, PathBuf)> {
let mut first = None;
for format in formats {
let dir = out.join(format);
let files = renderers.render_version(format, cfg, &version)?;
Expand All @@ -190,8 +285,9 @@ fn document_one(
target.display(),
files.len()
));
first.get_or_insert((format.clone(), target));
}
Ok(())
Ok(first.expect("formats is never empty"))
}

/// Every version, each rendered unless its cached folder still matches,
Expand Down
Loading
Loading