More style fixes in documentation scripts

This commit is contained in:
Ivan Blinkov 2018-02-21 21:28:27 +03:00
parent f83bf6bcd5
commit 7e902c0bd6
3 changed files with 40 additions and 37 deletions

View File

@ -14,7 +14,6 @@
# - For not http-links without anchor script logs an error and cuts them from the resulting single-page document. # - For not http-links without anchor script logs an error and cuts them from the resulting single-page document.
import codecs import codecs
import sys import sys
import re import re
@ -29,19 +28,19 @@ if not os.path.exists(sys.argv[1]):
print "Pass language_dir correctly. For example, 'ru'." print "Pass language_dir correctly. For example, 'ru'."
sys.exit(2) sys.exit(2)
#Configuration # Configuration
PROJ_CONFIG = 'mkdocs_'+sys.argv[1]+'.yml' PROJ_CONFIG = 'mkdocs_' + sys.argv[1] + '.yml'
SINGLE_PAGE = sys.argv[1]+'_single_page/index.md' SINGLE_PAGE = sys.argv[1] + '_single_page/index.md'
DOCS_DIR = sys.argv[1]+'/' DOCS_DIR = sys.argv[1] + '/'
# 1. Open mkdocs.yml file and read `pages` configuration to get an ordered list of files # 1. Open mkdocs.yml file and read `pages` configuration to get an ordered list of files
cfg_file = open(PROJ_CONFIG) cfg_file = open(PROJ_CONFIG)
files_to_concatenate=[] files_to_concatenate = []
for l in cfg_file : for l in cfg_file:
if( '.md' in l ) and ('single_page' not in l): if('.md' in l) and ('single_page' not in l):
path = (l[l.index(':')+1:]).strip(" '\n") path = (l[l.index(':') + 1:]).strip(" '\n")
files_to_concatenate.append(path) files_to_concatenate.append(path)
print str(len(files_to_concatenate)) + " files will be concatenated into single md-file.\nFiles:" print str(len(files_to_concatenate)) + " files will be concatenated into single md-file.\nFiles:"
@ -57,30 +56,30 @@ for path in files_to_concatenate:
single_page_file.write('\n\n') single_page_file.write('\n\n')
file = open(DOCS_DIR+path) file = open(DOCS_DIR + path)
#function is passed into re.sub() to process links # function is passed into re.sub() to process links
def link_proc( matchObj ): def link_proc(matchObj):
text, link = matchObj.group().strip('[)').split('](') text, link = matchObj.group().strip('[)').split('](')
if link.startswith('http'): if link.startswith('http'):
return '['+text+']('+link+')' return '[' + text + '](' + link + ')'
else : else:
sharp_pos = link.find('#') sharp_pos = link.find('#')
if sharp_pos > -1: if sharp_pos > -1:
return '['+text+']('+link[sharp_pos:]+')' return '[' + text + '](' + link[sharp_pos:] + ')'
else : else:
print 'ERROR: Link ['+text+']('+link+') in file '+path+' has no anchor. Please provide it.' print 'ERROR: Link [' + text + '](' + link + ') in file ' + path + ' has no anchor. Please provide it.'
#return '['+text+'](#'+link.replace('/','-')+')' # return '['+text+'](#'+link.replace('/','-')+')'
for l in file: for l in file:
#Processing links in a string # Processing links in a string
l = re.sub(r'\[.+?\]\(.+?\)', link_proc, l) l = re.sub(r'\[.+?\]\(.+?\)', link_proc, l)
#Correcting headers levels # Correcting headers levels
if not first_file: if not first_file:
if( l.startswith('#') ): if(l.startswith('#')):
l='#'+l l = '#' + l
else : else:
first_file = False first_file = False
single_page_file.write(l) single_page_file.write(l)

View File

@ -4,12 +4,13 @@
SOURCES_TREE = 'ru' SOURCES_TREE = 'ru'
from os import walk from os import walk
def get_header(filepath): def get_header(filepath):
f = open(filepath) f = open(filepath)
header = '' header = ''
for line in f: for line in f:
if line.startswith('#') : if line.startswith('#'):
# print line # print line
header = line[1:].strip(' \n') header = line[1:].strip(' \n')
break break
@ -17,19 +18,22 @@ def get_header(filepath):
return header return header
pages_file = open("strings_for_pages.txt","w") pages_file = open("strings_for_pages.txt", "w")
md_links_file = open("links_for_md.txt","w") md_links_file = open("links_for_md.txt", "w")
for (dirpath, dirnames, filenames) in walk(SOURCES_TREE): for (dirpath, dirnames, filenames) in walk(SOURCES_TREE):
for filename in filenames : for filename in filenames:
if '.md' not in filename: continue if '.md' not in filename:
continue
header = get_header(dirpath+'/'+filename) header = get_header(dirpath + '/' + filename)
path = dirpath.replace('docs/','')+'/'+filename path = dirpath.replace('docs/', '') + '/' + filename
if filename == 'index.md': pages_file.write("- '" + header + "': " + "'" + path + "'\n") if filename == 'index.md':
else: pages_file.write(" - '" + header + "': " + "'" + path + "'\n") pages_file.write("- '" + header + "': " + "'" + path + "'\n")
else:
pages_file.write(" - '" + header + "': " + "'" + path + "'\n")
md_links_file.write("[" + header + "](" + path + ")\n") md_links_file.write("[" + header + "](" + path + ")\n")

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Gets all the files in SOURCES_TREE directory, shows all level headers for each file and skip or process files by user's selection. # Gets all the files in SOURCES_TREE directory, shows all level headers
# for each file and skip or process files by user's selection.
import os import os
@ -18,7 +19,6 @@ for (dirpath, dirnames, filenames) in os.walk(SOURCES_TREE):
content = f.readlines() content = f.readlines()
f.close() f.close()
# Showing headers structure in md-file # Showing headers structure in md-file
count_lines = 0 count_lines = 0
for l in content: for l in content:
@ -41,12 +41,12 @@ for (dirpath, dirnames, filenames) in os.walk(SOURCES_TREE):
count_lines = 0 count_lines = 0
for l in content: for l in content:
if l.startswith('==='): if l.startswith('==='):
print count_lines, content[count_lines -1], content[count_lines] print count_lines, content[count_lines - 1], content[count_lines]
content[count_lines - 1] = '# ' + content[count_lines - 1] content[count_lines - 1] = '# ' + content[count_lines - 1]
content.pop(count_lines) content.pop(count_lines)
if l.startswith('---'): if l.startswith('---'):
print count_lines, content[count_lines -1], content[count_lines] print count_lines, content[count_lines - 1], content[count_lines]
content[count_lines - 1] = '## ' + content[count_lines - 1] content[count_lines - 1] = '## ' + content[count_lines - 1]
content.pop(count_lines) content.pop(count_lines)