import os
import json
basePath = r'D:\study\blog\blog\docs'
outFilePath = r'D:\study\blog\blog\docs\.vuepress\config'
prefixStr = 'export default '
fzname = 'sidebarConf.ts'
charBase=r'/'
def cwdDir(basePath):
parentNode = dict({})
pdir = os.listdir(basePath)
for i in pdir:
fullPath=os.path.join(basePath,i)
if i.find('.') == -1:
apath=os.path.join(basePath,i)
if os.path.isdir(apath):
jpathA = charBase+(i)
twoPath = os.listdir(apath)
if jpathA.find('hhh') != -1:
continue
for tp in twoPath:
jpathB = jpathA+charBase+tp
fpath = os.path.join(apath,tp)
parentValue = []
pvNode = dict({'title':tp.upper(),'text':tp.upper(),'collapsable':False,'sidebarDepth':1 })
if os.path.isdir(fpath):
childArr = []
for fs in os.listdir(fpath):
if fs.find('image') == -1:
childArr.append(fs)
pvNode['children'] = childArr
parentValue.append(pvNode)
parentNode[jpathB]=parentValue
return parentNode
pd = cwdDir(basePath)
targetFile = os.path.join(outFilePath,fzname)
if os.path.isfile(targetFile):
with open(targetFile,mode='w') as tf:
ast =prefixStr + json.dumps(pd,indent=4)
tf.write(ast)
print("SUCCESS !!")
@RequestMapping("/json")
public Map<String, List<BlogTree>> json() {
String directoryPath = "D:\\1907\\0418blog\\osvue\\docs";
File directory = new File(directoryPath);
Map<String, List<BlogTree>> pmp = new HashMap<>();
listFiles(directory, pmp, "");
return pmp;
}
public static void listFiles(File directory, Map<String, List<BlogTree>> pmp, String baseUrl) {
String fname = directory.getName();
if (fname.contains("image") || fname.contains("assets")|| fname.contains("hhh")||fname.startsWith(".")) {
} else {
if (directory.isDirectory()) {
File[] files = directory.listFiles();
baseUrl = baseUrl + "/" + fname;
if (files != null) {
for (File file : files) {
listFiles(file, pmp, baseUrl);
}
}
} else {
List<BlogTree> rList = pmp.get(baseUrl.substring(5));
if (rList != null) {
if (rList.size() > 0) {
BlogTree blogTree = rList.get(0);
blogTree.getChildren().add(fname);
}
} else {
List<BlogTree> als = new ArrayList<>();
BlogTree blogTree = new BlogTree();
blogTree.setText(generText(baseUrl));
blogTree.setTitle(generText(baseUrl));
List<String> urls = new ArrayList<>();
urls.add(fname);
blogTree.setChildren(urls);
als.add(blogTree);
pmp.put(baseUrl.substring(5), als);
}
}
}
}
private static String generText(String fname) {
fname = fname.substring(fname.lastIndexOf("/")+1);
return fname.toUpperCase();
}
package com.osvue.activiti.bpmn.domain;
import java.util.List;
public class BlogTree {
String text;
String title;
boolean collapsable = false;
int sidebarDepth = 1;
List<String> children;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isCollapsable() {
return collapsable;
}
public void setCollapsable(boolean collapsable) {
this.collapsable = collapsable;
}
public int getSidebarDepth() {
return sidebarDepth;
}
public void setSidebarDepth(int sidebarDepth) {
this.sidebarDepth = sidebarDepth;
}
public List<String> getChildren() {
return children;
}
public void setChildren(List<String> children) {
this.children = children;
}
@Override
public String toString() {
return "BlogTree [text=" + text + ", title=" + title + ", collapsable=" + collapsable + ", sidebarDepth="
+ sidebarDepth + ", children=" + children + "]";
}
public BlogTree() {
super();
}
public BlogTree(String text, String title, boolean collapsable, int sidebarDepth, List<String> children) {
super();
this.text = text;
this.title = title;
this.collapsable = collapsable;
this.sidebarDepth = sidebarDepth;
this.children = children;
}
}