-
Notifications
You must be signed in to change notification settings - Fork 7
/
bookmanager-markdown.py
executable file
·139 lines (114 loc) · 3.99 KB
/
bookmanager-markdown.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#! /usr/bin/env python
import click
import textwrap
from cloudmesh.common.Shell import Shell
from cloudmesh.common.util import path_expand
import os
import sys
import shutil
def bibtex(file):
os.system(f"pandoc -F pandoc-crossref -F pandoc-citeproc -t commonmark -s --bibliography book/bib/devops.bib --csl bib/ieee-url.csl {file} -o ./tmp.md")
os.system(f"mv ./tmp.md {file}")
def metadata (title, weight, description=None):
if description is None:
data = textwrap.dedent(
f"""
---
title: {title}
weight: {weight}
---
""").strip()
else:
data = textwrap.dedent(
f"""
---
title: {title}
weight: {weight}
description: >
{description}
---
""").strip()
return data
'''
converter = [
{
"file": "book/chapters/devops/devop-ci.md",
"to": "./content/en/modules/devops/devop-ci.md",
"old": "# DevOps - Continuous Improvement",
"new": """
---
title: DevOps - Continuous Improvement
---"""
}
'''
@click.command()
@click.option('--weight', default=100, help='the weight of the entry.')
@click.option('--title', default=None, help='the title of the entry.')
@click.option('--content', default="content/en", help='the content directory base.')
@click.option('--description', default=None, help='the description.')
@click.argument('filename')
def convert(weight, title, content, description, filename):
"""convert the given bookmanager markdown to a hugo markdown with
bibtex and metadata.
"""
d = False
filename = filename.replace("./", "")
if d:
file_to = filename.replace("book/chapters/", "").replace(".md","")
directory = f"./content/en/modules/{file_to}"
destination = f"{directory}/_index.md"
else:
file_to = filename.replace("book/chapters/", "")
directory = os.path.dirname(f"./content/en/modules/{file_to}")
destination = f"./content/en/modules/{file_to}"
print (filename, "->", destination)
#
# copy file
#
Shell.mkdir(directory)
content = open(filename, 'r').read()
with_bib = "[@" in content
if with_bib:
content = content + "\n\n## Refernces\n\n"
with open(destination, 'w') as f:
f.write(content)
#
# apply bibtex
#
if with_bib:
bibtex(destination)
content = open(destination, 'r').read().split("\n")
#print (content)
for i in range(0, len(content)):
if "![" in content[i] and '(images' in content[i]:
content[i] = content[i].replace("(images","(../images")
elif "![" in content[i] and '(./images' in content[i]:
content[i] = content[i].replace("(./images","(../images")
elif '<img src="images' in content[i]:
print ("OOOOO", content[i])
content[i] = content[i].replace("images","../images")
print ("WWWWW", content[i])
elif '<img src="./images' in content[i]:
content[i] = content[i].replace("./images","../images")
# content = t
# print ("JJJJ")
#if '.\/images' in content:
# content.replace(".\/images", "..\/images")
if content[0].startswith("#"):
title = content[0].replace("# ", "").strip()
content[0] = metadata(title, weight, description)
content = "\n".join(content)
elif content[1].startswith("==="):
title = content[0].strip()
content[1] = ""
content[0] = metadata(title, weight, description)
content = "\n".join(content)
elif content[0].startswith("---"):
sys.exit("File in wrong markdown format. detected metadata")
else:
sys.exit("First line has no title of metadata")
with open(destination, 'w') as f:
f.write(content)
#print (">>>",directory)
if __name__ == '__main__':
convert()