1
- import sys
2
1
import argparse
2
+ import sys
3
3
from time import strftime
4
4
import yaml
5
-
6
5
from . import zdb , zettel , zquery
7
6
from .zutils import *
8
7
@@ -16,6 +15,10 @@ def get_argparse():
16
15
action = 'store_const' , const = True , default = False ,
17
16
help = "include field <%s> in output" % field )
18
17
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
+
19
22
parser .add_argument ('--count' , action = 'store_const' , const = True ,
20
23
default = False , help = "Show number of Zettels matching this search" )
21
24
@@ -42,6 +45,7 @@ def get_argparse():
42
45
parser .add_argument (
43
46
'--get-all-mentions' , help = "show all mentions in this database (disables all other options)" , action = "store_true" , default = False )
44
47
48
+
45
49
return parser
46
50
47
51
@@ -87,6 +91,20 @@ def main():
87
91
88
92
search_count = 0
89
93
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
90
108
for row in gen :
91
109
search_count = search_count + 1
92
110
printed_something = False
@@ -146,5 +164,66 @@ def main():
146
164
else :
147
165
print ("Filename %s exists; will not overwrite." % args .stats )
148
166
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
+
149
228
if __name__ == '__main__' :
150
229
main ()
0 commit comments