From f3231b52c4a7c2d9c4db79686ab7a7495caa33e1 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Wed, 7 Aug 2024 15:13:16 +0300 Subject: [PATCH] feat: meta tag, resolve title --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/daletl.rs | 3 ++- src/traits/from_daletl.rs | 3 ++- src/traits/mod.rs | 1 + src/traits/resolve_title.rs | 37 +++++++++++++++++++++++++++++++++++++ src/traits/to_daletl.rs | 3 ++- src/typed.rs | 7 ++++++- 8 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 src/traits/resolve_title.rs diff --git a/Cargo.lock b/Cargo.lock index 18bc624..c51b0cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -151,7 +151,7 @@ dependencies = [ [[package]] name = "dalet" -version = "1.0.0-pre.12" +version = "1.0.0-pre.13" dependencies = [ "bincode", "clap", diff --git a/Cargo.toml b/Cargo.toml index c157272..ad01074 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dalet" -version = "1.0.0-pre.12" +version = "1.0.0-pre.13" edition = "2021" authors = ["artegoser"] license = "MIT" diff --git a/src/daletl.rs b/src/daletl.rs index 42ae55b..df0ebd6 100644 --- a/src/daletl.rs +++ b/src/daletl.rs @@ -73,10 +73,11 @@ pub enum DlTid { Sup, Sub, Disc, - Bl, + Block, Carousel, Code, Pre, + Meta, } pub trait IsNull { diff --git a/src/traits/from_daletl.rs b/src/traits/from_daletl.rs index 6877d9e..0aec2cd 100644 --- a/src/traits/from_daletl.rs +++ b/src/traits/from_daletl.rs @@ -37,10 +37,11 @@ impl TryFrom for Tag { DlTid::Sup => Sup(tag.body.try_into()?), DlTid::Sub => Sub(tag.body.try_into()?), DlTid::Disc => Disc(tag.body.try_into()?), - DlTid::Bl => Bl(tag.body.try_into()?, tag.argument.try_into()?), + DlTid::Block => Block(tag.body.try_into()?, tag.argument.try_into()?), DlTid::Carousel => Carousel(tag.body.try_into()?), DlTid::Code => Code(tag.body.try_into()?, tag.argument.try_into()?), DlTid::Pre => Pre(tag.body.try_into()?), + DlTid::Meta => Meta(tag.body.try_into()?, tag.argument.try_into()?), }; Ok(result) diff --git a/src/traits/mod.rs b/src/traits/mod.rs index 23dd098..9ed6a88 100644 --- a/src/traits/mod.rs +++ b/src/traits/mod.rs @@ -1,3 +1,4 @@ mod from_daletl; mod is_null_daletl; +mod resolve_title; mod to_daletl; diff --git a/src/traits/resolve_title.rs b/src/traits/resolve_title.rs new file mode 100644 index 0000000..a4093f7 --- /dev/null +++ b/src/traits/resolve_title.rs @@ -0,0 +1,37 @@ +use crate::typed::{ + Hl, NNBody, Page, ResolveTitle, + Tag::{self, Block, Meta, H}, +}; + +impl ResolveTitle for Page { + fn resolve_title(&self) -> Option<&String> { + resolve_from_tags(&self.data) + } +} + +fn resolve_from_tags(tags: &Vec) -> Option<&String> { + for tag in tags { + match tag { + H(title, level) => { + if *level == Hl::One { + return Some(title); + } + } + + Meta(body, key) => { + if key == "title" { + return Some(body); + } + } + + Block(body, _) => match body { + NNBody::Tags(tags) => return resolve_from_tags(tags), + _ => {} + }, + + _ => {} + }; + } + + None +} diff --git a/src/traits/to_daletl.rs b/src/traits/to_daletl.rs index f152a9e..089b1aa 100644 --- a/src/traits/to_daletl.rs +++ b/src/traits/to_daletl.rs @@ -32,10 +32,11 @@ impl From for DlTag { Tag::Sup(b) => dlt_new(DlTid::Sup, b.into(), NA), Tag::Sub(b) => dlt_new(DlTid::Sub, b.into(), NA), Tag::Disc(b) => dlt_new(DlTid::Disc, b.into(), NA), - Tag::Bl(b, a) => dlt_new(DlTid::Bl, b.into(), a.into()), + Tag::Block(b, a) => dlt_new(DlTid::Block, b.into(), a.into()), Tag::Carousel(b) => dlt_new(DlTid::Carousel, b.into(), NA), Tag::Code(b, a) => dlt_new(DlTid::Code, b.into(), a.into()), Tag::Pre(b) => dlt_new(DlTid::Pre, b.into(), NA), + Tag::Meta(b, a) => dlt_new(DlTid::Meta, b.into(), a.into()), } } } diff --git a/src/typed.rs b/src/typed.rs index eff65ef..d03732d 100644 --- a/src/typed.rs +++ b/src/typed.rs @@ -42,10 +42,11 @@ pub enum Tag { Sup(TBody), Sub(TBody), Disc(NNBody), - Bl(NNBody, AlignArg), + Block(NNBody, AlignArg), Carousel(Vec), Code(TBody, TNArg), Pre(TBody), + Meta(TBody, TArg), } #[derive(AutoFrom, Debug, Clone, PartialEq, Eq)] @@ -106,3 +107,7 @@ pub enum Hl { Five, Six, } + +pub trait ResolveTitle { + fn resolve_title(&self) -> Option<&String>; +}