mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
refactor(cli-rs): share Mermaid's accessibility directive filter
Gantt, timeline and gitGraph each kept the same block state and compiled the same two regular expressions. Share that filter after each parser strips its own comments: gitGraph honors quoted percent signs, unlike gantt and timeline. diagrams_match_typescript checks the resulting diagrams against the TS renderer.
This commit is contained in:
@@ -122,6 +122,25 @@ pub fn first_meaningful_line(content: &str) -> Option<&str> {
|
||||
meaningful_lines(content).next().map(|line| line.text)
|
||||
}
|
||||
|
||||
/// Accessibility directives describe the diagram but do not draw rows. A block ends on a line containing only `}`.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Accessibility {
|
||||
in_description: bool,
|
||||
}
|
||||
|
||||
impl Accessibility {
|
||||
pub(crate) fn skip(&mut self, line: &str) -> bool {
|
||||
static DIRECTIVE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)^acc(?:Title|Descr)(?::|\s|$)").unwrap());
|
||||
static BLOCK: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)^accDescr\s*\{$").unwrap());
|
||||
if self.in_description {
|
||||
self.in_description = line != "}";
|
||||
return true;
|
||||
}
|
||||
self.in_description = BLOCK.is_match(line);
|
||||
self.in_description || DIRECTIVE.is_match(line)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn strip_quotes(value: &str) -> String {
|
||||
let trimmed = js_trim(value);
|
||||
let quoted =
|
||||
|
||||
@@ -8,7 +8,7 @@ use regex::Regex;
|
||||
|
||||
use crate::core::canvas::{Canvas, SizeError};
|
||||
use crate::core::geometry::js_round;
|
||||
use crate::core::text::{first_meaningful_line, js_trim, meaningful_lines, strip_quotes, width};
|
||||
use crate::core::text::{Accessibility, first_meaningful_line, js_trim, meaningful_lines, strip_quotes, width};
|
||||
use crate::{Kind, SyntaxError};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
@@ -55,8 +55,6 @@ static HEADER: LazyLock<Regex> = LazyLock::new(|| pattern(r"(?i)^gantt$"));
|
||||
static DIRECTIVE: LazyLock<Regex> =
|
||||
LazyLock::new(|| pattern(r"(?i)^(title|dateFormat|axisFormat|tickInterval|excludes|todayMarker)\s+(.+)$"));
|
||||
static SECTION: LazyLock<Regex> = LazyLock::new(|| pattern(r"(?i)^section(?:\s+(.+))?$"));
|
||||
static ACCESSIBILITY: LazyLock<Regex> = LazyLock::new(|| pattern(r"(?i)^acc(?:Title|Descr)(?::|\s|$)"));
|
||||
static ACC_BLOCK: LazyLock<Regex> = LazyLock::new(|| pattern(r"(?i)^accDescr\s*\{$"));
|
||||
static AFTER: LazyLock<Regex> = LazyLock::new(|| pattern(r"(?i)^after\s+"));
|
||||
static DURATION: LazyLock<Regex> = LazyLock::new(|| pattern(r"(?i)^(\d+(?:\.\d+)?)\s*(ms|s|m|h|d|w)$"));
|
||||
static CALENDAR: LazyLock<Regex> =
|
||||
@@ -77,21 +75,11 @@ pub fn parse(content: &str) -> Result<Diagram, SyntaxError> {
|
||||
let mut date_format = "YYYY-MM-DD".to_string();
|
||||
let mut axis_format = "%Y-%m-%d".to_string();
|
||||
let mut header = false;
|
||||
let mut in_description = false;
|
||||
let mut accessibility = Accessibility::default();
|
||||
for source in meaningful_lines(content) {
|
||||
let line = js_trim(source.text.find("%%").map_or(source.text, |index| &source.text[..index]));
|
||||
let n = source.number;
|
||||
if in_description {
|
||||
if line == "}" {
|
||||
in_description = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if ACC_BLOCK.is_match(line) {
|
||||
in_description = true;
|
||||
continue;
|
||||
}
|
||||
if ACCESSIBILITY.is_match(line) || line.is_empty() {
|
||||
if accessibility.skip(line) || line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if HEADER.is_match(line) {
|
||||
|
||||
@@ -7,7 +7,7 @@ use indexmap::IndexMap;
|
||||
use regex::Regex;
|
||||
|
||||
use crate::core::canvas::{Canvas, SizeError};
|
||||
use crate::core::text::{first_meaningful_line, js_trim, meaningful_lines, strip_quotes, width};
|
||||
use crate::core::text::{Accessibility, first_meaningful_line, js_trim, meaningful_lines, strip_quotes, width};
|
||||
use crate::{Kind, SyntaxError};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
@@ -55,9 +55,6 @@ pub struct Diagram {
|
||||
|
||||
static HEADER: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"(?i)^gitGraph(?:\s+(LR|TB|BT))?\s*:?$").expect("header"));
|
||||
static ACCESSIBILITY: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(r"(?i)^acc(?:Title|Descr)(?::|\s|$)").expect("accessibility"));
|
||||
static ACC_BLOCK: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)^accDescr\s*\{$").expect("accDescr"));
|
||||
static TITLE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)^title(?:\s|$)").expect("title"));
|
||||
|
||||
pub fn is_gitgraph(content: &str) -> bool {
|
||||
@@ -81,22 +78,12 @@ pub fn parse(content: &str) -> Result<Diagram, SyntaxError> {
|
||||
let mut ids: HashSet<String> = HashSet::new();
|
||||
let mut current = "main".to_string();
|
||||
let mut generated = 1;
|
||||
let mut in_description = false;
|
||||
let mut accessibility = Accessibility::default();
|
||||
let mut header = false;
|
||||
for source in meaningful_lines(content) {
|
||||
let line = strip_comment(source.text);
|
||||
let n = source.number;
|
||||
if in_description {
|
||||
if line == "}" {
|
||||
in_description = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if ACC_BLOCK.is_match(line) {
|
||||
in_description = true;
|
||||
continue;
|
||||
}
|
||||
if line.is_empty() || ACCESSIBILITY.is_match(line) || TITLE.is_match(line) {
|
||||
if line.is_empty() || accessibility.skip(line) || TITLE.is_match(line) {
|
||||
continue;
|
||||
}
|
||||
if HEADER.is_match(line) {
|
||||
|
||||
@@ -5,7 +5,9 @@ use std::sync::LazyLock;
|
||||
use regex::Regex;
|
||||
|
||||
use crate::core::canvas::{Canvas, SizeError};
|
||||
use crate::core::text::{first_meaningful_line, js_trim, meaningful_lines, split_lines, strip_quotes, width};
|
||||
use crate::core::text::{
|
||||
Accessibility, first_meaningful_line, js_trim, meaningful_lines, split_lines, strip_quotes, width,
|
||||
};
|
||||
use crate::{Kind, SyntaxError};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
@@ -41,8 +43,6 @@ fn pattern(source: &str) -> Regex {
|
||||
static HEADER: LazyLock<Regex> = LazyLock::new(|| pattern(r"(?i)^timeline(?:\s+(TD|LR))?$"));
|
||||
static TITLE: LazyLock<Regex> = LazyLock::new(|| pattern(r"(?i)^title(?:\s+(.+))?$"));
|
||||
static SECTION: LazyLock<Regex> = LazyLock::new(|| pattern(r"(?i)^section(?:\s+(.+))?$"));
|
||||
static ACCESSIBILITY: LazyLock<Regex> = LazyLock::new(|| pattern(r"(?i)^acc(?:Title|Descr)(?::|\s|$)"));
|
||||
static ACC_BLOCK: LazyLock<Regex> = LazyLock::new(|| pattern(r"(?i)^accDescr\s*\{$"));
|
||||
|
||||
pub fn is_timeline(content: &str) -> bool {
|
||||
HEADER.is_match(first_meaningful_line(content).unwrap_or(""))
|
||||
@@ -57,21 +57,11 @@ pub fn parse(content: &str) -> Result<Diagram, SyntaxError> {
|
||||
let mut title = None;
|
||||
// The index in `entries` of the period continuation lines add to.
|
||||
let mut current: Option<usize> = None;
|
||||
let mut in_description = false;
|
||||
let mut accessibility = Accessibility::default();
|
||||
for source in meaningful_lines(content) {
|
||||
let line = js_trim(source.text.find("%%").map_or(source.text, |index| &source.text[..index]));
|
||||
let n = source.number;
|
||||
if in_description {
|
||||
if line == "}" {
|
||||
in_description = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if ACC_BLOCK.is_match(line) {
|
||||
in_description = true;
|
||||
continue;
|
||||
}
|
||||
if line.is_empty() || line.starts_with('#') || ACCESSIBILITY.is_match(line) || HEADER.is_match(line) {
|
||||
if line.is_empty() || line.starts_with('#') || accessibility.skip(line) || HEADER.is_match(line) {
|
||||
continue;
|
||||
}
|
||||
if let Some(captures) = TITLE.captures(line) {
|
||||
|
||||
Reference in New Issue
Block a user