后端分享之根据文档内容标签生成简易文档目录

3 篇文章 0 订阅
2 篇文章 0 订阅

docsify虽然好且方便,但是不一定所有人都会使用md,所以东拼西凑有了下面的操作

1、工具类

public class RegxUtils {
	/**
	 * 根据 标签和内容生成目录
	 * 
	 * @param reg
	 * @param context
	 * @return
	 */
	public static RegxModel generateMenu(String reg, String context) {
		StringBuffer buffer=new StringBuffer(context);
		RegxModel model = new RegxModel();
		// <[h1|h2|h3|h4|h5].*?>(.*?)</([h1|h2|h3|h4|h5].*?)>
		LinkedList<HashMap<String, String>> list = new LinkedList<>();
		//忽略大小写匹配
		Matcher m = Pattern.compile(reg,Pattern.CASE_INSENSITIVE).matcher(context);
		//记录循环次数
		int index=0;
		//记录上一次匹配的数据 得到的id值的长度
		int idLength=0;
		while (m.find()) {
			HashMap<String, String> map = new HashMap<>();
			String title = m.group(1);
			//去除嵌套标签
			String menuName=title.replaceAll("<[^<>]+>", "");
			if(!title.trim().equals("")) {
				// MD5转换code
				String id = MD5.GetMD5Code(menuName).substring(14, 20).toLowerCase();
				//转换为英文字母
				for (int i = 0; i < id.length(); i++) {
					if (((int) id.charAt(i)) < 65) {
						id = id.replace(id.charAt(i), (char) ((int) id.charAt(i) + 65));
					}
				}
				//标签
				String el = m.group().substring(1, 3).toLowerCase();
				//防止重复 所以加上变量
				id=id+""+index;
				//记录匹配的值的 【开始下标 】 后面需要使用
				int start=m.start();
				//记录匹配的值的 【结束下标】 后面需要使用
				int end=m.end();
				if(idLength!=0) {
					//因为上一次有新加 id值 所以下标要后移
					start+=idLength;
					end+=idLength;
				}
				buffer = buffer.replace(start,end, "<" + el + " id=\"" + id + "\">" + title + "</" + el + ">");
				map.put("id", id);
				map.put("cateName", menuName);
				map.put("nodeId", "m"+id);
				map.put("label", el);
				list.add(map);
				index++;
				idLength+=id.length()+6;// id=""  需要添加额外的占位
			}
		}
		model.setContext(buffer.toString());
		model.setMenu(list);
		return model;
	}

	public static void main(String[] args) {
		String context = "<H1><span>主菜单1</span></H1><h2>子菜单1</h2><h3>子子菜单1</h3><h2>子菜单2</h2><h1>主菜单1</h1>";
		String reg = "<[h1|h2|h3|h4|h5].*?>(.*?)</([h1|h2|h3|h4|h5].*?)>";
		RegxModel result = RegxUtils.generateMenu(reg, context);
		System.out.println(JSON.toJSONString(result));
	}
}

2、MD5

public class MD5 {
    
    // 全局数组
    private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

    public MD5() {
    }

    // 返回形式为数字跟字符串
    private static String byteToArrayString(byte bByte) {
        int iRet = bByte;
        // System.out.println("iRet="+iRet);
        if (iRet < 0) {
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return strDigits[iD1] + strDigits[iD2];
    }

    // 返回形式只为数字
    @SuppressWarnings("unused")
	private static String byteToNum(byte bByte) {
        int iRet = bByte;
        System.out.println("iRet1=" + iRet);
        if (iRet < 0) {
            iRet += 256;
        }
        return String.valueOf(iRet);
    }

    // 转换字节数组为16进制字串
    private static String byteToString(byte[] bByte) {
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 0; i < bByte.length; i++) {
            sBuffer.append(byteToArrayString(bByte[i]));
        }
        return sBuffer.toString();
    }

    public static String GetMD5Code(String strObj) {
        String resultString = null;
        try {
            resultString = new String(strObj);
            MessageDigest md = MessageDigest.getInstance("MD5");
            // md.digest() 该函数返回值为存放哈希值结果的byte数组
            resultString = byteToString(md.digest(strObj.getBytes()));
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        return resultString;
    }
}

3、接收数据实体

/**
 * 正则获取
 * @author 
 *
 */
@Getter
@Setter
public class RegxModel {
	/**
	 * 内容
	 */
	private String context;
	
	/**
	 * 目录
	 */
	private LinkedList<HashMap<String, String>> menu;
	
}

运行结果:

context为转换后的数据,menu为生成的菜单结构

{
	"context": "<h1 id=\"rcwzzt0\"><span>主菜单1</span></h1><h2 id=\"rzczrf1\">子菜单1</h2><h3 id=\"susuuu2\">子子菜单1</h3><h2 id=\"wdffde3\">子菜单2</h2><h1 id=\"rcwzzt4\">主菜单1</h1>",
	"menu": [{
		"id": "rcwzzt0",//对应标签的id,可以通过锚点点击跳转
		"label": "h1",//标签
		"cateName": "主菜单1",//标签内容
		"nodeId": "mrcwzzt0"//唯一标识
	}, {
		"id": "rzczrf1",
		"label": "h2",
		"cateName": "子菜单1",
		"nodeId": "mrzczrf1"
	}, {
		"id": "susuuu2",
		"label": "h3",
		"cateName": "子子菜单1",
		"nodeId": "msusuuu2"
	}, {
		"id": "wdffde3",
		"label": "h2",
		"cateName": "子菜单2",
		"nodeId": "mwdffde3"
	}, {
		"id": "rcwzzt4",
		"label": "h1",
		"cateName": "主菜单1",
		"nodeId": "mrcwzzt4"
	}]
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值