Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic search engine #22

Merged
merged 5 commits into from
Feb 2, 2022
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
547 changes: 532 additions & 15 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ rocket = "0.5.0-rc.1"
serde = { version = "1.0", features = ["derive"] }
strum = "0.23"
strum_macros = "0.23"
tantivy = "0.16.1"
thiserror = "1.0"
toml = "0.5.8"
10 changes: 10 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,14 @@ pub enum MyError {
#[from]
source: std::str::Utf8Error,
},
#[error("Search indexer failed in some way")]
SearchIndex {
#[from]
source: tantivy::TantivyError,
},
#[error("Failed to parse search query")]
SearchQueryParsing {
#[from]
source: tantivy::query::QueryParserError,
},
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static WIKI: OnceCell<Wiki> = OnceCell::new();
fn create_wiki() -> Result<Wiki, MyError> {
let settings = parse_settings_from_args()?;
let repo = create_file_system_repository(settings.git_repo().clone())?;
Ok(Wiki::new(settings, Box::new(repo)))
Wiki::new(settings, Box::new(repo))
}

#[rocket::async_trait]
Expand Down
44 changes: 43 additions & 1 deletion src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl MarkupLanguage {
fn file_extensions(&self) -> &[&str] {
match self {
MarkupLanguage::Markdown => {
static MARKDOWN_EXTENSIONS: &[&'static str] = &["md"];
static MARKDOWN_EXTENSIONS: &[&str] = &["md"];
MARKDOWN_EXTENSIONS
}
}
Expand All @@ -98,6 +98,26 @@ impl MarkupLanguage {
}
}
}

fn raw(
&self,
file_stem: &str,
file_contents: &str,
settings: &Settings,
) -> Result<Page, MyError> {
match self {
MarkupLanguage::Markdown => {
let markdown_page = MarkdownPage::new(settings, file_stem, file_contents);

let title = markdown_page.title().to_owned();

Ok(Page {
title,
body: file_contents.to_owned(),
})
}
}
}
}

fn get_language_for_file_extension(file_extension: &str) -> Option<MarkupLanguage> {
Expand All @@ -111,6 +131,7 @@ fn get_language_for_file_extension(file_extension: &str) -> Option<MarkupLanguag
None
}

/// Gets content of page, rendered as HTML.
pub fn get_page(
file_stem: &str,
file_extension: &str,
Expand All @@ -127,6 +148,27 @@ pub fn get_page(
}
}

/// Gets raw contents of page, after processing metadata
pub fn get_raw_page(
file_stem: &str,
file_extension: &str,
bytes: &[u8],
settings: &Settings,
) -> Result<Option<Page>, MyError> {
match get_language_for_file_extension(file_extension) {
None => Ok(None),
Some(lang) => Ok(Some(lang.raw(
file_stem,
std::str::from_utf8(bytes)?,
settings,
)?)),
}
}

pub fn is_page(file_extension: &str) -> bool {
get_language_for_file_extension(file_extension).is_some()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
23 changes: 23 additions & 0 deletions src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
io::{Read, Write},
ops::{Deref, DerefMut},
path::PathBuf,
};

Expand All @@ -19,6 +20,28 @@ pub trait Repository: std::fmt::Debug {
fn enumerate_files(&self, directory: &[&str]) -> Result<Vec<RepositoryItem>, MyError>;
}

#[derive(Debug)]
pub struct RepoBox(Box<dyn Repository + Sync + Send>);

impl RepoBox {
pub fn new(repo: Box<dyn Repository + Sync + Send>) -> Self {
Self(repo)
}
}

impl Deref for RepoBox {
type Target = dyn Repository + Sync + Send;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}

impl DerefMut for RepoBox {
fn deref_mut(&mut self) -> &mut Self::Target {
self.0.deref_mut()
}
}

fn path_element_ok(element: &str) -> bool {
!element.starts_with('.')
}
Expand Down
34 changes: 33 additions & 1 deletion src/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rocket::{Build, Rocket};
use crate::error::MyError;
use crate::repository;
use crate::templates;
use crate::templates::render_search_results;
use crate::templates::{
render_edit_page, render_overview, render_page, render_page_placeholder, Breadcrumb,
};
Expand Down Expand Up @@ -284,6 +285,34 @@ fn overview(path: WikiPagePath, w: Wiki) -> Result<response::content::Html<Strin
overview_inner(path, w)
}

fn search_inner(
q: &str,
offset: Option<usize>,
w: Wiki,
) -> Result<response::content::Html<String>, MyError> {
const RESULTS_PER_PAGE: usize = 10;
let results = w.search(q, RESULTS_PER_PAGE, offset)?;
let prev_url = offset.and_then(|v| {
if v >= RESULTS_PER_PAGE {
Some(uri!(search(q, Some(v - RESULTS_PER_PAGE))).to_string())
} else {
None
}
});
let next_url = Some(uri!(search(q, Some(offset.unwrap_or(0) + RESULTS_PER_PAGE))).to_string());
let html = render_search_results(q, results, prev_url, next_url)?;
Ok(response::content::Html(html))
}

#[get("/search?<q>&<offset>")]
fn search(
q: &str,
offset: Option<usize>,
w: Wiki,
) -> Result<response::content::Html<String>, MyError> {
search_inner(q, offset, w)
}

#[get("/")]
fn index(w: Wiki) -> response::Redirect {
let file_name = format!("{}.md", w.settings().index_page());
Expand All @@ -292,7 +321,10 @@ fn index(w: Wiki) -> response::Redirect {
}

pub fn mount_routes(rocket: Rocket<Build>) -> Rocket<Build> {
rocket.mount("/", routes![page, edit_save, edit_view, overview, index])
rocket.mount(
"/",
routes![page, search, edit_save, edit_view, overview, index],
)
}

#[cfg(test)]
Expand Down
41 changes: 37 additions & 4 deletions src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use askama::Template;

use crate::assets::favicon_png_uri;
use crate::assets::primer_css_uri;
use crate::wiki::SearchResult;

pub struct Breadcrumb<'a> {
name: &'a str,
Expand Down Expand Up @@ -116,10 +117,7 @@ pub struct DirectoryEntry<'a> {

impl<'a> DirectoryEntry<'a> {
pub fn new(name: &'a str, href: String) -> Self {
DirectoryEntry {
name,
href,
}
DirectoryEntry { name, href }
}
}

Expand Down Expand Up @@ -158,3 +156,38 @@ pub fn render_overview(
};
template.render()
}

#[derive(Template)]
#[template(path = "search_results.html", escape = "none")]
struct SearchResultsTemplate<'a> {
primer_css_uri: &'a str,
favicon_png_uri: &'a str,
title: &'a str,
query: &'a str,
documents: Vec<SearchResult>,
breadcrumbs: Vec<Breadcrumb<'a>>,
prev_url: Option<String>,
next_url: Option<String>,
}

pub fn render_search_results(
query: &str,
documents: Vec<SearchResult>,
prev_url: Option<String>,
next_url: Option<String>,
) -> askama::Result<String> {
let primer_css_uri = &primer_css_uri();
let favicon_png_uri = &favicon_png_uri();
let breadcrumbs = vec![];
let template = SearchResultsTemplate {
primer_css_uri,
favicon_png_uri,
title: "Search results",
query,
breadcrumbs,
documents,
prev_url,
next_url,
};
template.render()
}
Loading