grass/src/output.rs

215 lines
7.0 KiB
Rust
Raw Normal View History

2020-01-05 12:52:50 -05:00
//! # Convert from SCSS AST to CSS
2020-03-01 14:53:52 -05:00
use std::io::Write;
2020-06-16 19:38:30 -04:00
use codemap::CodeMap;
2020-04-24 22:57:39 -04:00
2020-06-16 20:00:11 -04:00
use crate::{error::SassResult, parse::Stmt, selector::Selector, style::Style};
#[derive(Debug, Clone)]
enum Toplevel {
RuleSet(Selector, Vec<BlockEntry>),
MultilineComment(String),
2020-06-16 19:38:30 -04:00
UnknownAtRule {
name: String,
params: String,
body: Vec<Stmt>,
},
Media {
params: String,
body: Vec<Stmt>,
},
Newline,
2020-04-12 21:47:32 -04:00
Style(Box<Style>),
}
#[derive(Debug, Clone)]
enum BlockEntry {
2020-02-14 18:28:09 -05:00
Style(Box<Style>),
MultilineComment(String),
}
2020-04-12 19:37:12 -04:00
impl BlockEntry {
pub fn to_string(&self) -> SassResult<String> {
match self {
2020-04-12 19:37:12 -04:00
BlockEntry::Style(s) => s.to_string(),
BlockEntry::MultilineComment(s) => Ok(format!("/*{}*/", s)),
}
}
}
impl Toplevel {
const fn new_rule(selector: Selector) -> Self {
Toplevel::RuleSet(selector, Vec::new())
}
2020-04-01 17:37:07 -04:00
fn push_style(&mut self, mut s: Style) -> SassResult<()> {
2020-04-12 19:37:12 -04:00
s = s.eval()?;
2020-04-19 00:39:18 -04:00
if s.value.is_null(s.value.span)? {
2020-04-01 17:37:07 -04:00
return Ok(());
}
if let Toplevel::RuleSet(_, entries) = self {
2020-02-14 18:28:09 -05:00
entries.push(BlockEntry::Style(Box::new(s)));
}
2020-04-01 17:37:07 -04:00
Ok(())
}
fn push_comment(&mut self, s: String) {
if let Toplevel::RuleSet(_, entries) = self {
entries.push(BlockEntry::MultilineComment(s));
}
}
}
#[derive(Debug, Clone)]
pub struct Css {
blocks: Vec<Toplevel>,
}
impl Css {
2020-01-05 12:52:50 -05:00
pub const fn new() -> Self {
2020-01-19 19:27:52 -05:00
Css { blocks: Vec::new() }
}
2020-06-16 19:38:30 -04:00
pub(crate) fn from_stmts(s: Vec<Stmt>) -> SassResult<Self> {
Css::new().parse_stylesheet(s)
}
2020-04-01 17:37:07 -04:00
fn parse_stmt(&mut self, stmt: Stmt) -> SassResult<Vec<Toplevel>> {
Ok(match stmt {
2020-06-16 19:38:30 -04:00
Stmt::RuleSet {
selector,
super_selector,
2020-06-16 19:38:30 -04:00
body,
} => {
let selector = selector
.resolve_parent_selectors(&super_selector, true)
.remove_placeholders();
if selector.is_empty() {
2020-04-01 17:37:07 -04:00
return Ok(Vec::new());
}
let mut vals = vec![Toplevel::new_rule(selector)];
2020-06-16 19:38:30 -04:00
for rule in body {
match rule {
Stmt::RuleSet { .. } => vals.extend(self.parse_stmt(rule)?),
2020-05-24 16:57:07 -04:00
Stmt::Style(s) => vals.get_mut(0).unwrap().push_style(*s)?,
2020-06-16 19:38:30 -04:00
Stmt::Comment(s) => vals.get_mut(0).unwrap().push_comment(s),
Stmt::Media { params, body, .. } => {
vals.push(Toplevel::Media { params, body })
}
Stmt::UnknownAtRule {
params, body, name, ..
} => vals.push(Toplevel::UnknownAtRule { params, body, name }),
Stmt::Return(..) => unreachable!(),
Stmt::AtRoot { body } => body
2020-04-06 13:13:03 -04:00
.into_iter()
2020-06-16 19:38:30 -04:00
.map(|r| Ok(vals.extend(self.parse_stmt(r)?)))
2020-04-06 13:13:03 -04:00
.collect::<SassResult<()>>()?,
2020-01-19 19:27:52 -05:00
};
}
2020-01-19 19:27:52 -05:00
vals
}
2020-06-16 19:38:30 -04:00
Stmt::Comment(s) => vec![Toplevel::MultilineComment(s)],
2020-04-12 21:47:32 -04:00
Stmt::Style(s) => vec![Toplevel::Style(s)],
2020-06-16 19:38:30 -04:00
Stmt::Media { params, body, .. } => vec![Toplevel::Media { params, body }],
Stmt::UnknownAtRule {
params, name, body, ..
} => vec![Toplevel::UnknownAtRule { params, name, body }],
2020-06-16 22:00:45 -04:00
Stmt::Return(..) => unreachable!("@return: {:?}", stmt),
Stmt::AtRoot { .. } => unreachable!("@at-root: {:?}", stmt),
2020-04-01 17:37:07 -04:00
})
}
2020-06-16 19:38:30 -04:00
fn parse_stylesheet(mut self, stmts: Vec<Stmt>) -> SassResult<Css> {
let mut is_first = true;
2020-06-16 19:38:30 -04:00
for stmt in stmts {
let v = self.parse_stmt(stmt)?;
// this is how we print newlines between unrelated styles
// it could probably be refactored
if !v.is_empty() {
2020-04-30 19:59:13 -04:00
if let Some(Toplevel::MultilineComment(..)) = v.get(0) {
2020-02-01 19:25:44 -05:00
} else if is_first {
is_first = false;
} else {
self.blocks.push(Toplevel::Newline);
}
2020-03-01 14:53:52 -05:00
self.blocks.extend(v);
}
}
2020-04-01 17:37:07 -04:00
Ok(self)
}
2020-04-24 22:57:39 -04:00
pub fn pretty_print(self, map: &CodeMap) -> SassResult<String> {
let mut string = Vec::new();
2020-04-24 22:57:39 -04:00
self._inner_pretty_print(&mut string, map, 0)?;
if string.iter().any(|s| !s.is_ascii()) {
return Ok(format!("@charset \"UTF-8\";\n{}", unsafe {
String::from_utf8_unchecked(string)
}));
2020-02-28 18:27:32 -05:00
}
Ok(unsafe { String::from_utf8_unchecked(string) })
}
2020-04-24 22:57:39 -04:00
fn _inner_pretty_print(
self,
buf: &mut Vec<u8>,
map: &CodeMap,
nesting: usize,
) -> SassResult<()> {
let mut has_written = false;
let padding = vec![' '; nesting * 2].iter().collect::<String>();
for block in self.blocks {
match block {
Toplevel::RuleSet(selector, styles) => {
if styles.is_empty() {
continue;
}
has_written = true;
writeln!(buf, "{}{} {{", padding, selector)?;
for style in styles {
2020-04-12 19:37:12 -04:00
writeln!(buf, "{} {}", padding, style.to_string()?)?;
}
writeln!(buf, "{}}}", padding)?;
}
Toplevel::MultilineComment(s) => {
has_written = true;
writeln!(buf, "{}/*{}*/", padding, s)?;
2020-01-26 15:27:38 -05:00
}
2020-06-16 19:38:30 -04:00
Toplevel::UnknownAtRule { params, name, body } => {
if params.is_empty() {
write!(buf, "{}@{}", padding, name)?;
} else {
write!(buf, "{}@{} {}", padding, name, params)?;
}
if body.is_empty() {
writeln!(buf, ";")?;
continue;
} else {
writeln!(buf, " {{")?;
}
Css::from_stmts(body)?._inner_pretty_print(buf, map, nesting + 1)?;
writeln!(buf, "{}}}", padding)?;
}
Toplevel::Media { params, body } => {
if body.is_empty() {
continue;
}
2020-06-16 19:38:30 -04:00
writeln!(buf, "{}@media {} {{", padding, params)?;
Css::from_stmts(body)?._inner_pretty_print(buf, map, nesting + 1)?;
writeln!(buf, "{}}}", padding)?;
2020-04-24 22:57:39 -04:00
}
2020-04-12 21:47:32 -04:00
Toplevel::Style(s) => {
writeln!(buf, "{}{}", padding, s.to_string()?)?;
}
2020-02-01 19:25:44 -05:00
Toplevel::Newline => {
if has_written {
writeln!(buf)?
}
}
}
}
Ok(())
}
2020-01-05 12:52:50 -05:00
}