-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
68 lines (59 loc) · 2.08 KB
/
convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import sys
import json
from string import Template
from jekyll import JekyllPage
from pathlib import Path
from slugify import slugify
round_template = Template("""
#### ${index}
{: .prompt }
${prompt}
{: .response }
${response}
""")
def format_round( index: int, prompt: str, response: str ) -> str :
return round_template.substitute(
index = index,
prompt = blockquote( prompt ),
response = blockquote( response ),
)
def blockquote( text:str ) -> str:
lines = text.split('\n')
lines = [ f"> {line}" for line in lines ]
return "\n".join( lines )
def bard_to_md( source_json: dict ) -> str:
rounds = source_json['rounds']
rounds = [
format_round( i + 1, round['prompt'], round['response'] )
for i, round in enumerate( rounds )
]
rounds_str = "\n".join( rounds )
title = source_json['metadata']['title']
date = source_json['metadata']['date']
agent = source_json['metadata']['agent']
front_matter = { 'title': title, 'layout': "post" }
content = f"# {title}\n\n_date:_ {date}\n\n_agent_: {agent}\n\n{rounds_str}"
jp = JekyllPage( front_matter=front_matter, content=content )
return jp.page()
if __name__ == "__main__":
try:
source_type = sys.argv[1]
except IndexError:
print( 'First parameter is model for parsing file content, e.g., "bard"' )
try:
source_path = Path( sys.argv[2] )
with source_path.open() as f:
source = "".join( f.read() )
source = json.loads( source )
except IndexError:
print( 'Second parameter is filename with structure consistent with model' )
if source_type == 'bard':
date = source['metadata']['date']
slug = slugify( source['metadata']['title'] )
post_name = f'{date}-{slug}.md'
archive_name = f'{date}-{slug}.json'
post_path = Path('_posts') / Path(post_name)
archive_path = Path('archive') / Path(archive_name)
with post_path.open( mode='w' ) as f:
f.write( bard_to_md( source ) )
source_path.replace( archive_path )