能让程序做的事情坚决不用人来做——批量修复markdownlint MD034警告
欢迎和大家交流技术相关问题:
邮箱: jiangxinnju@163.com
博客园地址: http://www.cnblogs.com/jiangxinnju
GitHub地址: https://github.com/jiangxincode
知乎地址: https://www.zhihu.com/people/jiangxinnju
现在各种编程语言都有自己的lint工具来做静态检查,防止一些低级错误并维持统一的风格。Markdown这样的样式标记语言也不例外,现在大家用的比较多的是markdownlint。该项目开源在github:https://github.com/markdownlint/markdownlint,当前很多实用markdown语言的项目都使用该工具。
markdownlint的检查规则目前有41项https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md,其中"MD034 - Bare URL used"是大家经常遇到的问题,如果你在Markdown文档中使用URL,但是没有在URL周围使用<>的话就会产生警告,我维护了一个类似于awesome的项目https://github.com/jiangxincode/cnblogs,其中报了1000多个类似的警告,如果全部手工来修改估计手都回废掉,因此写了个小程序在所有的.md文档中的所有url两遍加上了<>。
程序使用Java编写,比较简单,主要使用了正则表达式的替换,程序如下:
package edu.jiangxin.tools;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Solve the problem of MD034. <p>
* {@link https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md}
* This program doesn't process any exception!
* @author aloys
*
*/
public class MD034Solver {
public static final String regex = "\\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]";
public static final String sourceDirStr = "D:\\temp\\cnblogs";
public static final String targetDirStr = "D:\\temp\\cnblogsbak";
public static void main(String[] args) throws IOException {
// Source directory must be a valid directory which contains the text files to be processed.
File sourceDir = new File(sourceDirStr);
// Target directory must be a valid directory which will save the proecessed files.
File targetDir = new File(targetDirStr);
for (File file : sourceDir.listFiles()) {
// take off the .git directory and .gitignore file
if (file.getName().startsWith(".")) {
continue;
}
BufferedReader reader = new BufferedReader(new FileReader(file));
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(targetDir, file.getName())));
String temp = null;
while ((temp = reader.readLine()) != null) {
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher regexMatcher = pattern.matcher(temp);
StringBuffer sb = new StringBuffer();
while (regexMatcher.find()) {
String group = regexMatcher.group();
int start = regexMatcher.start();
if (start >= 1 && (temp.charAt(start - 1) == '<' || temp.charAt(start - 1) == '(')) {
regexMatcher.appendReplacement(sb, group);
} else {
regexMatcher.appendReplacement(sb, "<" + group + ">");
}
}
regexMatcher.appendTail(sb);
writer.write(sb.toString());
writer.newLine();
}
reader.close();
writer.close();
}
}
}
能让程序做的事情坚决不用人来做——批量修复markdownlint MD034警告的更多相关文章
- 10件在PHP7中不要做的事情
10件在PHP7中不要做的事情 1. 不要使用mysql_函数 这一天终于来了,从此你不仅仅“不应该”使用mysql_函数.PHP 7 已经把它们从核心中全部移除了,也就是说你需要迁移到好得多的mys ...
- 10件5G能实现,但4G不能做的事情
10件5G能实现,但4G不能做的事情 从三星Galaxy S10 5G手机到OnePlus 7 Pro 5G手机以及更高版本,首批5G手机现已上市.5G网络时代的开启是从小范围内,如果你居住在可以使用 ...
- 程序员IT狗有什么副业可以做呢?
1. 开篇 副业有很多,全网有做什么公众号.闲鱼.手机卡,各种各样的都有,大部分是骗子,小部分是通过自己的努力,获得了成功. 从年初就开始实践如何做一个自由职业者,近大半年有一些感受正好一起分享交流一 ...
- JS可以做很多事情
JS可以做很多事情,例如: 使用JavaScript可以做很多事情,使网页更具互动性,并为网站用户提供更好.更令人兴奋的体验.JavaScript允许您创建一个活动的用户界面,当用户在页面之间导航时, ...
- 网站部署 HTTPS 中需要做的事情
这篇文章首发于我的个人网站:听说 - https://tasaid.com/,建议在我的个人网站阅读,拥有更好的阅读体验. 这篇文章与 博客园 和 Segmentfault 共享. 前端开发QQ群:3 ...
- 高级Linux SA需要会做的事情
高级Linux SA需要会做的事情:linux---------系统安装(光盘或自动化安装)linux---------系统常用工具安装(sudo,ntp,yum,rsync,lrzsz syssta ...
- 揣摩实现一个ioc容器需要做的事情
思路: ioc框架的核心就是管理bean的生命周期,bean的生命周期包括:创建,使用,销毁. 创建 容器在创建一个bean的实例之前必须要解决以下问题:第一个问题: 创建bean的信息如何提供给你容 ...
- php大力力 [039节] 修改一下后台项目,同时启用印象笔记,要做的事情todo列表,记录在印象笔记,速度快一些
php大力力 [039节] 修改一下后台项目,同时启用印象笔记,要做的事情todo列表,记录在印象笔记,速度快一些
- 学好php可以做的事情真多!
学好php能做什么?其实学好php能做的事情很多! 一. 学好php可以就业 1:大中小公司通吃. 现在几乎所有有前途的公司都会在互联网上安家.只要在网上安家,就需要找这些方面的技术人员,而且很多公司 ...
随机推荐
- Mecanim高级主题:Mecanim Blend Tree应用、Blend Tree 选项、复合Blend Tree
一.Blend Tree介绍及应用 一个游戏动画的基本任务就是将两个或多个相似的动作混合.也许最广为人知的例子就是依照任务行动的速度将行走和跑动动画混合起来了.另一个例子就是角色在跑动中向左或向右转身 ...
- 手写代码UI,xib和StoryBoard间的的优劣比较
在UI制作方面,逐渐分化三种主要流派:使用代码手写UI:使用单个xib文件组织viewController或者view:使用StoryBoard来通过单个或很少的几个文件构建UI.三种方式各有优劣,也 ...
- OpenCV Create Circular Mask 圆形遮罩
在OpenCV中,比较常见的是矩形遮罩CvRect,没有专门提供圆形的mask,那么我们只能自己写一个来模拟圆形mask的函数,需要提供的参数为原图的大小,以及圆形mask的圆心位置和半径即可,返回一 ...
- POJ 2342 - Anniversary party - [树形DP]
题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...
- ZJU-1003 Crashing Balloon dfs,
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3 题意(难以描述):A,B两个人从1~100选数乘起来比谁的大(不能选重复的或者 ...
- Simple Mail Transfer Protocol
https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol https://zh.wikipedia.org/wiki/简单邮件传输协议 & ...
- eclipse maven build、maven clean、maven install和maven test的区别 精析
1.情景展示 选中maven项目,右键-->Run As或Debug As-->maven buid,maven install,maven test有什么区别? 2.区别说明 ...
- centos7设置iptables
https://www.linuxidc.com/Linux/2017-10/147238.htm
- router-link传参 query方式
router.js内的路由配置 { path: '/CreateProgress', name: 'CreateProgress', component:CreateProgress } 传参(q ...
- Elasticsearch 的坑爹事——记录一次mapping field修改过程(转)
原文:http://www.cnblogs.com/Creator/p/3722408.html 本文记录一次Elasticsearch mapping field修改过程 团队使用Elasticse ...