Skip to main content

Crate cosy

Crate cosy 

Source
Expand description

§cosy

Crates.io Documentation CI License: MIT

A parser for Cosense (formerly Scrapbox) markup syntax that produces a typed AST.

§Installation

[dependencies]
cosy = "0.1"

To enable JSON serialization of the AST via serde:

[dependencies]
cosy = { version = "0.1", features = ["serde"] }

§Usage

use cosy::ast::{BlockContent, Node};

let doc = cosy::parse("Hello [* world]! See [https://example.com].", &()).unwrap();

for block in &doc {
    if let BlockContent::Line(nodes) = &block.content {
        for node in nodes {
            println!("{node:?}");
        }
    }
}

§Custom syntax extensions

Implement CosyParserExtension to inject your own bracket syntax:

use cosy::CosyParserExtension;

#[derive(Debug, PartialEq)]
enum MySyntax { Highlight(String) }

struct MyExt;
impl CosyParserExtension for MyExt {
    type Output = MySyntax;
    fn parse_bracket(&self, content: &str) -> Option<MySyntax> {
        content.strip_prefix("! ").map(|s| MySyntax::Highlight(s.to_string()))
    }
}

let doc = cosy::parse("[! important]", &MyExt).unwrap();

See examples/speech_bubble_extension.rs for a full example.

§URL handling

Labeled links ([label https://...]) carry a parsed url::Url in Link::WithLabel.href. cosy re-exports the Url type at its crate root for convenience:

use cosy::Url;

§Supported syntax

§Block-level

SyntaxDescription
(indented lines)Nested bullet list
code:filenameCode block
table:nameTable
> textQuote
? queryHelpfeel search query
$ command % commandCommand-line notation (bash / csh)

§Inline

SyntaxDescription
[Page Name]Internal page link
[/project/page]Cross-project link
[https://...]External URL
[URL Label]Labeled link
[image.png]Image (detected by MIME type)
[img link]Linked image
[name.icon] [name.icon*3]User icon
`code`Inline code
[$ expr]Math (LaTeX)
[* bold] [/ italic] [- strike]Decoration
[[text]]Strong (large bold) text
[[image.png]]Strong (large) image
[[https://...]]Strong (large) link
[[name.icon]]Strong (large) icon
#tagHashtag
[N35.xx,E139.xx] [N35.xx,E139.xx,Z14]Geographic coordinate (map embed)

§License

MIT — see LICENSE.

Re-exports§

pub use error::ParseError;

Modules§

ast
Abstract Syntax Tree (AST) definitions for the parser.
error
Error types for the cosy parser.

Structs§

Url
A parsed URL record.

Traits§

CosyParserExtension
A trait that enables parsing of user-defined bracket syntax.

Functions§

parse
Parses an input string into a ast::Document AST.