Skip to content

Commit

Permalink
Add support for CSS in the CLI. (#9)
Browse files Browse the repository at this point in the history
* Add support for CSS in the CLI. Resolve #8
  • Loading branch information
mamcx committed Jun 17, 2023
1 parent 3f6d5ab commit 8cfdf29
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
3 changes: 2 additions & 1 deletion examples/example_diagrams.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use htmlescape;
use railroad_dsl;

use railroad::DEFAULT_CSS;
use std::fs;
use std::io;
use std::io::{Read, Write};
Expand All @@ -23,7 +24,7 @@ fn main() -> Result<(), io::Error> {
println!("Generating from `{}`", filename);
let mut buffer = String::new();
fs::File::open(path.path())?.read_to_string(&mut buffer)?;
let diagram = railroad_dsl::compile(&buffer).unwrap();
let diagram = railroad_dsl::compile(&buffer, DEFAULT_CSS).unwrap();
write!(outp, "<h3>Generated from <i>`{}`</i></h3>", filename)?;
write!(
outp,
Expand Down
26 changes: 20 additions & 6 deletions src/bin/railroad.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use railroad::DEFAULT_CSS;
use std::fs;
use std::io::{self, Read};
use std::path::PathBuf;
Expand All @@ -12,21 +13,27 @@ the file extension replaced by `.svg`.")]
struct Options {
#[structopt(help = "Input files to process")]
inputs: Vec<String>,
#[structopt(
help = "Alternative CSS file for the SVG",
long = "css",
parse(from_os_str)
)]
css: Option<PathBuf>,
}

enum Error {
Parser(Box<pest::error::Error<railroad_dsl::Rule>>),
IO(io::Error),
}

fn dia_from_stdin() -> Result<(), Error> {
fn dia_from_stdin(css: &str) -> Result<(), Error> {
let mut buf = String::new();
match io::stdin().read_to_string(&mut buf) {
Err(e) => {
eprintln!("error reading stdin: {e}");
Err(Error::IO(e))
}
Ok(_) => match railroad_dsl::compile(&buf) {
Ok(_) => match railroad_dsl::compile(&buf, css) {
Err(e) => {
eprintln!("syntax error:\n{}", e.clone().with_path("<stdin>"));
Err(Error::Parser(e))
Expand All @@ -39,7 +46,7 @@ fn dia_from_stdin() -> Result<(), Error> {
}
}

fn dia_from_files(inputs: &[String]) -> Result<(), Error> {
fn dia_from_files(inputs: &[String], css: &str) -> Result<(), Error> {
let mut err = Ok(());
for input in inputs {
let output = PathBuf::from(&input).with_extension("svg");
Expand All @@ -48,7 +55,7 @@ fn dia_from_files(inputs: &[String]) -> Result<(), Error> {
eprintln!("error reading file {input}: {e}");
err = Err(Error::IO(e));
}
Ok(buf) => match railroad_dsl::compile(&buf) {
Ok(buf) => match railroad_dsl::compile(&buf, css) {
Err(e) => {
eprintln!("syntax error:\n{}", e.clone().with_path(input));
err = Err(Error::Parser(e));
Expand All @@ -66,10 +73,17 @@ fn dia_from_files(inputs: &[String]) -> Result<(), Error> {
}

fn run(args: &Options) -> Result<(), Error> {
let css = args
.css
.as_deref()
.map(|f| fs::read_to_string(f))
.unwrap_or_else(|| Ok(DEFAULT_CSS.to_string()))
.map_err(Error::IO)?;

if args.inputs.is_empty() {
dia_from_stdin()
dia_from_stdin(&css)
} else {
dia_from_files(&args.inputs)
dia_from_files(&args.inputs, &css)
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use railroad as rr;

use pest::iterators::Pair;
use pest::Parser;
use railroad::svg;

#[derive(Parser)]
#[grammar = "parser.pest"]
Expand Down Expand Up @@ -73,7 +74,7 @@ fn start_to_end(root: Box<dyn rr::Node>) -> Box<dyn rr::Node> {
]))
}

pub fn compile(src: &str) -> Result<Diagram, Box<pest::error::Error<Rule>>> {
pub fn compile(src: &str, css: &str) -> Result<Diagram, Box<pest::error::Error<Rule>>> {
let mut result = RRParser::parse(Rule::input, src)?;
let trees = result.next().expect("expected root_expr").into_inner();
let mut trees: Vec<_> = trees.map(|p| start_to_end(make_node(p))).collect();
Expand All @@ -82,7 +83,14 @@ pub fn compile(src: &str) -> Result<Diagram, Box<pest::error::Error<Rule>>> {
} else {
Box::new(rr::VerticalGrid::new(trees))
};
let diagram = rr::Diagram::with_default_css(root);

let mut diagram = rr::Diagram::new(root);
diagram.add_element(
svg::Element::new("style")
.set("type", "text/css")
.raw_text(css),
);

let width = (&diagram as &dyn rr::Node).width();
let height = (&diagram as &dyn rr::Node).height();
Ok(Diagram {
Expand All @@ -95,6 +103,7 @@ pub fn compile(src: &str) -> Result<Diagram, Box<pest::error::Error<Rule>>> {
#[cfg(test)]
mod tests {
use super::*;
use railroad::DEFAULT_CSS;
use std::env;
use std::fs;
use std::io::Read;
Expand All @@ -114,7 +123,7 @@ mod tests {
.unwrap()
.read_to_string(&mut buffer)
.unwrap();
if let Err(e) = compile(&buffer) {
if let Err(e) = compile(&buffer, DEFAULT_CSS) {
panic!("Failed to compile {}", e.with_path(filename));
}
}
Expand Down

0 comments on commit 8cfdf29

Please sign in to comment.