Skip to content

Commit 86a899d

Browse files
committed
Mege with main
2 parents 9b3971a + 04f0d74 commit 86a899d

File tree

1 file changed

+54
-50
lines changed

1 file changed

+54
-50
lines changed

mk_docs.py

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,34 @@
1818
# - add header, navigation and footer to the converted file
1919
def md2html(md_file, html_file, file_name):
2020
print("Transforming " + md_file + " into " + html_file)
21-
21+
2222
with open(md_file, 'r') as f:
2323
text = f.read()
2424

2525
html = pymd.convert(text)
2626
soup = BeautifulSoup(html, 'html.parser')
2727
for a in soup.findAll('a'):
28-
28+
2929
if not a['href'].startswith(('http://', 'https://', '#')):
30-
if not a['href'].endswith(('html')):
31-
href = a['href']
32-
# Handle case when href is a link to another markdown file (commonly used in repositories).
33-
if href.endswith('.md'):
34-
href = href[:-3]
35-
a['href'] = href + '.html'
36-
30+
parts = a['href'].split('#')
31+
href = parts[0]
32+
if not href.endswith(('html')):
33+
# Handle case when href is a link to another markdown file (commonly used in repositories).
34+
if href.endswith('.md'):
35+
href = href[:-3]
36+
a['href'] = href + ".html"
37+
38+
if (len(parts) > 1):
39+
a['href'] = a['href'] + '#' + parts[1]
40+
3741
if (str(a['href']).find(":") > 0):
3842
a['href'] = a['href'].replace(":", "/")
39-
43+
4044
# TODO: At this point we could use title and description to auto generate a breadcrum or an index
4145
#title = soup.find('h1').string
4246
#if (len(title.split(":")) > 1):
4347
# title = title.split(":")[1]
44-
48+
4549
#desc = ""
4650
#p = soup.find('p')
4751
#if not p is None:
@@ -52,7 +56,7 @@ def md2html(md_file, html_file, file_name):
5256

5357
# search a filesystem directory for subdirs and retum these in a list
5458
def getsubdirs(dir_path):
55-
return(glob.glob(dir_path + '*/'))
59+
return(glob.glob(dir_path + '*/'))
5660

5761
# search a filesystem directory for markdown (md) files and parse these into html using the md2html function
5862
def parsefiles(docsdir, outputdir):
@@ -75,38 +79,38 @@ def parsefiles(docsdir, outputdir):
7579
# filter out the ones that do not have a ssp module
7680
def getmodulerepos():
7781
module_repos = []
78-
82+
7983
with urllib.request.urlopen("https://api.github.com/users/simplesamlphp/repos?per_page=100") as url:
8084
repos = json.loads(url.read().decode())
81-
85+
8286
for repo in repos:
8387
a_repo = {"name": [], "description": [], "html_url": [], "short_name": []}
84-
85-
# we assume all module will have a name that starts with 'simplesamlphp-module-'
88+
89+
# we assume all module will have a name that starts with 'simplesamlphp-module-'
8690
if (repo['name'].find('simplesamlphp-module-') == 0 and not repo['archived']):
8791
a_repo['name'] = str(repo['name'])
8892
a_repo['description'] = str(repo['description'])
8993
a_repo['html_url'] = str(repo['html_url'])
9094
a_repo['short_name'] = a_repo['name'].split("-")[2]
9195

92-
module_repos.append(a_repo)
93-
96+
module_repos.append(a_repo)
97+
9498
return module_repos
9599

96100
# clone a specific git repo to a given directory. Optionally fetch specific version
97101
# Target directories will be autocreated
98102
def getgitrepo(repo, repo_clone_dir, repo_root, version=None):
99103
os.makedirs(os.path.join(repo_clone_dir))
100104
os.chdir(repo_clone_dir)
101-
105+
102106
if (version is None or version == 'devel'):
103107
os.system('git clone --depth=1 ' + repo)
104108
else:
105109
os.system('git clone --depth=1 --branch simplesamlphp-' + version + ' ' + repo)
106110
os.chdir(repo_clone_dir + repo_root)
107-
111+
108112
print("Working in git repo from" + os.getcwd())
109-
113+
110114
os.system('git status')
111115

112116
# make the header and headerbad div contents for indjection into each documentation page
@@ -141,14 +145,14 @@ def mkContentHeader(versions):
141145
content += '</nav>'
142146
content += '<div id="content">'
143147
content += '</header>'
144-
145-
s = BeautifulSoup(content, 'html.parser')
146-
148+
149+
s = BeautifulSoup(content, 'html.parser')
150+
147151
return s.prettify()
148152

149-
# make a navigation structure based on the versions we have doucmentation for
153+
# make a navigation structure based on the versions we have doucmentation for
150154
def mkNavigation(versions):
151-
155+
152156
#content = '<div id="langbar" style="clar: both"><div id="navigation">Documentation is available for the following versions: '
153157
#for version in versions:
154158
# content += '<a href="/docs/'+version+'/index.html">'+version+'</a> | '
@@ -159,24 +163,24 @@ def mkNavigation(versions):
159163
if version == 'devel':
160164
content += '<div class="menuitem first">'
161165
else:
162-
content += '<div class="menuitem">'
166+
content += '<div class="menuitem">'
163167

