Skip to content

Commit

Permalink
refactor readme generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Marmare314 committed Sep 29, 2023
1 parent b787229 commit 4ef45c5
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 194 deletions.
40 changes: 30 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Lemmify
# lemmify

Lemmify is a library for typesetting mathematical
theorems in typst. It aims to be easy to use while
Expand All @@ -7,15 +7,21 @@ This means that the interface might change with updates to typst
(for example if user-defined element functions are introduced).
But no functionality should be lost.

If you are encountering any bugs, have questions or are missing
features, feel free to open an issue on
[GitHub](https://github.com/Marmare314/lemmify).

## Basic usage

1. Import the Lemmify library
1. Import lemmify:

```typst
#import "@preview/lemmify:0.2.0": default-theorems, select-kind
```

2. Generate some common theorem kinds with pre-defined styling
2. Generate some common theorem kinds with pre-defined style:

```typst
#let (
theorem, lemma, corollary,
Expand All @@ -24,36 +30,50 @@ But no functionality should be lost.
) = default-theorems(lang: "en")
```

3. Apply the generated styling
3. Apply the generated style:

```typst
#show: theorem-rules
```

4. Customize the styling using show rules. For example, to add a red box around proofs
4. Customize the theorems using show rules. For example, to add a block around proofs:

```typst
#show select-kind(proof): box.with(stroke: red + 1pt, inset: 1em)
#show select-kind(proof): block.with(
breakable: true,
width: 100%,
fill: gray,
inset: 1em,
radius: 5pt
)
```

5. Create theorems, lemmas, and proofs
5. Create theorems, lemmas, and proofs:

```typst
#theorem(name: "Some theorem")[
Theorem content goes here.
]<thm>
#theorem(numbering: none)[
Another theorem.
]
#proof(link-to: <thm>)[
Complicated proof.
]<proof>
@proof and @thm[theorem]
```

The result should now look something like this
The result should now look something like this:

![image](docs/images/image_0.png)

## Examples

This example shows how corollaries can be numbered after the last theorem.

```typst
#import "@preview/lemmify:0.2.0": theorem-rules, theorem-kind, select-kind, reset-counter
Expand All @@ -73,7 +93,6 @@ This example shows how corollaries can be numbered after the last theorem.
#corollary(lorem(5))
```


![image](docs/images/image_1.png)

## Custom style example
Expand Down Expand Up @@ -118,5 +137,6 @@ This example shows how corollaries can be numbered after the last theorem.
]
```


![image](docs/images/image_2.png)

For a full documentation of all functions check [readme.pdf](docs/readme.pdf)
92 changes: 47 additions & 45 deletions docs/generate_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,69 +13,71 @@ def file_name():
def image_name():
return os.path.join(IMAGE_FOLDER, "image_" + str(GENERATED_IMAGE_COUNT) + ".png")

def get_content():
print("Querying " + README_FILE_PATH, end="", flush=True)
completed_process = subprocess.run(["typst", "query", README_FILE_PATH, "--root", ".", "<export>"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
def create_file_folder():
if not os.path.exists(TMP_FILE_FOLDER):
os.makedirs(TMP_FILE_FOLDER)

def delete_file_folder():
for file in os.listdir(TMP_FILE_FOLDER):
os.remove(os.path.join(TMP_FILE_FOLDER, file))
os.rmdir(TMP_FILE_FOLDER)

def compile_code(code):
global GENERATED_IMAGE_COUNT

current_filename = file_name()
current_imagename = image_name()
GENERATED_IMAGE_COUNT += 1

with open(current_filename, mode="w") as file:
file.write(code)

print("Compiling " + current_filename, end="", flush=True)
completed_process = subprocess.run(["typst", "compile", current_filename, "--root", ".", current_imagename], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if completed_process.returncode != 0:
print(" ✗")
print(completed_process.stderr)
print(completed_process.stdout)
exit(1)
print(" ✓")
return completed_process.stdout
else:
print(" ✓")

return current_imagename

def compile_file():
print("Compiling " + file_name(), end="", flush=True)
completed_process = subprocess.run(["typst", "compile", file_name(), "--root", ".", image_name()], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
def query_exports():
print("Querying " + README_FILE_PATH, end="", flush=True)
completed_process = subprocess.run(["typst", "query", README_FILE_PATH, "--root", ".", "<export>"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if completed_process.returncode != 0:
print(" ✗")
print(completed_process.stderr)
print(completed_process.stdout)
exit(1)
else:
print(" ✓")
print(" ✓")
return completed_process.stdout

def create_file_folder():
if not os.path.exists(TMP_FILE_FOLDER):
os.makedirs(TMP_FILE_FOLDER)
def exported_markdown():
exports = json.loads(query_exports())
return "\n".join([e["value"] for e in exports]).lstrip()

def delete_file_folder():
for file in os.listdir(TMP_FILE_FOLDER):
os.remove(os.path.join(TMP_FILE_FOLDER, file))
os.rmdir(TMP_FILE_FOLDER)
def generate_images(markdown):
open_tag_index = markdown.find("<GENERATE-IMAGE>")
if open_tag_index >= 0:
close_tag_index = markdown.find("</GENERATE-IMAGE>")
assert close_tag_index >= 0

def convert_to_markdown(content):
global GENERATED_IMAGE_COUNT
before = markdown[:open_tag_index].rstrip() + "\n\n"
image_code = markdown[open_tag_index:close_tag_index].removeprefix("<GENERATE-IMAGE>")
after = markdown[close_tag_index:].removeprefix("</GENERATE-IMAGE>")

result = ""
for element in content:
if element["func"] == "heading":
if result != "":
result += "\n"
# assume unnumbered and text-only headings
result += "#" * element["level"] + " " + element["body"]["text"] + "\n\n"
elif element["func"] == "text":
result += element["text"] + "\n"
elif element["func"] == "raw":
result += "```" + element["lang"] + "\n" + element["text"] + "\n" + "```\n"
elif element["func"] == "metadata" and "code" in element["value"]:
with open(file_name(), mode="w") as file:
file.write(element["value"]["code"])
compile_file()
result += f"\n![image]({image_name()})\n"
GENERATED_IMAGE_COUNT += 1
elif element["func"] == "metadata" and "text" in element["value"]:
result += element["value"]["text"] + "\n"
elif element["func"] == "enum":
element = element["children"][0]
result += str(element["number"]) + ". " + element["body"]["text"] + "\n"

return result
image_path = compile_code(image_code)
return before + f"![image]({image_path})" + generate_images(after)
else:
return markdown

def main():
content = json.loads(get_content())
markdown = exported_markdown()
create_file_folder()
markdown = convert_to_markdown(content)
markdown = generate_images(markdown)
delete_file_folder()
with open("README.md", mode="w") as file:
file.write(markdown)
Expand Down
Binary file modified docs/readme.pdf
Binary file not shown.
Loading

0 comments on commit 4ef45c5

Please sign in to comment.