Skip to content

Commit

Permalink
feat: add support to infer book meta from outline (#75)
Browse files Browse the repository at this point in the history
* feat: partially support to infer book meta

* fix: errors

* fix: keep strict default meta source

* dev: shrink modules

* dev: remove one todo
  • Loading branch information
Myriad-Dreamin authored Jun 15, 2024
1 parent 1cd5d6e commit 54802cd
Show file tree
Hide file tree
Showing 8 changed files with 1,120 additions and 22 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ clap_complete_fig.workspace = true
comemo.workspace = true
# chrono.workspace = true
tokio.workspace = true
indexmap = "2"
include_dir.workspace = true

serde.workspace = true
Expand Down
66 changes: 66 additions & 0 deletions cli/src/debug_loc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use serde::{Deserialize, Serialize};
use typst::layout::Position as TypstPosition;

/// A serializable physical position in a document.
///
/// Note that it uses [`f32`] instead of [`f64`] as same as
/// [`TypstPosition`] for the coordinates to improve both performance
/// of serialization and calculation. It does sacrifice the floating
/// precision, but it is enough in our use cases.
///
/// Also see [`TypstPosition`].
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct DocumentPosition {
/// The page, starting at 1.
pub page_no: usize,
/// The exact x-coordinate on the page (from the left, as usual).
pub x: f32,
/// The exact y-coordinate on the page (from the top, as usual).
pub y: f32,
}

impl From<TypstPosition> for DocumentPosition {
fn from(position: TypstPosition) -> Self {
Self {
page_no: position.page.into(),
x: position.point.x.to_pt() as f32,
y: position.point.y.to_pt() as f32,
}
}
}

// /// Unevaluated source span.
// /// The raw source span is unsafe to serialize and deserialize.
// /// Because the real source location is only known during liveness of
// /// the compiled document.
// pub type SourceSpan = typst::syntax::Span;

// /// Raw representation of a source span.
// pub type RawSourceSpan = u64;

// /// A char position represented in form of line and column.
// /// The position is encoded in Utf-8 or Utf-16, and the encoding is
// /// determined by usage.
// ///
// pub struct CharPosition {
// /// The line number, starting at 0.
// line: usize,
// /// The column number, starting at 0.
// column: usize,
// }

// /// A resolved source (text) location.
// ///
// /// See [`CharPosition`] for the definition of the position inside a file.
// pub struct SourceLocation {
// filepath: PathBuf,
// pos: CharPosition,
// }
// /// A resolved source (text) range.
// ///
// /// See [`CharPosition`] for the definition of the position inside a file.
// pub struct SourceRange {
// filepath: PathBuf,
// start: CharPosition,
// end: CharPosition,
// }
30 changes: 29 additions & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
mod debug_loc;
pub mod error;
pub mod meta;
pub mod outline;
pub mod project;
pub mod render;
pub mod theme;
pub mod utils;
pub mod version;
use version::VersionFormat;

use core::fmt;
use std::path::PathBuf;

use clap::{ArgAction, Parser, Subcommand};
use clap::{ArgAction, Parser, Subcommand, ValueEnum};

#[derive(Debug, Parser)]
#[clap(name = "shiroa", version = "0.1.0")]
Expand Down Expand Up @@ -42,6 +45,27 @@ pub enum Subcommands {
Serve(ServeArgs),
}

/// Determine the approach to retrieving metadata of a book project.
#[derive(ValueEnum, Debug, Clone, Eq, PartialEq, Default)]
#[value(rename_all = "kebab-case")]
pub enum MetaSource {
/// Strictly retrieve the project's meta by label queries.
/// + retrieve the book meta from `<shiroa-book-meta>`
/// + retrieve the build meta from `<shiroa-build-meta>`
Strict,
/// Infer the project's meta from the outline of main file.
/// Note: if the main file also contains `<shiroa-book-meta>` or
/// `<shiroa-build-meta>`, the manual-set meta will be used first.
#[default]
Outline,
}

impl fmt::Display for MetaSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.to_possible_value().unwrap().get_name())
}
}

#[derive(Default, Debug, Clone, Parser)]
#[clap(next_help_heading = "Compile options")]
pub struct CompileArgs {
Expand All @@ -50,6 +74,10 @@ pub struct CompileArgs {
#[clap(default_value = "")]
pub dir: String,

/// Determine the approach to retrieving metadata of the book project.
#[clap(long, default_value = "strict")]
pub meta_source: MetaSource,

/// Root directory for the typst workspace, which is same as the
/// `typst-cli`'s root. (Defaults to the root directory for the book
/// when omitted)
Expand Down
2 changes: 1 addition & 1 deletion cli/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub enum BookMetaElem {

/// General information about your book.
/// Book metadata in summary.typ
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct BookMeta {
/// The title of the book
pub title: String,
Expand Down
Loading

0 comments on commit 54802cd

Please sign in to comment.