Skip to content

Commit

Permalink
support for embedded css with :css
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Saunders committed Dec 11, 2017
1 parent 2f92b7b commit a1b70fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
7 changes: 6 additions & 1 deletion orko/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ pub fn print_summary(path:&Path, result: io::Result<Vec<ProcessedFile>>) {
for (idx, c) in parse_error.context.iter().enumerate() {
let line_number = parse_error.line_number + 2 + idx - parse_error.context.len();
let padded_line_number = format!("{}:", line_number).pad_to_width(5);
let line = format!("{} {}", padded_line_number, c);

let marker = if parse_error.line_number == line_number {
">"
} else { " " };

let line = format!("{}{} {}", marker, padded_line_number, c);
println!("{}", line);
}

Expand Down
24 changes: 20 additions & 4 deletions templar/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ named!(javascript_line<&str, LineContent>,
)
);

named!(css_line<&str, LineContent>,
do_parse!(
tag!(":css") >>
rr : rest >>
( LineContent::StyleSheet )
)
);

named!(tag_element_line<&str, LineContent>,
do_parse!(
tag: identifier >>
Expand Down Expand Up @@ -159,7 +167,7 @@ named!(class_id_only_line<&str, LineContent>,
);

named!(line_p<&str, LineContent>,
alt_complete!(doctype_line | comment_line | javascript_line | tag_element_line | class_id_only_line | directive_line | text_line)
alt_complete!(doctype_line | comment_line | javascript_line | css_line | tag_element_line | class_id_only_line | directive_line | text_line)
);

#[derive(Debug)]
Expand All @@ -184,6 +192,7 @@ enum LineContent {
Element(HtmlElement),
Directive(String),
Text(String),
StyleSheet,
}

fn indentation(str: &str) -> Option<usize> {
Expand Down Expand Up @@ -275,14 +284,17 @@ pub fn parse(content:&str) -> ParseResult {
ter.iter().cloned().collect()
};

// for each line in the current file,
for (line_idx, line) in lines.iter().enumerate() {
// println!("line {}\n{}", line_idx, line);
// indentation and slicing first
// println!("-> {}", line);
if let Some(indent) = indentation(line) {
let (_, rest) = line.split_at(indent);

// println!("!indent is {:?}", indent);

// while the next element on the stack is indented more than the current line,
while contains(out_stack.last(), |&&(_, n)| n >= indent ) {
let (node, _) = out_stack.pop().expect("the top element");

Expand Down Expand Up @@ -310,11 +322,10 @@ pub fn parse(content:&str) -> ParseResult {

match line_content_result {
IResult::Done(_, line_content) => {
// println!("Done-> {}", format!("{:?}",line_content).green());
println!("Done-> {}", format!("{:?}",line_content));

match (mode, line_content) {
(ParseMode::InlineJavascript, LineContent::Text(string)) => {
// println!("!added some javascript content");
let &mut (ref mut next_down, _) = out_stack.last_mut().expect("a javascript node");
let node = Node::RawText(string);
if !next_down.append_child(node.clone()) {
Expand All @@ -335,12 +346,17 @@ pub fn parse(content:&str) -> ParseResult {
// ignore
},
LineContent::Javascript => {
// println!("!javasript element, startin javascript mode");
let mut ele = element("script", vec![("type", "text/javascript")]);
ele.children.push(Node::RawText("\n".into()));
mode = ParseMode::InlineJavascript;
out_stack.push((Node::Element(ele), indent));
},
LineContent::StyleSheet => {
let mut ele = element("style", vec![]);
ele.children.push(Node::RawText("\n".into()));
mode = ParseMode::InlineJavascript;
out_stack.push((Node::Element(ele), indent));
},
LineContent::Doctype(string) => {
if !out_stack.is_empty() {
return Err(ParseError {
Expand Down

0 comments on commit a1b70fa

Please sign in to comment.