Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Add [[mark]] to highlight text #112

Merged
merged 3 commits into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/parse/rule/impls/block/blocks/mark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* parse/rule/impls/block/blocks/mark.rs
*
* ftml - Library to parse Wikidot text
* Copyright (C) 2019-2021 Ammon Smith
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

use super::prelude::*;

pub const BLOCK_MARK: BlockRule = BlockRule {
name: "block-mark",
accepts_names: &["mark", "highlight"],
accepts_special: false,
newline_separator: false,
parse_fn,
};

fn parse_fn<'r, 't>(
log: &slog::Logger,
parser: &mut Parser<'r, 't>,
name: &'t str,
special: bool,
in_head: bool,
) -> ParseResult<'r, 't, Element<'t>> {
debug!(
log,
"Parsing highlight block";
"in-head" => in_head,
"name" => name,
);

assert_eq!(special, false, "Mark doesn't allow special variant");
assert_block_name(&BLOCK_MARK, name);

let mut arguments = parser.get_head_map(&BLOCK_MARK, in_head)?;

// Get styling arguments
let id = arguments.get("id");
let class = arguments.get("class");
let style = arguments.get("style");

// Get body content, without paragraphs
let (elements, exceptions) = parser.get_body_elements(&BLOCK_MARK, false)?.into();

// Build and return element
let element = Element::Mark {
elements,
id,
class,
style,
};

ok!(element, exceptions)
}
2 changes: 2 additions & 0 deletions src/parse/rule/impls/block/blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ mod del;
mod div;
mod ins;
mod lines;
mod mark;
mod module;
mod span;

Expand All @@ -71,5 +72,6 @@ pub use self::del::BLOCK_DEL;
pub use self::div::BLOCK_DIV;
pub use self::ins::BLOCK_INS;
pub use self::lines::BLOCK_LINES;
pub use self::mark::BLOCK_MARK;
pub use self::module::BLOCK_MODULE;
pub use self::span::BLOCK_SPAN;
3 changes: 2 additions & 1 deletion src/parse/rule/impls/block/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ use super::{blocks::*, BlockRule};
use std::collections::HashMap;
use unicase::UniCase;

pub const BLOCK_RULES: [BlockRule; 9] = [
pub const BLOCK_RULES: [BlockRule; 10] = [
BLOCK_CODE,
BLOCK_COLLAPSIBLE,
BLOCK_CSS,
BLOCK_DEL,
BLOCK_DIV,
BLOCK_INS,
BLOCK_LINES,
BLOCK_MARK,
BLOCK_MODULE,
BLOCK_SPAN,
];
Expand Down
41 changes: 41 additions & 0 deletions test/mark-alias.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"input": "Apple [[highlight]]Banana[[/highlight]]",
"tree": {
"elements": [
{
"element": "container",
"data": {
"type": "paragraph",
"elements": [
{
"element": "text",
"data": "Apple"
},
{
"element": "text",
"data": " "
},
{
"element": "mark",
"data": {
"id": null,
"class": null,
"style": null,
"elements": [
{
"element": "text",
"data": "Banana"
}
]
}
}
]
}
}
],
"styles": [
]
},
"warnings": [
]
}
46 changes: 46 additions & 0 deletions test/mark-newlines.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"input": "[[mark]]\nApple\nBanana\n[[/mark]]",
"tree": {
"elements": [
{
"element": "container",
"data": {
"type": "paragraph",
"elements": [
{
"element": "mark",
"data": {
"id": null,
"class": null,
"style": null,
"elements": [
{
"element": "line-break"
},
{
"element": "text",
"data": "Apple"
},
{
"element": "line-break"
},
{
"element": "text",
"data": "Banana"
},
{
"element": "line-break"
}
]
}
}
]
}
}
],
"styles": [
]
},
"warnings": [
]
}
33 changes: 33 additions & 0 deletions test/mark-style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"input": "[[mark id=\"banana\" class=\"fruit\" style=\"color: yellow;\"]]Banana[[/mark]]",
"tree": {
"elements": [
{
"element": "container",
"data": {
"type": "paragraph",
"elements": [
{
"element": "mark",
"data": {
"id": "banana",
"class": "fruit",
"style": "color: yellow;",
"elements": [
{
"element": "text",
"data": "Banana"
}
]
}
}
]
}
}
],
"styles": [
]
},
"warnings": [
]
}
33 changes: 33 additions & 0 deletions test/mark-uppercase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"input": "[[MARk ID = \"apple\" clASS =\"fruit\" stylE=\"color: red;\" ]]Apple[[/ MARK ]]",
"tree": {
"elements": [
{
"element": "container",
"data": {
"type": "paragraph",
"elements": [
{
"element": "mark",
"data": {
"id": "apple",
"class": "fruit",
"style": "color: red;",
"elements": [
{
"element": "text",
"data": "Apple"
}
]
}
}
]
}
}
],
"styles": [
]
},
"warnings": [
]
}
41 changes: 41 additions & 0 deletions test/mark.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"input": "Apple [[mark]]Banana[[/mark]]",
"tree": {
"elements": [
{
"element": "container",
"data": {
"type": "paragraph",
"elements": [
{
"element": "text",
"data": "Apple"
},
{
"element": "text",
"data": " "
},
{
"element": "mark",
"data": {
"id": null,
"class": null,
"style": null,
"elements": [
{
"element": "text",
"data": "Banana"
}
]
}
}
]
}
}
],
"styles": [
]
},
"warnings": [
]
}