Commit 97b4322a22f2f3f94e18226935df02c45daa7d39
Make several lines shorter, rearrange small chunks of code
Greg K Nicholson committed on 10/15/2017, 10:52:30 PMParent: 285357d39779c5b508eeb5aa4a655ee87c847a25
Files changed
scuttleblog.py | changed mode from 100644 to 100755 |
scuttleblog.py | |||
---|---|---|---|
@@ -48,36 +48,46 @@ | |||
48 | 48 … | '-c', 'this.value.content.type == "post"', \ | |
49 | 49 … | '-c', 'this.value.content.text != null', \ | |
50 | 50 … | '-c', 'this.value.content.text != ""', \ | |
51 | 51 … | '-c', 'this.value.content.root == null'] | |
52 | - user_posts_stream = subprocess.Popen(user_posts_args, stdout = subprocess.PIPE) | ||
53 | - user_posts_json = subprocess.check_output(json_posts_args, stdin = user_posts_stream.stdout) | ||
52 … | + user_posts_stream = subprocess.Popen(user_posts_args, | ||
53 … | + stdout = subprocess.PIPE) | ||
54 … | + user_posts_json = subprocess.check_output(json_posts_args, | ||
55 … | + stdin = user_posts_stream.stdout) | ||
54 | 56 … | user_posts = json.loads(user_posts_json) | |
55 | 57 … | return user_posts | |
56 | 58 … | ||
57 | 59 … | def define_post_text(text): | |
58 | 60 … | text = text.strip() | |
59 | 61 … | title = text.splitlines()[0] | |
60 | 62 … | if re.match('#', text): | |
61 | - # The first line is a heading. Use it as the title and strip it from the body. | ||
62 | - body = ''.join(text.splitlines(keepends=True)[1:]).strip() \ | ||
63 | - if len(text.splitlines()) > 1 \ | ||
64 | - else '' | ||
63 … | + # The first line is a heading. | ||
64 … | + # Use it as the title and strip it from the body. | ||
65 … | + if len(text.splitlines()) > 1: | ||
66 … | + body = ''.join(text.splitlines(keepends=True)[1:]).strip() | ||
67 … | + else: | ||
68 … | + body = '' | ||
65 | 69 … | else: | |
66 | - # Truncate the first line and use it as the title. Keep everything in the body. | ||
70 … | + # Truncate the first line and use it as the title. | ||
71 … | + # Keep everything in the body. | ||
67 | 72 … | maxlength = 100 | |
68 | 73 … | ellipsis = '...' | |
69 | - title = title if len(title) <= maxlength else title[:maxlength].rsplit(' ', 1)[0] + ellipsis | ||
74 … | + if len(title) > maxlength: | ||
75 … | + title = title[:maxlength].rsplit(' ', 1)[0] + ellipsis | ||
70 | 76 … | body = text | |
71 | 77 … | title = re.sub('^#+\s+', '', title) | |
72 | 78 … | return {'title': title, 'body': body} | |
73 | 79 … | ||
74 | 80 … | def build_post_structure(p): | |
75 | 81 … | post = {} | |
76 | 82 … | post['frontmatter'] = {} | |
77 | 83 … | post['frontmatter']['key'] = p['key'] | |
78 | - post['frontmatter']['title'] = define_post_text(p['value']['content']['text'])['title'] | ||
79 | - post['frontmatter']['date'] = datetime.datetime.fromtimestamp(int(p['value']['timestamp'] / 1000), datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ') | ||
84 … | + post['frontmatter']['title'] = define_post_text( | ||
85 … | + p['value']['content']['text'])['title'] | ||
86 … | + time = datetime.datetime.fromtimestamp(int(p['value']['timestamp'] | ||
87 … | + / 1000), | ||
88 … | + datetime.timezone.utc) | ||
89 … | + post['frontmatter']['date'] = time.strftime('%Y-%m-%dT%H:%M:%SZ') | ||
80 | 90 … | post['frontmatter']['sequence'] = p['value']['sequence'] | |
81 | 91 … | post['body'] = define_post_text(p['value']['content']['text'])['body'] | |
82 | 92 … | return (post) | |
83 | 93 … | ||
@@ -89,9 +99,8 @@ | |||
89 | 99 … | return content | |
90 | 100 … | ||
91 | 101 … | def define_post_filename(post): | |
92 | 102 … | folder = 'hugo/content/posts' | |
93 | - #slug = post['key'].replace('+','-').replace('/','_').replace('=.sha256','') | ||
94 | 103 … | slug = str(post['frontmatter']['sequence']) | |
95 | 104 … | filetype = 'md' | |
96 | 105 … | return folder + '/' + slug + '.' + filetype | |
97 | 106 … | ||
@@ -106,19 +115,27 @@ | |||
106 | 115 … | write_post_file(build_post_structure(post)) | |
107 | 116 … | ||
108 | 117 … | ||
109 | 118 … | def get_user_metadata(ssb_userid): | |
110 | - user_metadata_args = ['sbot', 'links', '--source', ssb_userid, '--dest', ssb_userid, '--rel', 'about', '--values'] | ||
111 | - json_metadata_args = ['json', '--deep-merge', '-c', 'this.value.content.type == "about"'] | ||
112 | - user_metadata_stream = subprocess.Popen(user_metadata_args, stdout = subprocess.PIPE) | ||
113 | - user_metadata_json = subprocess.check_output(json_metadata_args, stdin = user_metadata_stream.stdout) | ||
119 … | + user_metadata_args = ['sbot', 'links', | ||
120 … | + '--source', ssb_userid, | ||
121 … | + '--dest', ssb_userid, | ||
122 … | + '--rel', 'about', | ||
123 … | + '--values'] | ||
124 … | + json_metadata_args = ['json', '--deep-merge', | ||
125 … | + '-c', 'this.value.content.type == "about"'] | ||
126 … | + user_metadata_stream = subprocess.Popen(user_metadata_args, | ||
127 … | + stdout = subprocess.PIPE) | ||
128 … | + user_metadata_json = subprocess.check_output(json_metadata_args, | ||
129 … | + stdin = user_metadata_stream.stdout) | ||
114 | 130 … | user_metadata = json.loads(user_metadata_json) | |
115 | 131 … | return user_metadata | |
116 | 132 … | ||
117 | 133 … | def check_hugo_theme(theme, theme_clone_url): | |
118 | 134 … | hugo_theme_dir = os.path.join(scuttleblog_dir, 'hugo', 'themes', theme) | |
119 | 135 … | if not os.path.isdir(hugo_theme_dir): | |
120 | - subprocess.run(['git', 'submodule', 'add', '-f', theme_clone_url, hugo_theme_dir]) | ||
136 … | + subprocess.run(['git', 'submodule', 'add', '-f', | ||
137 … | + theme_clone_url, hugo_theme_dir]) | ||
121 | 138 … | ||
122 | 139 … | def build_hugo_config(m): | |
123 | 140 … | hugo_config = {} | |
124 | 141 … | hugo_config['baseurl'] = conf.hugo_baseurl | |
@@ -126,9 +143,10 @@ | |||
126 | 143 … | if 'name' in m['value']['content']: | |
127 | 144 … | hugo_config['title'] = m['value']['content']['name'] | |
128 | 145 … | hugo_config['params'] = {} | |
129 | 146 … | if 'description' in m['value']['content']: | |
130 | - hugo_config['params']['subtitle'] = m['value']['content']['description'].replace('\n', ' ') | ||
147 … | + description = m['value']['content']['description'] | ||
148 … | + hugo_config['params']['subtitle'] = description.replace('\n', ' ') | ||
131 | 149 … | return (hugo_config) | |
132 | 150 … | ||
133 | 151 … | def write_hugo_config_from_user(ssb_userid): | |
134 | 152 … | metadata = get_user_metadata(ssb_userid) |
Built with git-ssb-web