From 02c53afea1ec6c6ae5060e01f908497a01f03981 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 15 Oct 2020 09:41:20 +0300 Subject: feat: create new draft, publish it --- jt.md | 25 +++++++++++++++++ src/main.rs | 47 +++++++++++++++++++++++++++++++ subplot/jt.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ subplot/jt.yaml | 27 ++++++++++++++++++ 4 files changed, 185 insertions(+) diff --git a/jt.md b/jt.md index 6c2eb9b..3295e0f 100644 --- a/jt.md +++ b/jt.md @@ -55,6 +55,31 @@ then command fails ~~~ +## Create a new draft and publish it + +Verify that we can create a new draft entry for the journal. + +~~~scenario +when I invoke jt init jrnl default "My test journal" +then command is successful +and there are no drafts in jrnl +and there are no journal entries in jrnl + +when I invoke jt new "Abracadabra" --editor=none --dirname=jrnl +then command is successful +then there is one draft in jrnl +and draft 0 in jrnl contains "Abracadabra" + +when I edit draft 0 in jrnl to also contain "Behold!" +and I invoke jt finish --dirname=jrnl +then command is successful +then there is one journal entry in jrnl, at FILE +and file contains "Abracadabra" +and file contains "Behold!" +and there are no drafts in jrnl +~~~ + + --- title: "jt—a journalling tool" diff --git a/src/main.rs b/src/main.rs index 4e0a887..cb24474 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,23 @@ enum JT { #[structopt(help = "Directory that may or may not be a journal")] dirname: PathBuf, }, + New { + #[structopt(long, short, help = "Use DIRNAME as the location of the journal")] + dirname: PathBuf, + #[structopt( + long, + short, + help = "Invoke EDITOR for user to edit draft", + default_value = "/usr/bin/editor" + )] + editor: String, + #[structopt(help = "Title of new draft")] + title: String, + }, + Finish { + #[structopt(long, short, help = "Use DIRNAME as the location of the journal")] + dirname: PathBuf, + }, } #[derive(Debug, Error)] @@ -36,6 +53,12 @@ fn main() -> Result<()> { description, } => init(&dirname, &journalname, &description)?, JT::IsJournal { dirname } => is_journal(&dirname)?, + JT::New { + title, + dirname, + editor, + } => new_draft(&title, &dirname, &editor)?, + JT::Finish { dirname } => finish_draft(&dirname)?, } Ok(()) } @@ -52,3 +75,27 @@ fn is_journal(dirname: &Path) -> anyhow::Result<()> { } Ok(()) } + +fn new_draft(title: &str, dirname: &Path, editor: &str) -> anyhow::Result<()> { + let drafts = dirname.join("drafts"); + if !drafts.exists() { + std::fs::create_dir(&drafts)?; + } + let draft_filename = drafts.join("0.md"); + std::fs::write(draft_filename, title)?; + Ok(()) +} + +fn finish_draft(dirname: &Path) -> anyhow::Result<()> { + let drafts = dirname.join("drafts"); + let draft = drafts.join("0.md"); + + let entries = dirname.join("entries"); + if !entries.exists() { + std::fs::create_dir(&entries)?; + } + let entry = entries.join("0.md"); + + std::fs::rename(draft, entry)?; + Ok(()) +} diff --git a/subplot/jt.py b/subplot/jt.py index 1b46b0b..b9a7122 100644 --- a/subplot/jt.py +++ b/subplot/jt.py @@ -17,6 +17,18 @@ def run_jt_is_journal(ctx, dirname=None): runcmd_run(ctx, [_binary("jt"), "is-journal", dirname]) +def run_jt_new(ctx, title=None, dirname=None): + runcmd_run = globals()["runcmd_run"] + runcmd_run( + ctx, [_binary("jt"), "new", title, "--dirname", dirname, "--editor=none"] + ) + + +def run_jt_finish(ctx, dirname=None): + runcmd_run = globals()["runcmd_run"] + runcmd_run(ctx, [_binary("jt"), "finish", "--dirname", dirname]) + + def _binary(name): srcdir = globals()["srcdir"] return os.path.join(srcdir, "target", "debug", "jt2") @@ -32,3 +44,77 @@ def output_contains(ctx, pattern=None): assert_eq = globals()["assert_eq"] logging.debug("checking if %r contains", ctx["stdout"], pattern) assert_eq(pattern in ctx["stdout"], True) + + +def journal_has_no_drafts(ctx, dirname=None): + assert_eq = globals()["assert_eq"] + logging.debug(f"checking {dirname} has no drafts") + drafts = os.path.join(dirname, "drafts") + assert_eq(_find_files(drafts), []) + + +def journal_has_one_draft(ctx, dirname=None): + assert_eq = globals()["assert_eq"] + logging.debug(f"checking {dirname} has one draft") + drafts = os.path.join(dirname, "drafts") + assert_eq(len(_find_files(drafts)), 1) + + +def journal_has_no_entries(ctx, dirname=None): + assert_eq = globals()["assert_eq"] + logging.debug(f"checking {dirname} has no entries") + entries = os.path.join(dirname, "entries") + assert_eq(_find_files(entries), []) + + +def journal_has_one_entry(ctx, dirname=None, variable=None): + assert_eq = globals()["assert_eq"] + logging.debug( + f"checking {dirname} has one entry, whose filename is remembered as {variable}" + ) + entries = os.path.join(dirname, "entries") + files = _find_files(entries) + assert_eq(len(files), 1) + variables = ctx.get("variables", {}) + variables[variable] = files[0] + ctx["variables"] = variables + + +def _find_files(root): + if not os.path.exists(root): + return [] + + files = [] + for dirname, _, basenames in os.walk(root): + for basename in basenames: + files.append(os.path.join(dirname, basename)) + return files + + +def draft_contains_string(ctx, dirname=None, draftno=None, pattern=None): + logging.debug(f"checking draft {draftno} in {dirname} has contains {pattern!r}") + draft = os.path.join(dirname, "drafts", f"{draftno}.md") + with open(draft) as f: + data = f.read() + logging.debug(f"draft content: {data!r}") + assert pattern in data + + +def edit_draft(ctx, dirname=None, draftno=None, text=None): + logging.debug(f"editing draft {draftno} in {dirname} to also contain {text!r}") + draft = os.path.join(dirname, "drafts", f"{draftno}.md") + with open(draft, "a") as f: + f.write(text) + + +def file_contains(ctx, variable=None, pattern=None): + logging.debug(f"checking {variable} contains {pattern!r}") + + variables = ctx.get("variables", {}) + logging.debug(f"variables: {variables!r}") + + filename = variables[variable] + with open(filename) as f: + data = f.read() + logging.debug(f"file content: {data!r}") + assert pattern in data diff --git a/subplot/jt.yaml b/subplot/jt.yaml index 77b8c82..5ed6ed3 100644 --- a/subplot/jt.yaml +++ b/subplot/jt.yaml @@ -8,9 +8,36 @@ - when: I invoke jt is-journal {dirname} function: run_jt_is_journal +- when: I invoke jt new "{title:text}" --editor=none --dirname={dirname} + function: run_jt_new + +- when: I invoke jt finish --dirname={dirname} + function: run_jt_finish + +- when: I edit draft {draftno} in {dirname} to also contain "{text}" + function: edit_draft + - then: directory {dirname} exists function: is_directory - then: output contains "(?P.*)" regex: true function: output_contains + +- then: there are no drafts in {dirname} + function: journal_has_no_drafts + +- then: there is one draft in {dirname} + function: journal_has_one_draft + +- then: draft {draftno} in {dirname} contains "{pattern}" + function: draft_contains_string + +- then: there are no journal entries in {dirname} + function: journal_has_no_entries + +- then: there is one journal entry in {dirname}, at {variable} + function: journal_has_one_entry + +- then: file <{variable}> contains "{pattern}" + function: file_contains -- cgit v1.2.1