Commit a069ecbc authored by Marien Fressinaud's avatar Marien Fressinaud

add: Convert Markdown files to HTML

parent 9ef8be10
# Boop! – a simple "low-tech" static site generator, for fun. # Boop! – a simple "low-tech" static site generator, for fun.
## Installation
In this guide, we assume you are using Linux.
First, you should clone the Boop! repository and install its dependencies.
Boop! requires Python 3.6 to work (mainly because of our use of the `f-strings`).
```console
$ git clone https://framagit.org/marien.fressinaud/boop.git
$ cd boop
$ pip3 install -r requirements.txt
$ cd ..
```
Then, add the path to your `.bashrc`:
```console
$ echo export PATH="$(pwd)/boop:\$PATH" >> ~/.bashrc
```
And source it so it considers the change:
```console
$ source ~/.bashrc
```
## Usage ## Usage
In this section, we assume you are using Linux. First, create a project directory:
```console
$ mkdir my-website && cd my-website
```
Create your website in a `./content` folder and execute the Boop! command: Create a `./content` folder and execute the Boop! command:
```console ```console
$ mkdir content $ mkdir content
$ echo '<h1>Welcome!</h1>' > content/index.html $ echo '<h1>Welcome!</h1>' > content/index.html
$ ./boop.py $ boop.py
Boop! Boop!
``` ```
...@@ -23,13 +53,19 @@ $ tree ...@@ -23,13 +53,19 @@ $ tree
. .
├── content ├── content
│   └── index.html │   └── index.html
├── output └── output
│   └── index.html └── index.html
└── boop.py
``` ```
Yeah, it's kinda too simple for the moment (it only copies the files from Boop! basically just copied `content` files under a `output` directory. But it
`./content` to `./output`), but new features are coming soon! can convert Markdown files in HTML too:
```console
$ echo '# Welcome!' > content/markdown-index.md
$ boop.py
$ echo output/markdown-index.html
<h1>Welcome!</h1>
```
## Tests ## Tests
......
...@@ -4,6 +4,8 @@ import os ...@@ -4,6 +4,8 @@ import os
import sys import sys
import shutil import shutil
import markdown
class ProgramError(Exception): class ProgramError(Exception):
"""Exception raised during the program execution. """Exception raised during the program execution.
...@@ -54,16 +56,30 @@ def main(): ...@@ -54,16 +56,30 @@ def main():
# ... but it also can be a simple file if user created it manually! # ... but it also can be a simple file if user created it manually!
os.remove(output_path) os.remove(output_path)
md = markdown.Markdown()
# And copy the files from ./content to ./output # And copy the files from ./content to ./output
for filepath in dir_tree(content_path): for filepath in dir_tree(content_path):
base, ext = os.path.splitext(filepath)
content_filepath = os.path.join(content_path, filepath) content_filepath = os.path.join(content_path, filepath)
output_filepath = os.path.join(output_path, filepath) output_filepath = os.path.join(output_path, filepath)
if ext == ".md":
output_filepath = os.path.join(output_path, f"{base}.html")
# First we need to create all the parent directories for our file # First we need to create all the parent directories for our file
mkdirs_for_file(output_filepath) mkdirs_for_file(output_filepath)
# Then, we copy content file to its output destination if ext == ".md":
shutil.copyfile(content_filepath, output_filepath) # Then, if it is a Markdown file, we convert it
with open(content_filepath, "r") as content_file:
html = md.convert(content_file.read())
# And we write it as a HTML file
with open(output_filepath, "w") as output_file:
output_file.write(html)
else:
# ... else, we simply copy the file to the output directory
shutil.copyfile(content_filepath, output_filepath)
if __name__ == "__main__": if __name__ == "__main__":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment