Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/trimorphdev/highlighter
An extendable syntax highlighter written in Rust.
https://github.com/trimorphdev/highlighter
lexer lexer-framework rust rust-crate rust-crates syntax syntax-highlight syntax-highlighting syntax-highlighting-theme
Last synced: about 3 hours ago
JSON representation
An extendable syntax highlighter written in Rust.
- Host: GitHub
- URL: https://github.com/trimorphdev/highlighter
- Owner: trimorphdev
- License: mit
- Created: 2022-08-28T08:40:47.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2022-08-28T15:31:35.000Z (about 2 years ago)
- Last Synced: 2024-10-31T11:26:33.407Z (16 days ago)
- Topics: lexer, lexer-framework, rust, rust-crate, rust-crates, syntax, syntax-highlight, syntax-highlighting, syntax-highlighting-theme
- Language: Rust
- Homepage:
- Size: 17.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Highlighter
An extendable syntax highlighter written in Rust.## Installation
Simply add highlighter to your dependencies:
```toml
[dependencies]
highlighter = "0.1.1-alpha"
```## Basic Usage
```rust
use highlighter::{highlight_language, HighlightTargetHtml};fn main() {
let result = highlight_language("brainheck", "++++++++ set current cell to 8").unwrap().unwrap();
let html = HighlightTargetHtml::new()
.build(result);
println!(html);
}
```## Implementing Languages
To extend Highlighter, make a structure which implements the `Language` trait.```rust
extern crate highlighter;use highlighter::{core::language::{Language, Scope}, highlight, HighlighterTargetHtml};
/// My example programming language
pub struct MyLanguage;impl Language for MyLanguage {
fn name(&self) -> String {
"my-language".to_string()
}
fn init(&self, cx: &mut highlighter::core::LexerContext) -> Result<(), highlighter::core::Error> {
// Initialize the tokens in your language.
cx.token(Scope::KeywordControl, "\\b(if|else|while|continue|break|return)\\b")?;
cx.token(Scope::StorageType, "\\b(var|function)\\b")?;
cx.token(Scope::ConstantNumber, "\\b([0-9][0-9_]*)\\b")?;
cx.token(Scope::ConstantLanguage, "\\b(true|false)\\b")?;
Ok(())
}
}fn main() {
let tokens = highlight(MyLanguage, "var i = 0;").unwrap();
let html = HighlighterTargetHtml::new()
.build(tokens);
println!("{}", html);
}
```