Skip to content

Commit

Permalink
Add a box to type a commit message.
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinWise committed Feb 13, 2022
1 parent ddf024b commit 3857e5e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
33 changes: 22 additions & 11 deletions src/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,23 @@ impl<'r> WikiPagePath<'r> {
}
}

fn file_name_and_extension(&self) -> Option<(&str, &str)> {
fn file_name(&self) -> Option<&str> {
let (file_name, _) = self.segments.split_last()?;
Some(file_name)
}

fn file_stem_and_extension(&self) -> Option<(&str, &str)> {
let (file_name, _) = self.segments.split_last()?;
file_name.rsplit_once('.')
}

fn file_name(&self) -> Option<&str> {
Some(self.file_name_and_extension()?.0)
fn file_stem(&self) -> Option<&str> {
Some(self.file_stem_and_extension()?.0)
}

#[cfg(test)]
fn file_extension(&self) -> Option<&str> {
Some(self.file_name_and_extension()?.1)
Some(self.file_stem_and_extension()?.1)
}

fn breadcrumbs_helper<F: Fn(&'r [&'r str]) -> Origin>(
Expand Down Expand Up @@ -164,17 +169,23 @@ fn edit_save(
Ok(response::Redirect::to(uri!(page(path))))
}

fn default_edit_message(path: &WikiPagePath) -> String {
format!("Update {}", path.file_name().expect("Ill-formed path"))
}

#[get("/edit/<path..>")]
fn edit_view(path: WikiPagePath, w: Wiki) -> Result<response::content::Html<String>, MyError> {
let content = w.read_file(&path.segments).unwrap_or_else(|_| vec![]);
let content = std::str::from_utf8(&content)?;
let post_url = uri!(edit_save(&path));
let view_url = uri!(page(&path));
let file_stem = path.file_name().expect("Ill-formed path");
let title = format!("Editing {}", path.file_name().expect("Ill-formed path"));
let message_placeholder = default_edit_message(&path);
let html = render_edit_page(
file_stem,
&title,
&post_url.to_string(),
&view_url.to_string(),
&message_placeholder,
content,
path.page_breadcrumbs(),
)?;
Expand All @@ -194,7 +205,7 @@ fn page_response(
fn page_inner(path: WikiPagePath, w: Wiki) -> Result<WikiPageResponder, MyError> {
match w.read_file(&path.segments) {
Ok(bytes) => {
let file_info = path.file_name_and_extension();
let file_info = path.file_stem_and_extension();
Ok(match file_info {
Some((file_stem, file_ext)) => {
match crate::page::get_page(file_stem, file_ext, &bytes, w.settings())? {
Expand All @@ -220,7 +231,7 @@ fn page_inner(path: WikiPagePath, w: Wiki) -> Result<WikiPageResponder, MyError>
page(path)
))))
} else {
match path.file_name_and_extension() {
match path.file_stem_and_extension() {
Some((file_stem, "md")) => {
let create_url = uri!(edit_view(&path));
Ok(WikiPageResponder::PagePlaceholder(
Expand Down Expand Up @@ -340,7 +351,7 @@ mod tests {
let parsed = WikiPagePath::from_slice(input);
assert_eq!(
Some(expected_file_stem),
parsed.file_name(),
parsed.file_stem(),
"Unexpected file_stem while parsing request: {:?}",
input
);
Expand Down Expand Up @@ -374,11 +385,11 @@ mod tests {
fn test_request_path_parse_unsupported() {
let empty = WikiPagePath::new(vec![]);
assert!(empty.directories().is_empty());
assert!(empty.file_name_and_extension().is_none());
assert!(empty.file_stem_and_extension().is_none());

let extensionless_file = WikiPagePath::new(vec!["README"]);
assert!(extensionless_file.directories().is_empty());
assert!(extensionless_file.file_name_and_extension().is_none());
assert!(extensionless_file.file_stem_and_extension().is_none());
}

#[test]
Expand Down
3 changes: 3 additions & 0 deletions src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ struct EditTemplate<'a> {
title: &'a str,
post_url: &'a str,
view_url: &'a str,
message_placeholder: &'a str,
content: &'a str,
breadcrumbs: Vec<Breadcrumb<'a>>,
}
Expand All @@ -93,6 +94,7 @@ pub fn render_edit_page(
title: &str,
post_url: &str,
view_url: &str,
message_placeholder: &str,
content: &str,
breadcrumbs: Vec<Breadcrumb<'_>>,
) -> askama::Result<String> {
Expand All @@ -104,6 +106,7 @@ pub fn render_edit_page(
title,
post_url,
view_url,
message_placeholder,
content,
breadcrumbs,
};
Expand Down
30 changes: 19 additions & 11 deletions templates/edit_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@
{% block content %}

<form method="post" action="{{post_url}}">
<div class="tabnav">
<nav class="tabnav-tabs edit_box_tabs" aria-label="Page editor">
<a class="tabnav-tab" href="#url" aria-current="page">Edit file</a>
<!-- <a class="tabnav-tab" href="#url">Preview</a> -->
</nav>
<div class="Box edit_box">
<textarea id="content" name="content" class="edit_text_area">{{content}}</textarea>
<fieldset>
<div class="tabnav">
<nav class="tabnav-tabs edit_box_tabs" aria-label="Page editor">
<a class="tabnav-tab" href="#url" aria-current="page">Edit file</a>
<!-- <a class="tabnav-tab" href="#url">Preview</a> -->
</nav>
</div>
</div>
<button type="submit" class="btn btn-primary">Commit changes</button>
<a href="{{view_url}}" class="btn btn-danger">Cancel</a>
<div class="edit_box">
<textarea id="content" name="content" class="form-control edit_text_area">{{content}}</textarea>
</div>
<div>
<input type="text" name="message" class="form-control input-block" id="gollum-editor-message-field"
placeholder="{{message_placeholder}}">
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Commit changes</button>
<a href="{{view_url}}" class="btn btn-danger">Cancel</a>
</div>
</fieldset>
</form>

{% endblock %}
{% endblock %}
5 changes: 4 additions & 1 deletion templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@
}

.edit_box textarea {
margin: 1em;
font-family: monospace;
width: 100%;
min-height: 500px;
}

fieldset div {
margin-bottom: 1em;
}
</style>
{% block extra_scripts %}
{% endblock %}
Expand Down

0 comments on commit 3857e5e

Please sign in to comment.