Android開發協助文檔Doc開啟速度慢解決_Python篇,

來源:互聯網
上載者:User

Android開發協助文檔Doc開啟速度慢解決_Python篇,

解決android協助文檔開啟慢

網友說是因為Doc目錄下的html檔案裡含有訪問google的js檔案

<link rel="stylesheet"href="http://fonts.googleapis.com/css?family=Roboto:regular,medium,thin,italic,mediumitalic,bold" title="roboto">

 <script src="http://www.google.com/jsapi" type="text/javascript"></script>
經查的確如此。

由於這兩行指令碼需線上訪問Google,顯然後續內容的載入就會很慢慢慢慢......

咋辦呢?

將每個目錄下的.html檔案都開啟手動刪除上邊兩行內容?一定會刪到 疼。有人建議:

方法一:修改Hosts檔案

這樣解決,修改C:\WINDOWS\system32\drivers\etc目錄下的hosts檔案裡添加

127.0.0.1 fonts.googleapis.com
127.0.0.1www.google.com
127.0.0.1www.google.com/jsapi
127.0.0.1www.google-analytics.com
127.0.0.1 apis.google.com/js/

速度會提升很多。

方法二:編寫Java程式批量注釋

遍曆doc目錄下的所有檔案,將每個檔案的上邊兩行內容刪除,參考

/* * 去掉Android文檔中需要連網的javascript代碼 */import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class FormatDoc {    public static int j=1;    /**     * @param args     */    public static void main(String[] args) {                File file = new File("D:/android/android-sdk-windows/docs/");        searchDirectory(file, 0);        System.out.println("OVER");    }    public static void searchDirectory(File f, int depth) {        if (!f.isDirectory()) {            String fileName = f.getName();            if (fileName.matches(".*.{1}html")) {                String src= "<(link rel)[=]\"(stylesheet)\"\n(href)[=]\"(http)://(fonts.googleapis.com/css)[?](family)[=](Roboto)[:](regular,medium,thin,italic,mediumitalic,bold)\"( title)[=]\"roboto\">";                String src1 = "<script src=\"http://www.google.com/jsapi\" type=\"text/javascript\"></script>";                String dst = "";                //如果是html檔案則注釋掉其中的特定javascript代碼                annotation(f, src, dst);                annotation(f, src1, dst);            }        } else {            File[] fs = f.listFiles();            depth++;            for (int i = 0; i < fs.length; ++i) {                File file = fs[i];                searchDirectory(file, depth);            }        }    }    /*     * f 將要修改其中特定內容的檔案      * src 將被替換的內容      * dst 將被替換層的內容     */    public static void annotation(File f, String src, String dst) {        String content = FormatDoc.read(f);        content = content.replaceFirst(src, dst);        int ll=content.lastIndexOf(src);        System.out.println(ll);        FormatDoc.write(content, f);        System.out.println(j++);        return;    }    public static String read(File src) {        StringBuffer res = new StringBuffer();        String line = null;        try {            BufferedReader reader = new BufferedReader(new FileReader(src));            int i=0;            while ((line = reader.readLine()) != null) {                if (i!=0) {                    res.append('\n');                }                res.append(line);                i++;            }            reader.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return res.toString();    }    public static boolean write(String cont, File dist) {        try {            BufferedWriter writer = new BufferedWriter(new FileWriter(dist));            writer.write(cont);            writer.flush();            writer.close();            return true;        } catch (IOException e) {            e.printStackTrace();            return false;        }    }}

方法三:執行指令碼

通過shell刪除那行js代碼,非常簡潔方便,比上面的的java方便100倍,不過不能刪掉第一段js代碼。

find . -name "*.html"|xargs grep -l "jsapi"|xargs sed -i '/jsapi/d'
我沒試過。


人生苦短,我用Python

方法四:python代碼大量刪除

思路:遍曆doc或docs目錄及子目錄,尋找所有.html檔案,開啟這些檔案,讀取檔案內容,替換上邊的js內容為空白,把修改內容寫迴文件,結束。

說著很複雜,用python實現真的很簡單。

import oss1 = '''<link rel="stylesheet"href="http://fonts.googleapis.com/css?family=Roboto:regular,medium,thin,italic,mediumitalic,bold" title="roboto">'''s2 = '''<script src="http://www.google.com/jsapi" type="text/javascript"></script>'''s3 = '''<script type="text/javascript" async="" src="https://apis.google.com/js/plusone.js"></script>'''s4 = '''<script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script>'''for root,dirs,files in os.walk(r'C:\AndroidSdk\docs'):    for file in files:        fd = root + os.sep + file        if ".html" in fd:            print fd            f = open(fd, 'r')            s = f.read().replace(s1, "").replace(s2, "").replace(s3, "").replace(s4, "")            f.close()            f = open(fd, 'w')            f.write(s)            f.close()      

獻醜一條條解釋一下,假定我的Android的開發協助文檔在c:\androidsdk\docs下,遍曆其下所有目錄和檔案名稱只需用os的walk函數即可完成。

for root,dirs,files in os.walk(r'C:\AndroidSdk\docs'):
walk返回值,當前遍曆的目錄root、其下有哪些子目錄dirs、有哪些檔案files,重要的是遞迴的遍曆指定目錄C:\AndroidSdk\docs。

去掉html檔案裡兩條影響速度的js,得先有html檔案,

    for file in files:        fd = root + os.sep + file
則構造出了所有檔案名稱,找到(匹配).html檔案只需這樣

        if ".html" in fd:            print fd
接下來就是幹掉影顯示(速度)的js了,替換檔案裡相應內容為空白即可。

f = open(fd, 'r')            s = f.read().replace(s1, "").replace(s2, "").replace(s3, "").replace(s4, "")            f.close()            f = open(fd, 'w')            f.write(s)            f.close()

運行吧,1分鐘內就運行結束了,整個docs下共九千多個檔案,遍曆、讀寫需要時間。

最後找個doc試試 C:\AndroidSdk\docs\reference\android\widget\Spinner.html

那速度,杠杠的。

我用的是python2.7.5

呵呵,歡迎回複批評!或點贊!


python https://www.python.org/ftp/python/2.7.9/python-2.7.9.msi





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.