pysocha 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pysocha/__init__.py ADDED
@@ -0,0 +1,52 @@
1
+ # -*- coding: utf-8 -*-
2
+ import os
3
+ import typer
4
+ import yaml
5
+ from yaml import Loader
6
+ from pprint import pprint
7
+ from typing_extensions import Annotated
8
+ from .config import override_config
9
+ from .markdown import parseCommonMark
10
+ from .preview import makePreviewServer
11
+ from .build import buildSite
12
+
13
+ mark = """---
14
+ Title: Foo and Bar
15
+ Author: Me and myself
16
+ Date: 2004-09-25:12:28:00
17
+ ---
18
+ ### This is a header
19
+
20
+ * List 1
21
+ * List 2
22
+ * List 3
23
+
24
+ This is some stuff *bold* or not.
25
+ """
26
+
27
+ app = typer.Typer()
28
+
29
+ def initialize(config_file: str):
30
+ return override_config(yaml.load(config_file, Loader=Loader))
31
+
32
+ @app.command()
33
+ def hello(name: Annotated[str, typer.Argument()] = 'World') -> str:
34
+ print(f"Hello {name}!")
35
+
36
+ @app.command()
37
+ def preview(config_file: Annotated[typer.FileText, typer.Option('--config-file', '-c')] = './config.yaml'):
38
+ config = initialize(config_file)
39
+ output_dir = os.path.join(os.getcwd(), config['outputDir'])
40
+ server = makePreviewServer(output_dir, config['startPage'], config['defaultExtension'])
41
+ server.config['SEND_FILE_MAX_AGE_DEFAULT'] = 5
42
+ buildSite(config)
43
+ server.run(host='0.0.0.0')
44
+
45
+
46
+ @app.command()
47
+ def build(config_file: Annotated[typer.FileText, typer.Option('--config-file', '-c')] = './config.yaml'):
48
+ config = initialize(config_file)
49
+ pprint(config)
50
+ pprint(parseCommonMark(mark)['markdown'])
51
+ buildSite(config)
52
+
pysocha/__main__.py ADDED
@@ -0,0 +1,7 @@
1
+ # -*- coding: utf-8 -*-
2
+ from . import app
3
+
4
+
5
+ if __name__ == '__main__':
6
+ app()
7
+
pysocha/build.py ADDED
@@ -0,0 +1,252 @@
1
+ import dateutil.parser
2
+ import jinja2
3
+ import math
4
+ import os
5
+ import re
6
+ import shutil
7
+
8
+ from slugify import slugify
9
+
10
+ from .markdown import parseCommonMark
11
+
12
+
13
+ def get_markdown_files(directory):
14
+ """
15
+ This function is used to get all the markdown files in a directory
16
+ """
17
+ files = []
18
+ for file in os.listdir(directory):
19
+ if file.endswith('.md'):
20
+ files.append(file)
21
+ return files
22
+
23
+ def generate_blog_posts(config: dict):
24
+ """
25
+ We go through all blog posts and their related optional outputs like tags or authors, depending on config
26
+ """
27
+
28
+ templateDir = config['templatesDir']
29
+ templateLoader = jinja2.FileSystemLoader(searchpath=templateDir)
30
+ templateEnv = jinja2.Environment( loader=templateLoader )
31
+ template = templateEnv.get_template(config['blogConfig']['blogTemplate'])
32
+ outputDir = os.path.join(os.getcwd(), config['outputDir'])
33
+
34
+ posts = {}
35
+ sorted_posts = []
36
+ posts_dir = os.path.join(config['contentDir'], 'blog_posts')
37
+
38
+ # First we need to get all the content and frontmatter into posts for later
39
+ for post in get_markdown_files(posts_dir):
40
+ with open(os.path.join(posts_dir, post), 'r') as f:
41
+ file = f.read()
42
+ mark = parseCommonMark(file)
43
+ post_data = mark['frontmatter']
44
+ post_data['PostedDate'] = dateutil.parser.parse(post_data['PostedDate'])
45
+ post_template = template
46
+ # If we override the template for that page, here it gets invoked
47
+ if 'Template' in mark['frontmatter']:
48
+ post_template = templateEnv.get_template(mark['frontmatter']['Template'])
49
+ # Same with file extension, which opens some nice scripting possibilities
50
+ file_ext = mark['frontmatter'].get('Extension', config['defaultExtension'])
51
+ post_data['slugify'] = slugify(mark['frontmatter']['Title']) + file_ext
52
+ output_filename = os.path.join(outputDir + '/' + config['blogConfig']['blogBaseDir'] + '/' + post_data['slugify'])
53
+ # This will give all frontmatter and the content to the jinja template
54
+ post_data['content'] = mark['markdown']
55
+ post_data['rendered'] = post_template.render(post_data)
56
+ post_data['rendered'] = config['_pattern'].sub(lambda m: config['_replacements'][m.group(0)], post_data['rendered'])
57
+ posts[post] = post_data
58
+ if post_data.get('Unlisted') is None:
59
+ sorted_posts.append(post_data)
60
+
61
+ sorted_posts.sort(key=lambda post: post['PostedDate'], reverse=config['descending'])
62
+ blog_config = config['blogConfig']
63
+ listing_template = blog_config['listingTemplate']
64
+ listings = []
65
+ tags_template = blog_config['tagsTemplate']
66
+ tag_template = blog_config['tagTemplate']
67
+ tags = {}
68
+ authors_template = blog_config['authorsTemplate']
69
+ author_template = blog_config['authorTemplate']
70
+ authors = {}
71
+ output_dir = os.path.join(config['outputDir'], blog_config['blogBaseDir'])
72
+ file_ext = config['defaultExtension']
73
+ os.makedirs(output_dir, exist_ok=True)
74
+
75
+ # We'll need to make all the blog posts
76
+ for blog_post in sorted_posts:
77
+ output_filename = os.path.join(output_dir, blog_post['slugify'])
78
+ with open(output_filename, 'w+') as f:
79
+ f.write(blog_post['rendered'])
80
+
81
+ # Now populate lists, first one only if the post has all the keys needed for a listing
82
+ listings.append(blog_post)
83
+ # Make a list of posts for each tag out there
84
+ if tags_template and tag_template and blog_post.get('Tags'):
85
+ for tag in blog_post['Tags']:
86
+ if tag not in tags:
87
+ tags[tag] = []
88
+ tags[tag].append(blog_post)
89
+ # Make a list of posts for each author out there
90
+ if authors_template and author_template and blog_post.get('Author'):
91
+ if blog_post['Author'] not in authors:
92
+ authors[blog_post['Author']] = []
93
+ authors[blog_post['Author']].append(blog_post)
94
+
95
+ # Make them templates objects
96
+ listing_template = templateEnv.get_template(listing_template)
97
+ tags_template = templateEnv.get_template(tags_template)
98
+ tag_template = templateEnv.get_template(tag_template)
99
+ authors_template = templateEnv.get_template(authors_template)
100
+ author_template = templateEnv.get_template(author_template)
101
+
102
+ # Listings, and they need pages
103
+ page_limit = blog_config['listingPaginationNum']
104
+ total_pages = int(math.floor(len(listings) / page_limit) + 1)
105
+ ranges = range(1, total_pages + (total_pages > 1)) or [1]
106
+ for page_num in ranges:
107
+ file_num = str(page_num) + file_ext
108
+ offset = (page_num - 1) * page_limit
109
+ output_filename = os.path.join(output_dir, 'listing' + file_num)
110
+ with open(output_filename, 'w+') as f:
111
+ f.write(listing_template.render({'posts': listings[offset:offset+page_limit],
112
+ 'all_posts': sorted_posts,
113
+ 'title': blog_config['title'],
114
+ 'page_num': page_num,
115
+ 'total_pages': total_pages}))
116
+
117
+ output_filename = os.path.join(output_dir, 'tags' + file_ext)
118
+ with open(output_filename, 'w+') as f:
119
+ f.write(tags_template.render({'tags': tags, 'title': 'Tags', 'all_posts': sorted_posts}))
120
+ for tag, posts in tags.items():
121
+ page_limit = blog_config['tagPaginationNum']
122
+ total_pages = int(math.floor(len(posts) / page_limit)) + 1
123
+ ranges = range(1, total_pages + (total_pages > 1)) or [1]
124
+ for page_num in ranges:
125
+ file_num = str(page_num) + file_ext
126
+ offset = (page_num - 1) * page_limit
127
+ tag_file = 'tag_' + tag.replace(' ', '_')
128
+ output_filename = os.path.join(output_dir, tag_file + file_num)
129
+ with open(output_filename, 'w+') as f:
130
+ f.write(tag_template.render({'posts': posts[offset:offset+page_limit],
131
+ 'all_posts': sorted_posts,
132
+ 'tag': tag,
133
+ 'tag_file': tag_file,
134
+ 'title': 'Posts for tag: ' + tag,
135
+ 'page_num': page_num,
136
+ 'total_pages': total_pages}))
137
+
138
+ output_filename = os.path.join(output_dir, 'authors' + file_ext)
139
+ with open(output_filename, 'w+') as f:
140
+ f.write(authors_template.render({'authors': authors, 'title': 'Authors', 'all_posts': sorted_posts}))
141
+ for author, posts in authors.items():
142
+ if 'authorPaginationNum' in blog_config:
143
+ page_limit = blog_config['authorPaginationNum']
144
+ total_pages = int(math.floor(len(posts) / page_limit) + 1)
145
+ ranges = range(1, total_pages + (total_pages > 1)) or [1]
146
+ for page_num in ranges:
147
+ file_num = str(page_num) + file_ext
148
+ offset = (page_num - 1) * page_limit
149
+ author_file = 'author_' + author.replace(' ', '_')
150
+ output_filename = os.path.join(output_dir, author_file + file_num)
151
+ with open(output_filename, 'w+') as f:
152
+ f.write(author_template.render({'author': author,
153
+ 'author_file': author_file,
154
+ 'title': 'Posts for author: ' + author,
155
+ 'posts': posts[offset:offset+page_limit],
156
+ 'all_posts': sorted_posts,
157
+ 'page_num': page_num,
158
+ 'total_pages': total_pages}))
159
+ else:
160
+ output_filename = os.path.join(output_dir, 'author_' + author.replace(' ', '_') + file_ext)
161
+ with open(output_filename, 'w+') as f:
162
+ f.write(author_template.render({'author': author,
163
+ 'all_posts': sorted_posts,
164
+ 'title': 'Posts for author: ' + author,
165
+ 'posts': posts}))
166
+
167
+ def generate_pages(config: dict):
168
+ """
169
+ We go through all the possibilities for generating any page related details
170
+ """
171
+ templateDir = config['templatesDir']
172
+ templateLoader = jinja2.FileSystemLoader(searchpath=templateDir)
173
+ templateEnv = jinja2.Environment( loader=templateLoader )
174
+ template = templateEnv.get_template(config['pageConfig']['pageDefaultTemplate'])
175
+ outputDir = os.path.join(os.getcwd(), config['outputDir'])
176
+
177
+ pages = {}
178
+ pages_dir = os.path.join(config['contentDir'], 'pages')
179
+ for page in get_markdown_files(pages_dir):
180
+ pages[page] = {}
181
+ with open(os.path.join(pages_dir, page), 'r') as f:
182
+ file = f.read()
183
+ mark = parseCommonMark(file)
184
+ page_template = template
185
+ # If we override the template for that page, here it gets invoked
186
+ if 'Template' in mark['frontmatter']:
187
+ page_template = templateEnv.get_template(mark['frontmatter']['Template'])
188
+ # Same with file extension, which opens some nice scripting possibilities
189
+ file_ext = mark['frontmatter'].get('Extension', config['defaultExtension'])
190
+ pages[page]['slugify'] = page.rstrip('.md')
191
+ output_filename = os.path.join(outputDir + '/' + pages[page]['slugify'] + file_ext)
192
+ print(output_filename)
193
+ # This will give all frontmatter and the content to the jinja template
194
+ context = mark['frontmatter']
195
+ context['content'] = mark['markdown']
196
+ with open(output_filename, 'w+') as f:
197
+ foo = page_template.render(context)
198
+ foo = config['_pattern'].sub(lambda m: config['_replacements'][m.group(0)], foo)
199
+ f.write(foo)
200
+
201
+ def generate_extras(config: dict):
202
+ """
203
+ EVERY site should have extra files, even if it's just CSS or JS files, so process them here very simply
204
+ """
205
+ extra_files_dir = os.path.join(config['contentDir'], 'extra_files')
206
+ output_dir = os.path.join(config['outputDir'])
207
+ shutil.copytree(extra_files_dir, output_dir, dirs_exist_ok=True)
208
+
209
+ def build_pages_replacement_map(config: dict) -> dict:
210
+ """
211
+ We need to replace instances of things like "index.md" with "index.html" anywhere it's reference in markdown, so
212
+ just build a map for pages here ahead of the markdown conversion
213
+ """
214
+ pages_dir = os.path.join(config['contentDir'], 'pages')
215
+ replacements = {}
216
+ for page in get_markdown_files(pages_dir):
217
+ replacements[page] = page.replace('.md', config['defaultExtension'])
218
+ return replacements
219
+
220
+ def build_posts_replacement_map(config: dict) -> dict:
221
+ """
222
+ We also need a replacement map for posts, which means we need to replace them with their slugs
223
+ """
224
+ pages_dir = os.path.join(config['contentDir'], 'blog_posts')
225
+ # At some point, if we link to the listings or just the blog base as me, we'll need that in this patterns too,
226
+ # but that's a mistake anyway, as we should be using the default extension and know what we're linking to
227
+ replacements = {}
228
+ for page in get_markdown_files(pages_dir):
229
+ with open(os.path.join(pages_dir, page), 'r') as f:
230
+ file = f.read()
231
+ mark = parseCommonMark(file)
232
+ replacements[page] = config['blogConfig']['blogBaseDir'] + '/' + slugify(mark['frontmatter']['Title']) + config['defaultExtension']
233
+ return replacements
234
+
235
+ def buildSite(config: dict):
236
+ if os.path.exists(config['outputDir']):
237
+ shutil.rmtree(config['outputDir'])
238
+ os.makedirs(config['outputDir'])
239
+ pages_map = build_pages_replacement_map(config)
240
+ posts_map = build_posts_replacement_map(config)
241
+ # Now that we have the maps, we can build the replacements and the pattern for regex on all files
242
+ config['_replacements'] = {**pages_map, **posts_map}
243
+ rep = dict((re.escape(k), v) for k, v in config['_replacements'].items())
244
+ config['_pattern'] = re.compile("|".join(rep.keys()))
245
+ if len(config['_replacements'].keys()) > len(pages_map.keys()) + len(posts_map.keys()):
246
+ print('Pages and Posts have overlapping names. Please fix this.')
247
+ print('Overlappng names: ', pages_map.keys() & posts_map.keys())
248
+ exit(1)
249
+ generate_extras(config)
250
+ generate_pages(config)
251
+ generate_blog_posts(config)
252
+ return
pysocha/config.py ADDED
@@ -0,0 +1,35 @@
1
+ import yaml
2
+
3
+ default_yaml = """
4
+ contentDir: "content"
5
+ outputDir: "generated"
6
+ templatesDir: "templates"
7
+ siteTitle: "Example dot Com!"
8
+ descending: True
9
+ defaultExtension: ".html"
10
+ startPage: "index.html"
11
+ pageConfig:
12
+ pageTitle: "Example dot Com!"
13
+ pageDefaultTemplate: "page.jinja2"
14
+ blogConfig:
15
+ title: "Example blog"
16
+ blogBaseDir: "blog"
17
+ blogTemplate: "blog.jinja2"
18
+ listingTemplate: "listing.jinja2"
19
+ listingPagination_num: 5
20
+ listingKeysNeeded: ['hook']
21
+ atomFeed: True
22
+ tagsTemplate: "tags.jinja2"
23
+ tagTemplate: "tag.jinja2"
24
+ tagPaginationNum: 5
25
+ authorsTemplate: "authors.jinja2"
26
+ authorTemplate: "author.jinja2"
27
+ authorPaginationNum: 10
28
+ """
29
+
30
+ CONFIG = yaml.load(default_yaml, Loader=yaml.Loader)
31
+
32
+ def override_config(overrides: dict) -> dict:
33
+ CONFIG.update(overrides)
34
+ return CONFIG
35
+
pysocha/markdown.py ADDED
@@ -0,0 +1,27 @@
1
+ import frontmatter
2
+ from markdown_it import MarkdownIt
3
+ from mdit_py_plugins.front_matter import front_matter_plugin
4
+
5
+ MD = None
6
+
7
+ def _initialize(options: dict = None):
8
+ global MD
9
+ if options is None:
10
+ options = {}
11
+ MD = (
12
+ MarkdownIt("commonmark", options)
13
+ .use(front_matter_plugin)
14
+ .enable(['table', 'linkify', 'replacements'])
15
+ )
16
+
17
+ def parseCommonMark(markdown: str, options: dict = None):
18
+ global MD
19
+ if options is None:
20
+ options = {}
21
+ if MD is None:
22
+ _initialize(options)
23
+ retVal = {
24
+ 'frontmatter': frontmatter.loads(markdown).metadata,
25
+ 'markdown': MD.render(markdown)
26
+ }
27
+ return retVal
pysocha/preview.py ADDED
@@ -0,0 +1,36 @@
1
+ import mimetypes
2
+ from flask import Flask, make_response
3
+ from functools import update_wrapper
4
+
5
+ # Yes, I know, globals bad, it's a preview dev tool
6
+ previewServer = Flask(__name__, static_url_path='')
7
+ _startPage = 'index.html'
8
+
9
+ def nocache(f):
10
+ def new_func(*args, **kwargs):
11
+ resp = make_response(f(*args, **kwargs))
12
+ resp.cache_control.no_cache = True
13
+ return resp
14
+ return update_wrapper(new_func, f)
15
+
16
+ @previewServer.after_request
17
+ def nocache_response(response):
18
+ response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
19
+ response.headers['Expires'] = '-1'
20
+ return response
21
+
22
+ @previewServer.route('/hello')
23
+ def hello_world():
24
+ return 'Hello World!'
25
+
26
+ @previewServer.route('/')
27
+ @nocache
28
+ def root():
29
+ return previewServer.send_static_file(_startPage)
30
+
31
+ def makePreviewServer(static_folder: str = 'generated', start_page: str = 'index.html', extension: str = 'html'):
32
+ global previewServer, _startPage
33
+ mimetypes.add_type('text/html', extension)
34
+ previewServer.static_folder=static_folder
35
+ _startPage = start_page
36
+ return previewServer
@@ -0,0 +1,21 @@
1
+ Metadata-Version: 2.3
2
+ Name: pysocha
3
+ Version: 0.1.0
4
+ Summary: Extremely simple static site generator
5
+ Author-email: Tod Hansmann <githubpub@todandlorna.com>
6
+ Requires-Python: >=3.8
7
+ Requires-Dist: flask>=3.0.3
8
+ Requires-Dist: jinja2>=3.1.4
9
+ Requires-Dist: linkify-it-py>=2.0.3
10
+ Requires-Dist: markdown-it-py>=3.0.0
11
+ Requires-Dist: mdit-py-plugins>=0.4.1
12
+ Requires-Dist: python-dateutil>=2.9.0.post0
13
+ Requires-Dist: python-frontmatter>=1.1.0
14
+ Requires-Dist: python-slugify>=8.0.4
15
+ Requires-Dist: pyyaml>=6.0.1
16
+ Requires-Dist: typer>=0.12.3
17
+ Description-Content-Type: text/markdown
18
+
19
+ # pysocha
20
+
21
+ pysocha is a static site generator of as simple a variety as we can make for our needs.
@@ -0,0 +1,10 @@
1
+ pysocha/__init__.py,sha256=kaGMDBNKR1JxsPCA8ER6OTdZ1sJP6GGACF23OA1JX6w,1369
2
+ pysocha/__main__.py,sha256=iKbFhzLN2XC4yRbkkwnMfLwJRRDxoXvSlIo0Vj6nU8s,83
3
+ pysocha/build.py,sha256=e9M2JsxB0OAT1OONxJODV_bJaJn-a4AWs1jwhh7fZSc,12553
4
+ pysocha/config.py,sha256=7kffSB1EVD3ks-OJ8AJP5zr4J7MTXXqqxgW1AYqHgeQ,810
5
+ pysocha/markdown.py,sha256=A60uk1hEju7RRmRnuSnhbX4AhAp-g_3apNoD1uAAAik,678
6
+ pysocha/preview.py,sha256=diwSpuDTczts3DGKpnGjleBrR-P5KfHOju2pI3jzbPg,1141
7
+ pysocha-0.1.0.dist-info/METADATA,sha256=2xlbsTIzDvBk9XVBu_Lh_TERj19vf_o-6z6hDytrF4Y,665
8
+ pysocha-0.1.0.dist-info/WHEEL,sha256=zEMcRr9Kr03x1ozGwg5v9NQBKn3kndp6LSoSlVg-jhU,87
9
+ pysocha-0.1.0.dist-info/entry_points.txt,sha256=iyC_dtNuFpxqyo55McZemkofmxe23mcudLxhS5FOXU8,41
10
+ pysocha-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.24.2
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ pysocha = pysocha:main