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

View File

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

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python
# -*- 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
@ -18,7 +19,6 @@ for (dirpath, dirnames, filenames) in os.walk(SOURCES_TREE):
content = f.readlines()
f.close()
# Showing headers structure in md-file
count_lines = 0
for l in content:
@ -41,12 +41,12 @@ for (dirpath, dirnames, filenames) in os.walk(SOURCES_TREE):
count_lines = 0
for l in content:
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.pop(count_lines)
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.pop(count_lines)