Groovy指令碼檢查html壞連結

來源:互聯網
上載者:User

標籤:groovy   html檢查   壞連結檢查   

這些天在搞Gradle翻譯,因為原譯者在翻譯的同時也把檔案進行了整理,並且把翻譯過的章節放到新的檔案夾中,導致可能有些超連結未改正過來變成死連結。

本想在網上找個工具來檢查的,百度了幾個工具要麼太大要麼要安裝,懶得弄那麼多,於是用Groovy寫了一個指令碼。此指令碼僅檢查本地超連結,代碼如下:

if (args.size() != 1) {    printf("Please specify a folder or HTML file path...")    return}def file = new File(args[0])if(file.isFile()) {    if(!args[0].toLowerCase().endsWith(".html")) {        return    }    checkHtml(file)} else if (file.isDirectory()) {    def errorLinks = new HashMap<String, List<String>>()    file.eachFileMatch( ~/.*\.html/, {        checkHtml(it, errorLinks)    })    errorLinks.each {name, links ->        println "file: " + name        links.each {            println "href:\t" + it        }    }}void checkHtml(File file, HashMap<String, List<String>> errorlinks) {    def matches = file.text.findAll('href="([^#(http)].+?)("|#)')    def links = new ArrayList<String>()    matches.each {        def path = it - 'href="' - '"' - '#'        if(!new File(file.getParentFile(), path).exists()) {            links.add(path)        }    }    if(!links.isEmpty()) {        errorlinks.put(file.path, links)    }}

運行時傳入一個地址。如果是HTML檔案,則檢查該檔案。如果是目錄,則檢查裡面的HTML檔案,其他檔案不檢查。然後把有錯誤的檔案及其超連結在最後列印出來,正確的不列印。

Groovy指令碼檢查html壞連結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.