Add centered text

pull/1/head
trivernis 5 years ago
parent e80ecea6cf
commit cfc02b8da7

2
Cargo.lock generated

@ -358,7 +358,7 @@ dependencies = [
[[package]]
name = "snekdown"
version = "0.6.0"
version = "0.7.0"
dependencies = [
"chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",

@ -1,6 +1,6 @@
[package]
name = "snekdown"
version = "0.6.0"
version = "0.7.0"
authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018"
license-file = "LICENSE"

@ -82,6 +82,7 @@ Header with rows
### Placeholders
Placeholders can be used to insert special elements in a specific place.
Placeholders are always case insensitive.
```md
Insert the table of contents
@ -139,3 +140,10 @@ Set the source of a quote
Set options for placeholders
[[toc]][ordered]
```
### Centered Text
```
|| These two lines
|| are centered
```

@ -117,3 +117,7 @@ blockquote {
color: #444;
font-style: italic;
}
.centered {
text-align: center;
}

@ -36,6 +36,7 @@ impl ToHtml for Line {
Line::Text(text) => text.to_html(),
Line::Ruler(ruler) => ruler.to_html(),
Line::Anchor(anchor) => anchor.to_html(),
Line::Centered(centered) => centered.to_html(),
}
}
}
@ -147,7 +148,7 @@ impl ToHtml for Paragraph {
.elements
.iter()
.fold("".to_string(), |a, b| combine_with_lb!(a, b));
format!("<p>{}</p>", inner)
format!("<div class='paragraph'>{}</div>", inner)
}
}
@ -414,3 +415,9 @@ impl ToHtml for InlineMetadata {
}
}
}
impl ToHtml for Centered {
fn to_html(&self) -> String {
format!("<div class='centered'>{}</div>", self.line.to_html())
}
}

@ -57,6 +57,7 @@ pub enum Line {
Text(TextLine),
Ruler(Ruler),
Anchor(Anchor),
Centered(Centered),
}
#[derive(Clone, Debug)]
@ -220,6 +221,11 @@ pub struct Anchor {
pub(crate) reference: String,
}
#[derive(Clone, Debug)]
pub struct Centered {
pub(crate) line: TextLine,
}
// implementations
impl Document {

@ -650,7 +650,6 @@ impl Parser {
self.seek_whitespace();
while let Ok(row) = self.parse_row() {
table.add_row(row);
self.seek_whitespace();
}
Ok(table)
@ -662,23 +661,39 @@ impl Parser {
self.seek_inline_whitespace();
self.assert_special(&PIPE, start_index)?;
self.skip_char();
if self.check_special(&PIPE) {
return Err(self.revert_with_error(start_index));
}
self.inline_break_at.push(PIPE);
self.seek_inline_whitespace();
let mut row = Row::new();
while let Ok(element) = self.parse_line() {
row.add_cell(Cell { text: element });
if self.check_special(&PIPE) {
if self.next_char() == None {
loop {
let mut element = TextLine::new();
while let Ok(inline) = self.parse_inline() {
element.subtext.push(inline);
if self.check_linebreak() || self.check_special(&PIPE) {
break;
}
}
row.add_cell(Cell {
text: Line::Text(element),
});
if self.check_special(&PIPE) {
self.skip_char();
}
if self.check_linebreak() {
break;
}
self.seek_inline_whitespace();
}
self.inline_break_at.clear();
if self.check_special(&PIPE) {
self.skip_char();
self.skip_char();
} else {
self.skip_char();
}
if row.cells.len() > 0 {
Ok(row)
@ -693,12 +708,25 @@ impl Parser {
Err(ParseError::new(self.index))
} else {
if let Ok(ruler) = self.parse_ruler() {
return Ok(Line::Ruler(ruler));
Ok(Line::Ruler(ruler))
} else if let Ok(centered) = self.parse_centered() {
Ok(Line::Centered(centered))
} else if let Ok(text) = self.parse_text_line() {
return Ok(Line::Text(text));
Ok(Line::Text(text))
} else {
Err(ParseError::new(self.index))
}
}
return Err(ParseError::new(self.index));
}
/// parses centered text
fn parse_centered(&mut self) -> Result<Centered, ParseError> {
let start_index = self.index;
self.assert_special_sequence(&SQ_CENTERED_START, start_index)?;
self.skip_char();
let line = self.parse_text_line()?;
Ok(Centered { line })
}
/// parses a placeholder element

@ -52,15 +52,16 @@ pub(crate) const BOLD: [char; 2] = [ASTERISK, ASTERISK];
pub(crate) const QUOTES: [char; 2] = [SINGLE_QUOTE, DOUBLE_QUOTE];
pub(crate) const BLOCK_SPECIAL_CHARS: [&[char]; 8] = [
pub(crate) const BLOCK_SPECIAL_CHARS: [&[char]; 9] = [
&[HASH],
&[HASH, META_OPEN],
&[MINUS, SPACE],
&[BACKTICK, BACKTICK, BACKTICK],
&SQ_CODE_BLOCK,
&[PIPE],
&[QUOTE_START],
&[META_OPEN],
&[IMPORT_START, IMPORT_OPEN],
&SQ_CENTERED_START,
];
pub(crate) const INLINE_SPECIAL_CHARS: [char; 8] = [
@ -77,6 +78,7 @@ pub(crate) const SQ_CODE_BLOCK: [char; 3] = [BACKTICK, BACKTICK, BACKTICK];
pub(crate) const SQ_RULER: [char; 5] = [MINUS, SPACE, MINUS, SPACE, MINUS];
pub(crate) const SQ_PHOLDER_START: [char; 2] = [PHOLDER_OPEN, PHOLDER_OPEN];
pub(crate) const SQ_PHOLDER_STOP: [char; 2] = [PHOLDER_CLOSE, PHOLDER_CLOSE];
pub(crate) const SQ_CENTERED_START: [char; 2] = [PIPE, PIPE];
// expressions

Loading…
Cancel
Save