164168
content += '<a href="'+site_base_path+version+'/index.html">'+version
165169
if version == versions[0]:
166170
content += ' (stable)'
167171
content += '</a></div>'
168-
172+
169173
content += ' <div class="menuitem last">'
170174
content += ' <a href="' + site_base_path + 'contributed_modules.html"> Contributed modules</a>'
171175
content += ' </div>'
172-
176+
173177
content += '</div>'
174-
178+
175179
return content
176180

177181
# make sure some resources are put in the right place for the website
178182
def mkResources(root_dir, web_root):
179-
# starter index.html (just a redirect to 'stable')
183+
# starter index.html (just a redirect to 'stable')
180184
os.system('cp ' + root_dir + 'resources/index.html ' + web_root + 'index.html ')
181185

182186
# Builds an index.md file of all the contributed repository documetation (if available)
@@ -185,20 +189,20 @@ def mkcontribmodsindex(contrib_mods, module_index_file, contrib_mods_files):
185189
module_index += "===========================\n\n"
186190

187191
pages = {}
188-
192+
189193
for page in contrib_mods_files:
190194
s = page.split("/")
191195
mod_name = s[len(s) -2]
192196
page_name = s[len(s) -1]
193-
197+
194198
if mod_name not in pages.keys():
195199
pages[mod_name] = []
196200

197201
pages[mod_name].append(page_name)
198-
202+
199203
for module in contrib_mods:
200204
module_index += " * "+ module["name"] + "\n"
201-
205+
202206
if module["description"] is not None:
203207
module_index += ": " + module["description"] + "\n"
204208

@@ -208,30 +212,30 @@ def mkcontribmodsindex(contrib_mods, module_index_file, contrib_mods_files):
208212
for page in pages[module["short_name"]]:
209213
module_index += " * Documentation: ["+ page + "](contrib_modules/"+ module["short_name"] + "/" + page +")\n"
210214
except KeyError:
211-
# some modules do not have documentation, just ignore them
215+
# some modules do not have documentation, just ignore them
212216
pass
213-
217+
214218
with open(module_index_file, 'w+') as f:
215219
f.write(module_index)
216220

217221
# reads files and (sub)dirs from a given directory
218222
def getListOfFiles(dirName):
219-
# create a list of file and sub directories
220-
# names in the given directory
223+
# create a list of file and sub directories
224+
# names in the given directory
221225
listOfFile = os.listdir(dirName)
222226
allFiles = list()
223227
# Iterate over all the entries
224228
for entry in listOfFile:
225229
# Create full path
226230
fullPath = os.path.join(dirName, entry)
227-
# If entry is a directory then get the list of files in this directory
231+
# If entry is a directory then get the list of files in this directory
228232
if os.path.isdir(fullPath):
229233
allFiles = allFiles + getListOfFiles(fullPath)
230234
else:
231235
allFiles.append(fullPath)
232-
236+
233237
return allFiles
234-
238+
235239
################################################
236240
#
237241
# MAIN
@@ -280,7 +284,7 @@ def getListOfFiles(dirName):
280284
for ssp_version in ssp_versions:
281285
print("Working on: " + ssp_version)
282286
#print("Repo Root: " + repo_root_dir)
283-
287+
284288
version_dir = tempdir + ssp_version + "/"
285289
#print("Version dir: " + version_dir)
286290
getgitrepo('https://github.com/simplesamlphp/simplesamlphp.git', version_dir, repo_root_dir, ssp_version)
@@ -310,22 +314,22 @@ def getListOfFiles(dirName):
310314
print("Working on: " + module["name"])
311315
contrib_mod_dir = tempdir + "contrib_modules/" + module["name"] + "/"
312316
contrib_mod_web_dir = site_root_dir + "contrib_modules" + "/"
313-
314-
# We assume contributes modules live in the ssp repo and are named 'simplesamlphp-module-*'
317+
318+
# We assume contributes modules live in the ssp repo and are named 'simplesamlphp-module-*'
315319
getgitrepo(module["html_url"], contrib_mod_dir, module["name"])
316-
320+
317321
module_dir = os.path.join(contrib_mod_dir, module["name"], "docs/")
318322
print(module_dir)
319-
323+
320324
if os.path.isdir(module_dir):
321325
module_output_dir = os.path.join(contrib_mod_web_dir, module["short_name"]+ "/")
322326
parsefiles(module_dir, module_output_dir)
323-
327+
324328
else:
325329
print("No docs found for '" + module["name"] +"'")
326-
330+
327331
# Now build an index of generated documents
328-
contrib_mods_files= getListOfFiles(site_root_dir + "contrib_modules" + "/")
332+
contrib_mods_files= getListOfFiles(site_root_dir + "contrib_modules" + "/")
329333
mkcontribmodsindex(contrib_mods, module_index_file, contrib_mods_files)
330334
md2html(module_index_file, site_root_dir + 'contributed_modules.html', 'contributed_modules.html')
331335

0 commit comments

Comments
 (0)