Browse Source

create `zola index` command and route existing search indexing code to it

this is the first step in building a new `zola index` command that allows building search indexes as a stand-alone task.

the new Subcommand in src/cli.rs outlines the planned api.

the biggest thing it will be providing compared to current code is optional indexing with tantivy.

this commit doesn't add tantivy or any new functionality other than allowing the existing search indexing to be performed as a subcommand. (passing --index-type tantivy will trigger unimplemented!() panic)
index-subcmd
Jonathan Strong 4 years ago
parent
commit
cffb69c5be
4 changed files with 93 additions and 1 deletions
  1. +24
    -1
      src/cli.rs
  2. +46
    -0
      src/cmd/index.rs
  3. +2
    -0
      src/cmd/mod.rs
  4. +21
    -0
      src/main.rs

+ 24
- 1
src/cli.rs View File

@@ -95,6 +95,29 @@ pub fn build_cli() -> App<'static, 'static> {
.long("drafts")
.takes_value(false)
.help("Include drafts when loading the site"),
])
]),
SubCommand::with_name("index")
.about("Create a search index as a stand-alone task, and with additional options")
.args(&[
Arg::with_name("index_type")
.long("index-type")
.short("t")
.takes_value(true)
.possible_values(&["elasticlunr", "tantivy"])
.required(true)
.help("what kind of search index to build"),
Arg::with_name("output_dir")
.short("o")
.long("output-dir")
.default_value("public")
.takes_value(true)
.help("Outputs the generated search index files into the provided dir. \
Note: Tantivy indexing produces a directory instead of a file, \
which will be located at output-dir/tantivy-index"),
Arg::with_name("drafts")
.long("drafts")
.takes_value(false)
.help("Include drafts when loading the site"),
]),
])
}

+ 46
- 0
src/cmd/index.rs View File

@@ -0,0 +1,46 @@
use std::path::Path;

use errors::Result;
use site::Site;

//use crate::console;

pub fn index(
root_dir: &Path,
config_file: &str,
base_url: Option<&str>,
output_dir: &str,
include_drafts: bool,
index_type: &str,
) -> Result<()> {
let mut site = Site::new(root_dir, config_file)?;
site.set_output_path(output_dir);

// TODO: is base_url even necessary for this command?
if let Some(b) = base_url {
site.set_base_url(b.to_string());
}
if include_drafts {
site.include_drafts();
}
site.load()?;

// TODO: could skipping the theme and/or sass prep end up
// somehow impacting the search indexing? doesn't seem like
// it could, but maybe

match index_type {
"elasticlunr" => {
site.build_search_index()?;
}

"tantivy" => {
unimplemented!()
}

_ => unreachable!()
}

Ok(())
}

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

@@ -2,8 +2,10 @@ mod build;
mod check;
mod init;
mod serve;
mod index;

pub use self::build::build;
pub use self::check::check;
pub use self::init::create_new_project;
pub use self::serve::serve;
pub use self::index::index;

+ 21
- 0
src/main.rs View File

@@ -111,6 +111,27 @@ fn main() {
}
};
}
("index", Some(matches)) => {
console::info("Building search index...");
let start = Instant::now();
let output_dir = matches.value_of("output_dir").unwrap();
let index_type = matches.value_of("index_type").unwrap();
match cmd::index(
&root_dir,
config_file,
matches.value_of("base_url"),
output_dir,
matches.is_present("drafts"),
index_type,
) {
Ok(()) => console::report_elapsed_time(start),
Err(e) => {
console::unravel_errors("Failed to build search index", &e);
::std::process::exit(1);
}
};
}

_ => unreachable!(),
}
}

Loading…
Cancel
Save