.style.yapf
... ...
@@ -8,6 +8,7 @@ blank_line_before_module_docstring=True
8 8
blank_lines_around_top_level_definition=1
9 9
coalesce_brackets=True
10 10
column_limit=80
11
+continuation_indent_width=2
11 12
dedent_closing_brackets=True
12 13
indent_dictionary_value=True
13 14
indent_width=2
pages/essays.md
... ...
@@ -20,6 +20,9 @@ Not all essays here are of a spiritual nature. Some, like **A Vision of
20 20
Saturn**, are more of a personal commentary or thought.
21 21
22 22
---
23
+<<ListMdFiles('essays')>>
24
+
25
+---
23 26
24 27
[Rights, Powers and Principles](essays/rights-powers-principles)
25 28
post2md
... ...
@@ -3,61 +3,100 @@
3 3
import argparse
4 4
import json
5 5
import textwrap
6
-#import sys
7
-import os
8 6
9
-from jinja2 import Environment, FileSystemLoader, select_autoescape
7
+from jinja2 import Environment
10 8
11 9
##############################################################################
10
+# Argument
11
+
12
+json_filename = ''
13
+
14
+#-----------------------------------------------------------------------------
15
+def load_json(filename):
16
+ try:
17
+ global json_filename
18
+
19
+ with open(filename, 'r') as file:
20
+ json_filename = filename
21
+ return json.load(file)
22
+
23
+ except FileNotFoundError:
24
+ raise argparse.ArgumentTypeError(f"The file {filename} does not exist.")
25
+
26
+ except json.JSONDecodeError:
27
+ raise argparse.ArgumentTypeError(
28
+ f"The file {filename} could not be parsed as JSON."
29
+ )
30
+
31
+#-----------------------------------------------------------------------------
12 32
def parse_arguments():
13 33
parser = argparse.ArgumentParser(
14
- description='Convert JSON data from a Reddit post to Markdown format.'
34
+ description='Convert JSON data from a Reddit post to Markdown format.'
15 35
)
16 36
17 37
parser.add_argument(
18
- 'jsonfile', help='The JSON file containing the Reddit post data.'
38
+ '-j', '--jsonfile', dest="post_data", type=load_json, help="Path to the JSON file to load"
19 39
)
20 40
21 41
parser.add_argument(
22
- '-t',
23
- '--title',
24
- help='Override the post title with a custom title.',
25
- default=None
42
+ '-t',
43
+ '--title',
44
+ help='Override the post title with a custom title.',
45
+ default=None
26 46
)
27 47
28
- args = parser.parse_args()
29
- return args
48
+ return parser.parse_args()
30 49
31 50
##############################################################################
32 51
# Jinja Stuff
33 52
34 53
#-----------------------------------------------------------------------------
35
-def wrap_text(text, width=80):
54
+def wrap_text(text, width=80, prefix=''):
55
+ initial = prefix
56
+ subsequent = prefix
36 57
37
- return textwrap.fill(text, width)
58
+ paragraphs = text.split('\n\n')
59
+
60
+ wrapped = [
61
+ textwrap.fill(
62
+ paragraph,
63
+ width,
64
+ break_long_words=False,
65
+ initial_indent=initial,
66
+ subsequent_indent=subsequent
67
+ ) for paragraph in paragraphs
68
+ ]
69
+
70
+ return f"\n{prefix}\n".join(wrapped)
38 71
39 72
#-----------------------------------------------------------------------------
40
-env = Environment(
41
- loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), '..')),
42
- lstrip_blocks=False,
43
- trim_blocks=False
44
-)
73
+env = Environment(lstrip_blocks=False, trim_blocks=False)
45 74
46 75
env.filters['wrap'] = wrap_text
47 76
48 77
article_j2 = """---
49 78
title: {{ post.title }}
79
+tags:
80
+ - reddit post
50 81
---
51 82
---
52
-{% filter wrap %}
83
+
84
+{% filter wrap -%}
53 85
This is a post I submitted to
54 86
[{{ post.subreddit }}]({{ post.url }})
55 87
with some selected comment threads. If you wish to be credited for your
56 88
comment please let me know and I'll add your name to your comment.
57 89
{% endfilter %}
90
+
58 91
---
59 92
60 93
{{ post.text | wrap }}
94
+
95
+---
96
+{% for comment in comments %}
97
+Comment:
98
+{{ comment.text | wrap(prefix='> ') }}
99
+{% endfor %}
61 100
"""
62 101
63 102
template = env.from_string(article_j2)
... ...
@@ -68,40 +107,38 @@ def extract_post(json):
68 107
data = json[0]['data']['children'][0]['data']
69 108
70 109
post = {
71
- "subreddit": data["subreddit_name_prefixed"],
72
- "url": data["url"],
73
- "title": data["title"],
74
- "text": data["selftext"],
110
+ "subreddit": data["subreddit_name_prefixed"],
111
+ "url": data["url"],
112
+ "title": data["title"],
113
+ "text": data["selftext"],
75 114
}
76 115
77 116
return post
78 117
79 118
#-----------------------------------------------------------------------------
80
-def json_to_markdown(json_data, custom_title=None):
81
-# comments_data = json_data[1]['data']['children']
119
+def extract_comments(json):
120
+ data = json[1]['data']['children']
82 121
83
- post = extract_post(json_data)
84
- if custom_title:
85
- post["title"] = custom_title
122
+ comments = []
86 123
87
-# for comment in comments_data:
88
-# comment_body = textwrap.fill(comment['data']['body'], width=80)
89
-# markdown_content += f"## Comment\n\n{comment_body}\n\n"
90
-#
91
-# return markdown_content
92
-#
93
- markdown = template.render(post=post).strip()
124
+ for d in data:
125
+ c = {"text": d["data"]["body"]}
126
+ comments.append(c)
94 127
95
- return markdown
128
+ return comments
96 129
97 130
#-----------------------------------------------------------------------------
98
-def convert(jsonfile, custom_title=None):
99
- with open(jsonfile, 'r') as json_file:
100
- json_data = json.load(json_file)
101
- markdown_content = json_to_markdown(json_data, custom_title)
102
- print(markdown_content)
131
+def convert(data, title=None):
132
+ post = extract_post(data)
133
+ if title:
134
+ post["title"] = title
135
+
136
+ comments = extract_comments(data)
137
+
138
+ return template.render(post=post, comments=comments).strip()
103 139
104 140
##############################################################################
105 141
if __name__ == "__main__":
106 142
args = parse_arguments()
107
- convert(args.jsonfile, args.title)
143
+ content = convert(args.post_data, args.title)
144
+ print(f"{content}")