Compare commits
8 Commits
python
...
388c75b351
Author | SHA1 | Date | |
---|---|---|---|
388c75b351 | |||
17fae8c60e | |||
89e5a1061a | |||
e7db61e551 | |||
42d0775fe6 | |||
700709f171 | |||
ca3baf0768 | |||
70d92e6d04 |
28
notes2web.py
28
notes2web.py
@@ -57,6 +57,7 @@ def get_args():
|
||||
parser.add_argument('-I', '--template-index-foot', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/templates/indexfoot.html'))
|
||||
parser.add_argument('-s', '--stylesheet', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/styles.css'))
|
||||
parser.add_argument('-e', '--extra-index-content', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/templates/extra_index_content.html'))
|
||||
parser.add_argument('-n', '--index-article-names', action='append', default=['index.md'])
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
@@ -87,11 +88,24 @@ def main(args):
|
||||
|
||||
markdown_files, plaintext_files, other_files = get_files(args.notes)
|
||||
|
||||
print(f"{args.index_article_names=}")
|
||||
|
||||
dirs_with_index_article = []
|
||||
|
||||
print(f"{markdown_files=}")
|
||||
for filename in markdown_files:
|
||||
print(f"{filename=}")
|
||||
print(f"{os.path.basename(filename)=}")
|
||||
html = pypandoc.convert_file(filename, 'html', extra_args=[f'--template={args.template}'])
|
||||
output_filename = os.path.splitext(re.sub(f"^{args.notes.name}", args.output_dir.name, filename))[0] + '.html'
|
||||
if os.path.basename(filename) in args.index_article_names:
|
||||
output_filename = os.path.join(
|
||||
os.path.dirname(re.sub(f"^{args.notes.name}", args.output_dir.name, filename)),
|
||||
'index.html'
|
||||
)
|
||||
dirs_with_index_article.append(os.path.dirname(re.sub(f"^{args.notes.name}", args.output_dir.name, filename)))
|
||||
else:
|
||||
output_filename = os.path.splitext(re.sub(f"^{args.notes.name}", args.output_dir.name, filename))[0] + '.html'
|
||||
print(f"{output_filename=}")
|
||||
os.makedirs(os.path.dirname(output_filename), exist_ok=True)
|
||||
|
||||
with open(output_filename, 'w+') as fp:
|
||||
@@ -99,9 +113,9 @@ def main(args):
|
||||
|
||||
print(f"{plaintext_files=}")
|
||||
for filename in plaintext_files:
|
||||
title = os.path.basename(re.sub(f"^{args.notes.name}", args.output_dir.name, filename))
|
||||
output_filename = re.sub(f"^{args.notes.name}", args.output_dir.name, filename) + '.html'
|
||||
os.makedirs(os.path.dirname(output_filename), exist_ok=True)
|
||||
title = os.path.basename(output_filename)
|
||||
html = re.sub(r'\$title\$', title, TEXT_ARTICLE_TEMPLATE_HEAD)
|
||||
html = re.sub(r'\$raw\$', os.path.basename(filename), html)
|
||||
with open(filename) as fp:
|
||||
@@ -123,13 +137,16 @@ def main(args):
|
||||
print(f"{os.path.commonpath(dirs_to_index)=}")
|
||||
|
||||
for directory in dirs_to_index:
|
||||
if directory in dirs_with_index_article:
|
||||
continue
|
||||
paths = os.listdir(directory)
|
||||
print(f"{paths=}")
|
||||
|
||||
indexentries = []
|
||||
|
||||
for path in paths:
|
||||
if path == 'index.html':
|
||||
print(f"{path=}")
|
||||
if path in [ 'index.html', '.git' ]:
|
||||
continue
|
||||
|
||||
fullpath = os.path.join(directory, path)
|
||||
@@ -141,8 +158,11 @@ def main(args):
|
||||
title = soup.find('title').get_text()
|
||||
except AttributeError:
|
||||
title = path
|
||||
else:
|
||||
elif os.path.isdir(fullpath):
|
||||
title = path
|
||||
else:
|
||||
# don't add plaintext files to index, since they have a html wrapper
|
||||
continue
|
||||
|
||||
if title.strip() == '':
|
||||
title = path
|
||||
|
22
readme.md
22
readme.md
@@ -4,13 +4,26 @@ View your notes as a static html site.
|
||||
|
||||

|
||||
|
||||
|
||||
## Why?
|
||||
|
||||
I want to be able to view my notes in a more convenient way.
|
||||
I was already writing them in Pandoc markdown and could view them as PDFs but that wasn't quite
|
||||
doing it for me:
|
||||
|
||||
- It was inconvenient to flick through multiple files of notes to find the right PDF
|
||||
- It was annoying to sync to my phone
|
||||
- PDFs do not scale so they were hard to read on smaller screens
|
||||
- Probably more reasons I can't think of right now
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
0. Install [Pandoc](https://pandoc.org/index.html) and [yq](https://github.com/mikefarah/yq)
|
||||
0. Install [Pandoc](https://pandoc.org/index.html) and [Pip](https://github.com/pypa/pip)
|
||||
|
||||
On arch:
|
||||
```
|
||||
# pacman -S pandoc yq
|
||||
# pacman -S pandoc python-pip
|
||||
```
|
||||
|
||||
1. Run `make install` as root
|
||||
@@ -18,7 +31,7 @@ View your notes as a static html site.
|
||||
## Usage
|
||||
|
||||
```
|
||||
$ notes2web.py NOTES_DIRECTORY_1
|
||||
$ notes2web.py notes_directory
|
||||
```
|
||||
|
||||
Output of `notes2web.py --help`:
|
||||
@@ -27,7 +40,7 @@ Output of `notes2web.py --help`:
|
||||
usage: notes2web.py [-h] [-o OUTPUT_DIR] [-t TEMPLATE] [-H TEMPLATE_TEXT_HEAD]
|
||||
[-f TEMPLATE_TEXT_FOOT] [-i TEMPLATE_INDEX_HEAD]
|
||||
[-I TEMPLATE_INDEX_FOOT] [-s STYLESHEET]
|
||||
[-e EXTRA_INDEX_CONTENT]
|
||||
[-e EXTRA_INDEX_CONTENT] [-n INDEX_ARTICLE_NAMES]
|
||||
notes
|
||||
|
||||
positional arguments:
|
||||
@@ -43,6 +56,7 @@ optional arguments:
|
||||
-I TEMPLATE_INDEX_FOOT, --template-index-foot TEMPLATE_INDEX_FOOT
|
||||
-s STYLESHEET, --stylesheet STYLESHEET
|
||||
-e EXTRA_INDEX_CONTENT, --extra-index-content EXTRA_INDEX_CONTENT
|
||||
-n INDEX_ARTICLE_NAMES, --index-article-names INDEX_ARTICLE_NAMES
|
||||
```
|
||||
|
||||
The command will generate a website in the `output-dir` directory (`./web` by default).
|
||||
|
@@ -1,3 +1,4 @@
|
||||
beautifulsoup4==4.9.3
|
||||
pypandoc==1.5
|
||||
python-magic==0.4.24
|
||||
soupsieve==2.2.1
|
||||
|
@@ -35,6 +35,7 @@ pre {
|
||||
background-color: #d9d9d9 ;
|
||||
color: #000;
|
||||
padding: 1em;
|
||||
overflow-x: scroll;
|
||||
}
|
||||
|
||||
details {
|
||||
|
Reference in New Issue
Block a user