Improve html rendering

- Add stylesheet
- Remove unnecessary linebreaks
pull/1/head
trivernis 5 years ago
parent e9ec656740
commit 7d0d04454e

@ -0,0 +1,37 @@
body {
background-color: #DDD;
}
.content {
font-family: "Noto Sans", SansSerif, sans-serif;
width: 100vh;
max-width: 100%;
padding: 2rem;
margin: auto;
background-color: #FFF;
box-shadow: 1em 1em 1em gray;
}
img {
max-width: 100%;
max-height: 100vh;
height: auto;
}
blockquote {
border-left: 0.3em solid gray;
border-radius: 0.2em;
padding-left: 0.5em;
}
.figure {
width: 100%;
display: block;
text-align: center;
}
.figure .imageDescription {
display: block;
color: #444;
font-style: italic;
}

@ -1,5 +1,15 @@
use crate::elements::*; use crate::elements::*;
macro_rules! combine_with_lb {
($a:expr, $b:expr) => {
if $a.len() > 0 {
format!("{}<br>{}", $a, $b.to_html())
} else {
$b.to_html()
}
};
}
pub trait ToHtml { pub trait ToHtml {
fn to_html(&self) -> String; fn to_html(&self) -> String;
} }
@ -50,7 +60,11 @@ impl ToHtml for Document {
.iter() .iter()
.fold("".to_string(), |a, b| format!("{}{}", a, b.to_html())); .fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()));
if self.is_root { if self.is_root {
format!("<html><body>{}</body></html>", inner) let style = std::include_str!("assets/style.css");
format!(
"<html><head><style>{}</style></head><body><div class='content'>{}</div></body></html>",
style, inner
)
} else { } else {
format!( format!(
"<div class='documentImport' document-import=true>{}</div>", "<div class='documentImport' document-import=true>{}</div>",
@ -92,7 +106,7 @@ impl ToHtml for Paragraph {
let inner = self let inner = self
.elements .elements
.iter() .iter()
.fold("".to_string(), |a, b| format!("{}<br>{}", a, b.to_html())); .fold("".to_string(), |a, b| combine_with_lb!(a, b));
format!("<p>{}</p>", inner) format!("<p>{}</p>", inner)
} }
} }
@ -173,7 +187,7 @@ impl ToHtml for Quote {
let text = self let text = self
.text .text
.iter() .iter()
.fold("".to_string(), |a, b| format!("{}<br>{}", a, b.to_html())); .fold("".to_string(), |a, b| combine_with_lb!(a, b));
if let Some(meta) = self.metadata.clone() { if let Some(meta) = self.metadata.clone() {
format!( format!(
"<div><blockquote>{}</blockquote><span>- {}</span></div>", "<div><blockquote>{}</blockquote><span>- {}</span></div>",
@ -203,12 +217,12 @@ impl ToHtml for Image {
fn to_html(&self) -> String { fn to_html(&self) -> String {
if let Some(description) = self.url.description.clone() { if let Some(description) = self.url.description.clone() {
format!( format!(
"<div>\ "<div class='figure'>\
<a href={0}>\ <a href={0}>\
<img src='{0}' alt='{1}'/>\ <img src='{0}' alt='{1}'/>\
</a>\ </a>\
<label class='imageDescription'>{1}</label> <label class='imageDescription'>{1}</label>\
</div>", </div>",
self.url.url.clone(), self.url.url.clone(),
description description
) )

@ -6,6 +6,7 @@ use termion::style;
use std::path::PathBuf; use std::path::PathBuf;
use structopt::StructOpt; use structopt::StructOpt;
use termion::color::{Fg, Red};
#[derive(StructOpt, Debug)] #[derive(StructOpt, Debug)]
struct Opt { struct Opt {
@ -19,6 +20,24 @@ struct Opt {
fn main() { fn main() {
let opt: Opt = Opt::from_args(); let opt: Opt = Opt::from_args();
if !opt.input.exists() {
println!(
"{}The input file {} could not be found{}",
Fg(Red),
opt.input.to_str().unwrap(),
style::Reset
);
return;
}
if !opt.output.exists() {
println!(
"{} The output file {} could not be found{}",
Fg(Red),
opt.output.to_str().unwrap(),
style::Reset
);
return;
}
let start = Instant::now(); let start = Instant::now();
let mut parser = Parser::new_from_file(opt.input.to_str().unwrap().to_string()).unwrap(); let mut parser = Parser::new_from_file(opt.input.to_str().unwrap().to_string()).unwrap();
let document = parser.parse(); let document = parser.parse();

@ -352,6 +352,7 @@ impl Parser {
header.size = size; header.size = size;
self.section_nesting = size; self.section_nesting = size;
let mut section = Section::new(header); let mut section = Section::new(header);
self.seek_whitespace();
while let Ok(block) = self.parse_block() { while let Ok(block) = self.parse_block() {
section.add_element(block); section.add_element(block);
@ -417,7 +418,9 @@ impl Parser {
&& self.check_seek_inline_whitespace() && self.check_seek_inline_whitespace()
{ {
if let Ok(text) = self.parse_text() { if let Ok(text) = self.parse_text() {
quote.add_text(text); if text.subtext.len() > 0 {
quote.add_text(text);
}
} else { } else {
break; break;
} }

Loading…
Cancel
Save