-
Notifications
You must be signed in to change notification settings - Fork 2
/
bookdownparse.py
165 lines (139 loc) · 5.51 KB
/
bookdownparse.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""Generate README from Fan Rmd Frontmatter
The :mod:`pyfan.util.rmd.bookdownparse` generates rmd frontmatter.
Generates README.md TOC contents based on parsed YAML frontmatter from files listed
in a bookdown yaml file that contains a list of RMD files to process through.
Includes method :func:`fs_yml2readme`
"""
import yaml
import frontmatter
import pyfan.util.rmd.rmdparse as rmdparse
import os
def fs_yml2readme(sfc_prj='R4Econ',
sph_prj='C:/Users/fan/R4Econ/',
spn_prj_bookdown_yml='_bookdown.yml',
spn_prj_readme_toc='README_toc.md',
ls_st_ignore=['index.Rmd', 'README_appendix.md', 'title.Rmd', 'main.Rmd'],
sph_pdf='htmlpdfr',
sph_html='htmlpdfr',
sph_r='htmlpdfr',
st_file_type='r',
verbose=False):
"""Write to file README detailed TOC for files in bookdown yaml list
Parameters
----------
sfc_prj : string
The git project name
sph_prj : string
the local path to the git project
spn_prj_bookdown_yml : string
yml file name under project root contains rmd names under 'rmd_files' key
spn_prj_readme_toc : string
md generated file name under project root
ls_st_ignore: list
list of string names to ignore
sph_pdf : string
subfolder to store pdf files in the rmd folder
sph_html : string
subfolder to store html files in the rmd folder
sph_r : string
subfolder to store r files in the rmd folder does not
have to be r, any other raw file type, m of py for example
st_file_type: string
the RMD file is for which underlying language: r for R, m for matlab, py
for python
verbose: bool
print details
Returns
-------
None
nothing is returned, the spn_prj_readme_toc toc file is generated
Examples
--------
>>> fs_yml2readme(sfc_prj='pyfan', sph_prj='../../../', verbose=False)
"""
spn_readme = sph_prj + spn_prj_readme_toc
f_core = open(spn_readme, "w")
spn_yml = sph_prj + spn_prj_bookdown_yml
with open(spn_yml, 'r') as f:
ob_book_yml = yaml.load(f, Loader=yaml.BaseLoader)
ls_st_rmd_files = ob_book_yml['rmd_files']
sfc_main = 'main.Rmd'
ctr_chapter = 0
ctr_section = 0
ctr_subsection = 0
for sfc in ls_st_rmd_files:
if sfc in ls_st_ignore:
if verbose:
print('ignore:' + sfc)
elif sfc_main in sfc:
# Load _main File
spn_rmd_main = sph_prj + sfc
ob_yml_rmd_main = frontmatter.load(spn_rmd_main)
ob_yml_rmd_main = ob_yml_rmd_main.content + '\n\n'
# section heading with proper numbering
it_pound = ob_yml_rmd_main.count("#")
if it_pound == 1:
ctr_chapter = ctr_chapter + 1
ctr_section = 0
ctr_subsection = 0
st_text_out = '# ' + str(ctr_chapter) + ' ' + \
ob_yml_rmd_main.replace("#", "")
if ctr_chapter != 1:
st_text_out = '\n' + st_text_out
elif it_pound == 2:
ctr_section = ctr_section + 1
ctr_subsection = 0
st_text_out = '## ' + str(ctr_chapter) + '.' + str(ctr_section) + \
' ' + ob_yml_rmd_main.replace("#", "")
if ctr_section != 1:
st_text_out = '\n' + st_text_out
# write to file
if verbose:
print(st_text_out)
f_core.write(st_text_out)
else:
# Load RMD front matter and summarize
ctr_subsection = ctr_subsection + 1
st_head_link, ls_desc_out, ls_code_out = \
rmdparse.fs_rmd_yml_parse(
sfc_prj=sfc_prj, sph_prj=sph_prj, spn_prj_rmd=sfc,
sph_pdf=sph_pdf,
sph_html=sph_html,
sph_r=sph_r,
st_file_type=st_file_type)
# write to file file title and main
st_text_out = str(ctr_subsection) + '. ' + st_head_link + '\n'
if verbose:
print(st_text_out)
f_core.write(st_text_out)
# write to file file desc
for st_desc in ls_desc_out:
f_core.write(st_desc + '\n')
for st_code in ls_code_out:
f_core.write(st_code + '\n')
# ls_front_keys = ob_book_yml.keys()
# print(ls_front_keys)
f_core.close()
# if __name__ == '__main__':
# # sfc_prj = 'R4Econ'
# # sph_prj = 'C:/Users/fan/R4Econ/'
# # spn_prj_bookdown_yml = '_bookdown.yml'
# # spn_prj_readme_toc = 'README_toc_two.md'
# #
# # fs_yml2readme(sfc_prj=sfc_prj,
# # sph_prj=sph_prj,
# # spn_prj_bookdown_yml=spn_prj_bookdown_yml,
# # spn_prj_readme_toc=spn_prj_readme_toc)
#
# # fs_yml2readme(sfc_prj='pyfan', sph_prj='../../../', verbose=False)
#
# fs_yml2readme(sfc_prj='Math4Econ',
# sph_prj='C:/Users/fan/Math4Econ/',
# spn_prj_bookdown_yml='_bookdown.yml',
# spn_prj_readme_toc='README_toc.md',
# ls_st_ignore=['index.Rmd', 'README_appendix.md', 'title.Rmd', 'main.Rmd'],
# sph_pdf='htmlpdfm',
# sph_html='htmlpdfm',
# sph_r='htmlpdfm',
# st_file_type='m',
# verbose=True)