Skip to content

Commit 7f841d2

Browse files
author
George K. Thiruvathukal
committed
templating system for published view of a zfind
1 parent 2458e3c commit 7f841d2

File tree

1 file changed

+81
-2
lines changed

1 file changed

+81
-2
lines changed

zettelgeist/zfind.py

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import sys
21
import argparse
2+
import sys
33
from time import strftime
44
import yaml
5-
65
from . import zdb, zettel, zquery
76
from .zutils import *
87

@@ -16,6 +15,10 @@ def get_argparse():
1615
action='store_const', const=True, default=False,
1716
help="include field <%s> in output" % field)
1817

18+
parser.add_argument('--publish', help="use template to publish zfind output (suppresses all --show-FIELD)", default=None)
19+
20+
parser.add_argument('--publish-conf', help="use publish configuration file to format special fields", default=None)
21+
1922
parser.add_argument('--count', action='store_const', const=True,
2023
default=False, help="Show number of Zettels matching this search")
2124

@@ -42,6 +45,7 @@ def get_argparse():
4245
parser.add_argument(
4346
'--get-all-mentions', help="show all mentions in this database (disables all other options)", action="store_true", default=False)
4447

48+
4549
return parser
4650

4751

@@ -87,6 +91,20 @@ def main():
8791

8892
search_count = 0
8993
match_filenames = []
94+
95+
# This is for the --publish option. Publish suppresses all other output.
96+
97+
if args.publish:
98+
template_file = args.publish
99+
publish_conf = args.publish_conf
100+
for row in gen:
101+
loader = zettel.ZettelLoader(row['filename'])
102+
zettels = loader.getZettels()
103+
z = next(zettels)
104+
publish(row, z, template_file, publish_conf)
105+
return
106+
107+
# This is for the --show options
90108
for row in gen:
91109
search_count = search_count + 1
92110
printed_something = False
@@ -146,5 +164,66 @@ def main():
146164
else:
147165
print("Filename %s exists; will not overwrite." % args.stats)
148166

167+
def publish(row, z, template_file, publish_conf):
168+
import json
169+
170+
formatting = {}
171+
if publish_conf:
172+
with open(publish_conf) as jsonf:
173+
formatting = json.load(jsonf)
174+
175+
all_vars = { k:'' for k in zettel.ZettelFields }
176+
all_vars.update(z.zettel)
177+
all_vars['filename'] = row['filename']
178+
179+
180+
181+
tags = reformat_tags(all_vars, formatting)
182+
mentions = reformat_mentions(all_vars, formatting)
183+
cite = reformat_cite(all_vars, formatting)
184+
185+
reformatted_data = { 'tags' : tags, 'mentions' : mentions, 'cite' : cite }
186+
187+
all_vars.update(reformatted_data)
188+
189+
with open(template_file) as tf:
190+
text = tf.read()
191+
print(text % all_vars)
192+
193+
def reformat_tags(all_vars, formatting):
194+
tags_format = formatting.get('tags', {})
195+
tags = all_vars.get('tags', [])
196+
if len(tags) == 0:
197+
return ''
198+
default_tag_format = '%(tag)s'
199+
formatted_tags = \
200+
[ tags_format.get('tag', default_tag_format) % { 'tag' : tag } for tag in tags ]
201+
return tags_format.get('before','') + ''.join(formatted_tags) + tags_format.get('after','')
202+
203+
def reformat_mentions(all_vars, formatting):
204+
mentions_format = formatting.get('mentions', {})
205+
mentions = all_vars.get('mentions', [])
206+
if len(mentions) == 0:
207+
return ''
208+
default_mention_format = '%(mention)s'
209+
formatted_mentions = \
210+
[ mentions_format.get('mention', default_mention_format) % { 'mention' : mention } for mention in mentions ]
211+
return mentions_format.get('before','') + ''.join(formatted_mentions) + mentions_format.get('after','')
212+
213+
def reformat_cite(all_vars, formatting):
214+
cite_format = formatting.get('cite', {})
215+
cite = all_vars.get("cite", {})
216+
if len(cite) == 0:
217+
return ''
218+
if len(cite) > 0 and type(cite_format) == type({}):
219+
cite_template = cite_format.get('before')
220+
cite_template += cite_format.get('bibkey', '%(bibkey)s')
221+
cite_template += cite_format.get('page', '%(page)s')
222+
cite_template += cite_format.get('after')
223+
new_cite = cite_template % cite
224+
return new_cite
225+
else:
226+
return ''
227+
149228
if __name__ == '__main__':
150229
main()

0 commit comments

Comments
 (0)