summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--riki.md26
-rw-r--r--src/directive/table.rs32
2 files changed, 52 insertions, 6 deletions
diff --git a/riki.md b/riki.md
index de4e08a..b7551e1 100644
--- a/riki.md
+++ b/riki.md
@@ -569,6 +569,32 @@ then file output/a/index.html contains "<a href="https://example.com/foo/123">fo
[[!shortcut name="foo" url="https://example.com/foo/%s" desc="foo!%s"]]
~~~
+### `table`
+
+_Requirement: the `table` directive creates a simple table._
+
+~~~scenario
+given an installed riki
+given file site/index.mdwn from table
+when I run riki build site output
+when I run cat output/index.html
+then file output/index.html contains "<table>"
+then file output/index.html contains "<th><td>Greeting</td>"
+then file output/index.html contains "<td>Greetee</td>"
+then file output/index.html contains "<tr><td>hello</td>"
+then file output/index.html contains "<td>world</td>"
+then file output/index.html contains "<tr><td>goodbye</td>"
+then file output/index.html contains "<td>cruel world</td>"
+~~~
+
+~~~{#table .file .markdown}
+[[!table data="""
+Greeting | Greetee
+hello | world
+goodbye | cruel world
+"""]]
+~~~
+
## Source file tree
### Listing source files
diff --git a/src/directive/table.rs b/src/directive/table.rs
index 1d0016f..7610a6b 100644
--- a/src/directive/table.rs
+++ b/src/directive/table.rs
@@ -2,26 +2,46 @@ use crate::error::SiteError;
use crate::page::PageMeta;
use crate::site::Site;
use crate::wikitext::ParsedDirective;
+use log::debug;
#[derive(Debug, Eq, PartialEq)]
-pub struct Table {}
+pub struct Table {
+ data: String,
+}
impl Table {
pub const REQUIRED: &'static [&'static str] = &["data"];
pub const ALLOWED: &'static [&'static str] = &[];
pub const ALLOW_ANY_UNNAMED: bool = true;
- pub fn new() -> Self {
- Self {}
+ pub fn new(data: String) -> Self {
+ Self { data }
}
pub fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> {
- Ok("FIXME:table".into())
+ let mut table = String::new();
+ let mut lines = self.data.trim().lines();
+ if let Some(first) = lines.next() {
+ let columns = first.split('|').count();
+ debug!("table first: {:?}", first);
+ debug!("table column count: {}", columns);
+ table.push_str(&format!("|{}|\n", first));
+ for _ in 0..columns {
+ table.push_str("|-");
+ }
+ table.push_str("|\n");
+ }
+ for line in lines {
+ table.push_str(&format!("|{}|\n", line));
+ }
+ debug!("table data: {}", self.data);
+ debug!("table: {}", table);
+ Ok(table)
}
}
impl From<&ParsedDirective> for Table {
- fn from(_: &ParsedDirective) -> Self {
- Table::new()
+ fn from(p: &ParsedDirective) -> Self {
+ Table::new(p.args().get("data").unwrap().to_string())
}
}