Browse Source

Started working on pages

index-subcmd
Vincent Prouillet 7 years ago
parent
commit
a48a4c9bdf
8 changed files with 225 additions and 7 deletions
  1. +98
    -0
      Cargo.lock
  2. +4
    -0
      Cargo.toml
  3. +9
    -0
      src/cmd/build.rs
  4. +2
    -0
      src/cmd/mod.rs
  5. +1
    -0
      src/config.rs
  6. +4
    -0
      src/errors.rs
  7. +28
    -7
      src/main.rs
  8. +79
    -0
      src/page.rs

+ 98
- 0
Cargo.lock View File

@@ -5,7 +5,19 @@ dependencies = [
"clap 2.19.1 (registry+https://github.com/rust-lang/crates.io-index)",
"clippy 0.0.103 (registry+https://github.com/rust-lang/crates.io-index)",
"error-chain 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"pulldown-cmark 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "aho-corasick"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
@@ -36,6 +48,11 @@ dependencies = [
"libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "bitflags"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "bitflags"
version = "0.7.0"
@@ -105,6 +122,11 @@ name = "gcc"
version = "0.3.39"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "getopts"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "kernel32-sys"
version = "0.2.2"
@@ -114,6 +136,11 @@ dependencies = [
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "lazy_static"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "libc"
version = "0.2.18"
@@ -124,16 +151,45 @@ name = "matches"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "memchr"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "nom"
version = "1.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "pulldown-cmark"
version = "0.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "quine-mc_cluskey"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "regex"
version = "0.1.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "regex-syntax"
version = "0.3.9"
@@ -177,6 +233,23 @@ dependencies = [
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "thread-id"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "thread_local"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "toml"
version = "0.1.30"
@@ -208,11 +281,25 @@ name = "unicode-width"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "utf8-ranges"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "vec_map"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "walkdir"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "winapi"
version = "0.2.8"
@@ -224,9 +311,11 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"

[metadata]
"checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66"
"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"
"checksum backtrace 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f551bc2ddd53aea015d453ef0b635af89444afa5ed2405dd0b2062ad5d600d80"
"checksum backtrace-sys 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3602e8d8c43336088a8505fa55cae2b3884a9be29440863a11528a42f46f6bb7"
"checksum bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4f67931368edf3a9a51d29886d245f1c3db2f1ef0dcc9e35ff70341b78c10d23"
"checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d"
"checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c"
"checksum clap 2.19.1 (registry+https://github.com/rust-lang/crates.io-index)" = "956cee0b2427dd9e71129a509d1ef17a7f5df9f8253924074d7a5d79bc61851e"
@@ -235,11 +324,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum dbghelp-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97590ba53bcb8ac28279161ca943a924d1fd4a8fb3fa63302591647c4fc5b850"
"checksum error-chain 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1cd681735364a04cd5d69f01a4f6768e70473941f8d86d8c224faf6955a75799"
"checksum gcc 0.3.39 (registry+https://github.com/rust-lang/crates.io-index)" = "771e4a97ff6f237cf0f7d5f5102f6e28bb9743814b6198d684da5c58b76c11e0"
"checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6abe0ee2e758cd6bc8a2cd56726359007748fbf4128da998b65d0b70f881e19b"
"checksum libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "a51822fc847e7a8101514d1d44e354ba2ffa7d4c194dcab48870740e327cac70"
"checksum matches 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "efd7622e3022e1a6eaa602c4cea8912254e5582c9c692e9167714182244801b1"
"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
"checksum nom 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a5b8c256fd9471521bcb84c3cdba98921497f1a331cbc15b8030fc63b82050ce"
"checksum pulldown-cmark 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1058d7bb927ca067656537eec4e02c2b4b70eaaa129664c5b90c111e20326f41"
"checksum quine-mc_cluskey 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "07589615d719a60c8dd8a4622e7946465dfef20d1a428f969e3443e7386d5f45"
"checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f"
"checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957"
"checksum rustc-demangle 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1430d286cadb237c17c885e25447c982c97113926bb579f4379c0eca8d9586dc"
"checksum rustc-serialize 0.3.21 (registry+https://github.com/rust-lang/crates.io-index)" = "bff9fc1c79f2dec76b253273d07682e94a978bd8f132ded071188122b2af9818"
@@ -247,11 +341,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum serde 0.8.19 (registry+https://github.com/rust-lang/crates.io-index)" = "58a19c0871c298847e6b68318484685cd51fa5478c0c905095647540031356e5"
"checksum strsim 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "67f84c44fbb2f91db7fef94554e6b2ac05909c9c0b0bc23bb98d3a1aebfe7f7c"
"checksum term_size 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3f7f5f3f71b0040cecc71af239414c23fd3c73570f5ff54cf50e03cef637f2a0"
"checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03"
"checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
"checksum toml 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)" = "0590d72182e50e879c4da3b11c6488dae18fccb1ae0c7a3eda18e16795844796"
"checksum toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "736b60249cb25337bc196faa43ee12c705e426f3d55c214d73a4e7be06f92cb4"
"checksum unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "26643a2f83bac55f1976fb716c10234485f9202dcd65cfbdf9da49867b271172"
"checksum unicode-segmentation 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c3bc443ded17b11305ffffe6b37e2076f328a5a8cb6aa877b1b98f77699e98b5"
"checksum unicode-width 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2d6722facc10989f63ee0e20a83cd4e1714a9ae11529403ac7e0afd069abc39e"
"checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f"
"checksum vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cac5efe5cb0fa14ec2f84f83c701c562ee63f6dcc680861b21d65c682adfb05f"
"checksum walkdir 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "dd7c16466ecc507c7cb5988db03e6eab4aaeab89a5c37a29251fcfd3ac9b7afe"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"

+ 4
- 0
Cargo.toml View File

@@ -12,6 +12,10 @@ keywords = ["static", "site", "generator", "blog"]
[dependencies]
error-chain = "0.7"
clap = "2.19"
walkdir = "1"
pulldown-cmark = "0"
regex = "0.1"
lazy_static = "0.2"
clippy = {version = "~0.0.103", optional = true}

[dependencies.toml]


+ 9
- 0
src/cmd/build.rs View File

@@ -0,0 +1,9 @@

use config:: Config;
use errors::{Result, ErrorKind};


pub fn build(config: Config) -> Result<()> {

Ok(())
}

+ 2
- 0
src/cmd/mod.rs View File

@@ -1,3 +1,5 @@
mod new;
mod build;

pub use self::new::create_new_project;
pub use self::build::build;

+ 1
- 0
src/config.rs View File

@@ -41,6 +41,7 @@ impl Config {

return Ok(config);
} else {
// TODO: handle error in parsing TOML
println!("parse errors: {:?}", parser.errors);
}



+ 4
- 0
src/errors.rs View File

@@ -5,6 +5,10 @@ error_chain! {
}

errors {
InvalidFrontMatter(name: String) {
description("frontmatter is invalid")
display("Front Matter of file '{}' is missing or is invalid", name)
}
InvalidConfig {
description("invalid config")
display("The config.toml is invalid or is using the wrong type for an argument")


+ 28
- 7
src/main.rs View File

@@ -3,15 +3,22 @@

#[macro_use] extern crate clap;
#[macro_use] extern crate error_chain;
#[macro_use] extern crate lazy_static;
extern crate toml;
extern crate walkdir;
extern crate pulldown_cmark;
extern crate regex;

mod config;
mod errors;
mod cmd;
mod page;

use config::Config;


// Get and parse the config.
// If it doesn't succeed, exit
fn get_config() -> Config {
match Config::from_file("config.toml") {
Ok(c) => c,
@@ -33,18 +40,32 @@ fn main() {
(about: "Create a new Gutenberg project")
(@arg name: +required "Name of the project. Will create a directory with that name in the current directory")
)
(@subcommand build =>
(about: "Builds the site")
)
).get_matches();

match matches.subcommand() {
("new", Some(matches)) => {
match cmd::create_new_project(matches.value_of("name").unwrap()) {
Ok(()) => {
println!("Project created");
},
Err(e) => {
println!("Error: {}", e);
::std::process::exit(1);
},
Ok(()) => {
println!("Project created");
},
Err(e) => {
println!("Error: {}", e);
::std::process::exit(1);
},
};
},
("build", None) => {
match cmd::build(get_config()) {
Ok(()) => {
println!("Project built");
},
Err(e) => {
println!("Error: {}", e);
::std::process::exit(1);
},
};
},
_ => unreachable!(),


+ 79
- 0
src/page.rs View File

@@ -0,0 +1,79 @@
/// A page, can be a blog post or a basic page
use std::collections::HashMap;

use pulldown_cmark as cmark;
use regex::Regex;
use toml::Parser;

use errors::{Result, ErrorKind};


lazy_static! {
static ref DELIM_RE: Regex = Regex::new(r"\+\+\+\s*\r?\n").unwrap();
}


#[derive(Debug, PartialEq)]
struct Page {
// <title> of the page
title: String,
// the url the page appears at (slug form)
url: String,
// the actual content of the page
content: String,
// tags, not to be confused with categories
tags: Vec<String>,
// any extra parameter present in the front matter
// it will be passed to the template context
extra: HashMap<String, String>,

// only one category allowed
category: Option<String>,
// optional date if we want to order pages (ie block)
date: Option<bool>,
// optional layout, if we want to specify which html to render for that page
layout: Option<String>,
// description that appears when linked, e.g. on twitter
description: Option<String>,
}


impl Page {
// Parse a page given the content of the .md file
// Files without front matter or with invalid front matter are considered
// erroneous
pub fn from_str(filename: &str, content: &str) -> Result<()> {
// 1. separate front matter from content
if !DELIM_RE.is_match(content) {
return Err(ErrorKind::InvalidFrontMatter(filename.to_string()).into());
}

// 2. extract the front matter and the content
let splits: Vec<&str> = DELIM_RE.splitn(content, 2).collect();
let front_matter = splits[0];
let content = splits[1];

// 2. parse front matter
let mut parser = Parser::new(&front_matter);
if let Some(value) = parser.parse() {

} else {
// TODO: handle error in parsing TOML
println!("parse errors: {:?}", parser.errors);
}

Ok(())
}
}


#[cfg(test)]
mod tests {
use super::*;


#[test]
fn test_can_extract_front_matter() {

}
}

Loading…
Cancel
Save