From 8ca21621aed95443907c7697c6a99a4c7e0737f2 Mon Sep 17 00:00:00 2001 From: Bradley Reynolds Date: Fri, 25 Oct 2024 11:05:38 +0000 Subject: [PATCH] Inline questions Signed-off-by: GitHub --- src/crazylibs/stories.py | 28 +++++++++----------------- stories/a_day_at_the_zoo.txt | 39 +++++++++--------------------------- 2 files changed, 19 insertions(+), 48 deletions(-) diff --git a/src/crazylibs/stories.py b/src/crazylibs/stories.py index 10544a4..3cfdd45 100644 --- a/src/crazylibs/stories.py +++ b/src/crazylibs/stories.py @@ -1,38 +1,30 @@ """Story functions.""" from pathlib import Path - +import re import typer def run_story(story_file: Path) -> None: """Run a story.""" - story_text = Path(story_file).read_text() - raw_title, questions_block, template = story_text.split("---") - - title = raw_title.strip() - - question_rows = questions_block.strip().split("\n") - questions = {} - for row in question_rows: - index, question = row.split(":") - questions[index] = question.strip() + raw_text = Path(story_file).read_text().strip() + title, story_text = raw_text.split("\n", maxsplit=1) typer.echo(f'Welcome to "{title}"') typer.echo("") typer.echo("Please fill in the following words:") - context = {} - for index, question in questions.items(): - context[index] = typer.prompt(question) + context: dict[str, str] = {} - text = template - for index, item in context.items(): - text = text.replace(f"({index})", item) + lines = story_text.split("\n") + for line in lines: + print("oneoffs", re.findall(r"\([a-zA-Z]+\)", line)) + print("new contexts", re.findall(r"\([a-zA-Z]+ : \d+\)", line)) + print("existing", re.findall(r"\(\d+\)", line)) typer.echo("") typer.echo("Wow, what a good selection of words!") typer.echo("Here is your story:") typer.echo("") - typer.echo(text) + # typer.echo(story_text) diff --git a/stories/a_day_at_the_zoo.txt b/stories/a_day_at_the_zoo.txt index 5548266..0991da5 100644 --- a/stories/a_day_at_the_zoo.txt +++ b/stories/a_day_at_the_zoo.txt @@ -1,33 +1,12 @@ A Day at the Zoo ---- -1: adjective -2: animal -3: verb ending in -ing -4: place -5: adjective -6: animal -7: verb ending in -ing -8: adjective -9: food -10: adjective -11: animal -12: adjective -13: animal -14: verb ending in -ing -15: verb ending in -ing -16: adjective -17: adjective -18: noun -19: adjective ---- -One day, my friend and I decided to visit the zoo. We were very (1) to see all the animals. -First, we saw the (2), which was (3) in its (4). -Next, we went to the (5) exhibit where we saw a (6) that was (7). It was so (8)! +One day, my friend and I decided to visit the zoo. We were very (adjective) to see all the animals. +First, we saw the (animal), which was (verb ending in -ing) in its (place). +Next, we went to the (adjective) exhibit where we saw a (animal) that was (verb ending in -ing). It was so (adjective)! -After that, we decided to have some (9) for lunch. -While eating, we saw a (10) (11) that tried to steal our (9)! -We laughed and decided to go see the (12) (13) show. The (13) did tricks like (14) and (15). -It was the most (16) part of our day. +After that, we decided to have some (food : 9) for lunch. +While eating, we saw a (adjective) (animal) that tried to steal our (9)! +We laughed and decided to go see the (adjective) (animal) show. The (animal) did tricks like (verb ending in -ing) and (verb ending in -ing). +It was the most (adjective) part of our day. -Finally, we visited the gift shop and bought a (17) (18) as a souvenir. -It was a (19) day at the zoo, and we couldn't wait to come back again! +Finally, we visited the gift shop and bought a (adjective) (noun) as a souvenir. +It was a (adjective) day at the zoo, and we couldn't wait to come back again!