ziyy_core/
lib.rs

1#![allow(clippy::pedantic)]
2#![doc = include_str!("../README.md")]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4
5pub use error::{Error, ErrorType, Result};
6
7#[cfg(feature = "unstable")]
8#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
9pub use crate::{
10    indexer::Indexer,
11    parser::{
12        Parser,
13        ansi::{Ansi, AnsiOptions, DuoEffect, Effect},
14        chunk::{Chunk, ChunkData},
15        color::{Ansi4Bit, Ansi256, Color, Rgb},
16        tag_parser::TagParser,
17        tag_parser::tag::{Tag, TagType},
18        word_parser::WORD_PARSER,
19    },
20    resolver::{
21        Resolver,
22        document::{Document, Node},
23    },
24    splitter::{
25        Splitter,
26        fragment::{Fragment, FragmentType},
27    },
28};
29
30#[cfg(not(feature = "unstable"))]
31use crate::{
32    indexer::Indexer,
33    parser::Parser,
34    resolver::Resolver,
35    splitter::{
36        Splitter,
37        fragment::{Fragment, FragmentType},
38    },
39};
40
41pub use common::{Position, Span};
42
43mod builtin;
44mod error;
45#[macro_use]
46mod scanner;
47mod common;
48mod indexer;
49mod parser;
50mod resolver;
51mod splitter;
52
53// mod ziyy;
54
55/// Styles the given text using ziyy.
56///
57/// # Example
58///
59/// ```
60/// # use ziyy_core as ziyy;
61/// use ziyy::style;
62///
63/// let styled_text = style("This is <b>bold</b> text");
64/// ```
65/// # Panics
66///
67/// This function will panic if the parser encounters an error while parsing the input source.
68pub fn style<T: AsRef<str>>(source: T) -> String {
69    match try_style(source) {
70        Ok(v) => v,
71        Err(e) => panic!("{}", e),
72    }
73}
74
75/// Styles the given text using ziyy.
76///
77/// # Example
78///
79/// ```
80/// # fn main() -> ziyy_core::Result<()> {
81/// # use ziyy_core as ziyy;
82/// use ziyy::try_style;
83///
84/// let styled_text = try_style(r#"
85/// <let id="custom" c="blue">
86///     This is a custom element.
87/// </let>
88/// <span class='s custom'>This text is in blue</span>"#)?;
89/// # Ok(())
90/// # }
91pub fn try_style<T: AsRef<str>>(source: T) -> Result<String> {
92    if source.as_ref().is_empty() {
93        return Ok(String::new());
94    }
95
96    let mut indexer = Indexer::new();
97    let source = indexer.index(source.as_ref());
98    let mut splitter = Splitter::new();
99    let frags = splitter.split(&source)?;
100
101    let parser = Parser::new(false);
102    let chunks = parser.parse(frags);
103
104    let mut resolver = Resolver::new(false);
105    let output = resolver.resolve(chunks)?;
106
107    let mut buf = String::new();
108    output.root().to_string(&mut buf);
109    Ok(buf)
110}