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::*;
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 {
fn to_html(&self) -> String;
}
@ -50,7 +60,11 @@ impl ToHtml for Document {
.iter()
.fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()));
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 {
format!(
"<div class='documentImport' document-import=true>{}</div>",
@ -92,7 +106,7 @@ impl ToHtml for Paragraph {
let inner = self
.elements
.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)
}
}
@ -173,7 +187,7 @@ impl ToHtml for Quote {
let text = self
.text
.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() {
format!(
"<div><blockquote>{}</blockquote><span>- {}</span></div>",
@ -203,12 +217,12 @@ impl ToHtml for Image {
fn to_html(&self) -> String {
if let Some(description) = self.url.description.clone() {
format!(
"<div>\
<a href={0}>\
<img src='{0}' alt='{1}'/>\
</a>\
<label class='imageDescription'>{1}</label>
</div>",
"<div class='figure'>\
<a href={0}>\
<img src='{0}' alt='{1}'/>\
</a>\
<label class='imageDescription'>{1}</label>\
</div>",
self.url.url.clone(),
description
)

@ -6,6 +6,7 @@ use termion::style;
use std::path::PathBuf;
use structopt::StructOpt;
use termion::color::{Fg, Red};
#[derive(StructOpt, Debug)]
struct Opt {
@ -19,6 +20,24 @@ struct Opt {
fn main() {
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 mut parser = Parser::new_from_file(opt.input.to_str().unwrap().to_string()).unwrap();
let document = parser.parse();

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

Loading…
Cancel
Save