Skip to content

Commit

Permalink
toc dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
rbmonster committed Jul 1, 2022
1 parent ef58c9d commit 2c96455
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 126 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
- [拦截器与过滤器](/src/main/java/com/toc/FILTERANDINTERCEPTOR.md)
- [Spring Boot加载流程](/src/main/java/com/toc/SPRINGBOOT.md)
- [Spring Security](/src/main/java/com/toc/SPRING-SECURITY.md)
- [Spring Sharding-JDBC](/src/main/java/com/toc/SPRING-SECURITY.md)
- [Spring Sharding-JDBC](/src/main/java/com/four/SHARDING_JDBC.md)

## 中间件
- [Redis](/src/main/java/com/toc/REDIS.md)
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<artifactId>netty-all</artifactId> <!-- Use 'netty-all' for 4.0 or above -->
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>markdown-toc</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
125 changes: 0 additions & 125 deletions src/main/java/com/FLINK.md

This file was deleted.

135 changes: 135 additions & 0 deletions src/main/java/com/TOC-v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import sys
import os
import time

headline = ['#','##','###','####','#####','######']
lines_in_file = []
lineNum = [0,0,0,0,0,0]

"""生成目录列表中的某一项"""
def creat_directory_line(line,headline_mark,i):
if headline_mark == '#':
lineNum[0]+=1
lineNum[1]=0
prefix = ''
# return '<a href="#' + str(i) + '">' + prefix + line[2:-1] + "</a> \n"
return '* [' + prefix + line[2:-1] + '](#'+ line[2:-1] + '[[Top]](#index)'+") \n"
elif headline_mark == '##':
#&emsp;为Markdown中的一种缩进,这里不直接用空格作为缩进是因为多个空格一起出现可能会生成代码块,引发歧义
lineNum[1]+=1
lineNum[2]=0
prefix = str(lineNum[1])+". "
# return '&emsp;<a href="#' + str(i) + '">' + prefix + line[3:-1] + "</a> \n"
return ' * [' + prefix + line[3:-1] + '](#' + line[3:-1] + '[[Top]](#index)'+") \n"
elif headline_mark == '###':
lineNum[2]+=1
lineNum[3]=0
prefix = str(lineNum[1])+"."+str(lineNum[2])+'. '
# return '&emsp;&emsp;<a href="#' + str(i) + '">' +prefix + line[4:-1] + "</a> \n"
return ' * [' +prefix + line[4:-1]+ '](#' + line[4:-1]+ ") \n"
elif headline_mark == '####':
lineNum[3]+=1
lineNum[4]=0
prefix = str(lineNum[1])+"."+str(lineNum[2])+"."+str(lineNum[3])+'. '
return ' * [' +prefix + line[5:-1] + '](#' +line[5:-1] + ") \n"
elif headline_mark == '#####':
lineNum[4]+=1
lineNum[5]=0
prefix = str(lineNum[1])+"."+str(lineNum[2])+"."+str(lineNum[3])+"."+str(lineNum[4])+'. '
return ' * [' + prefix + line[6:-1]+ '](#' + line[6:-1]+") \n"
elif headline_mark == '######':
lineNum[5]+=1
prefix = str(lineNum[1])+"."+str(lineNum[2])+"."+str(lineNum[3])+"."+str(lineNum[4])+"."+str(lineNum[5])+'. '
return ' * [' + prefix + line[7:-1]+ '](#' + line[7:-1] + ") \n"

"""生成目录列表"""
def creat_directory(f):
i = 0
directory = []
directory.append('<a name="index">**Index**</a>\n\n')
for line in f:
lines_in_file.append(line)
f.close()
length = len(lines_in_file)
for j in range(length):
splitedline = lines_in_file[j].lstrip().split(' ')
if splitedline[0] in headline:
#如果为最后一行且末尾无换行(防最后一个字被去除)
if j == length - 1 and lines_in_file[j][-1] != '\n':
directory.append(creat_directory_line(lines_in_file[j] + '\n',splitedline[0],i) + '\n')
lines_in_file[j] = lines_in_file[j].replace(splitedline[0] + ' ',splitedline[0] +' ' )[:] + '[[Top]](#index)' + "\n"
i = i + 1
else:
directory.append(creat_directory_line(lines_in_file[j],splitedline[0],i))
lines_in_file[j] = lines_in_file[j].replace(splitedline[0] + ' ', splitedline[0]+' ' )[:-1] + '[[Top]](#index)' + "\n"
i = i + 1
lineNum = [0,0,0,0,0,0]
return directory

"""以目录列表为参数生成添加目录的文件"""
def creat_file_with_toc(f, file_name):
directory = creat_directory(f)
file_with_toc = os.getcwd() + os.sep +file_name
print(file_with_toc)
if not os.path.exists(file_with_toc):
with open(file_with_toc, 'w+',encoding='utf-8') as f:
for directory_line in directory:
f.write(directory_line)
for line in lines_in_file:
f.write(line)
lines_in_file.clear()
print('文件已生成')
else:
print('文件名重复,请修改文件'+file_with_toc+'.md 的文件名后重试')

result=[]

def search(path="", name=""):
for item in os.listdir(path):
item_path = os.path.join(path, item)
if os.path.isdir(item_path):
search(item_path, name)
elif os.path.isfile(item_path) and item_path.find('toc') == -1:
if name in item:
global result
result.append(item_path)

def generateFile(file_name):
if os.path.exists(file_name) and os.path.isfile(file_name):
with open(file_name,'r',encoding='utf-8') as f:
newFileName = os.path.basename(file_name)
creat_file_with_toc(f, newFileName)
else:
msg = "未找到文件"
print(msg)

if __name__=='__main__':
file_name = ''
#如果未传入文件名
if len(sys.argv) < 2:
path = os.getcwd()
file_and_dir = os.listdir(path)
md_file = []
for item in file_and_dir:
if item.split('.')[-1].lower() in ['md','mdown','markdown'] and os.path.isfile(item):
md_file.append(item)
if len(md_file) != 0:
print('当前目录下的Markdown文件:')
for file in md_file:
print(file)
file_name = input('请输入文件名(含后缀)\n')
else:
print('该目录下无Markdown文件,即将退出...')
time.sleep(2)
os._exit(0)
else:
file_name = sys.argv[1]
if 'all' == file_name:
path = os.getcwd()
search(path,'md')
print(os.path.exists(str(result[1]).strip()))
for i in range(len(result)):
print("序号:%s 值:%s" % ( i , result[i] ))
generateFile(result[i])
else:
generateFile(file_name)
Loading

0 comments on commit 2c96455

Please sign in to comment.