Android开发帮助文档Doc打开速度慢解决_Python篇

来源:互联网 发布:你懂的网站永久域名 编辑:程序博客网 时间:2024/05/04 02:47

目录(?)[+]

解决android帮助文档打开慢


网友说是因为Doc目录下的html文件里含有访问google的js文件

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

[html] view plaincopyprint?
  1. <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目录下的所有文件,将每个文件的上边两行内容删除,参考

[java] view plaincopyprint?
  1. /* 
  2.  * 去掉Android文档中需要联网的javascript代码 
  3.  */  
  4. import java.io.BufferedReader;  
  5. import java.io.BufferedWriter;  
  6. import java.io.File;  
  7. import java.io.FileNotFoundException;  
  8. import java.io.FileReader;  
  9. import java.io.FileWriter;  
  10. import java.io.IOException;  
  11.   
  12. public class FormatDoc {  
  13.     public static int j=1;  
  14.     /** 
  15.      * @param args 
  16.      */  
  17.     public static void main(String[] args) {  
  18.           
  19.         File file = new File("D:/android/android-sdk-windows/docs/");  
  20.         searchDirectory(file, 0);  
  21.         System.out.println("OVER");  
  22.     }  
  23.   
  24.     public static void searchDirectory(File f, int depth) {  
  25.         if (!f.isDirectory()) {  
  26.             String fileName = f.getName();  
  27.             if (fileName.matches(".*.{1}html")) {  
  28.                 String src= "<(link rel)[=]\"(stylesheet)\"\n(href)[=]\"(http)://(fonts.googleapis.com/css)[?](family)[=](Roboto)[:](regular,medium,thin,italic,mediumitalic,bold)\"( title)[=]\"roboto\">";  
  29.                 String src1 = "<script src=\"http://www.google.com/jsapi\" type=\"text/javascript\"></script>";  
  30.                 String dst = "";  
  31.                 //如果是html文件则注释掉其中的特定javascript代码  
  32.                 annotation(f, src, dst);  
  33.                 annotation(f, src1, dst);  
  34.             }  
  35.         } else {  
  36.             File[] fs = f.listFiles();  
  37.             depth++;  
  38.             for (int i = 0; i < fs.length; ++i) {  
  39.                 File file = fs[i];  
  40.                 searchDirectory(file, depth);  
  41.             }  
  42.         }  
  43.     }  
  44.   
  45.     /* 
  46.      * f 将要修改其中特定内容的文件  
  47.      * src 将被替换的内容  
  48.      * dst 将被替换层的内容 
  49.      */  
  50.     public static void annotation(File f, String src, String dst) {  
  51.         String content = FormatDoc.read(f);  
  52.         content = content.replaceFirst(src, dst);  
  53.         int ll=content.lastIndexOf(src);  
  54.         System.out.println(ll);  
  55.         FormatDoc.write(content, f);  
  56.         System.out.println(j++);  
  57.         return;  
  58.   
  59.     }  
  60.   
  61.     public static String read(File src) {  
  62.         StringBuffer res = new StringBuffer();  
  63.         String line = null;  
  64.         try {  
  65.             BufferedReader reader = new BufferedReader(new FileReader(src));  
  66.             int i=0;  
  67.             while ((line = reader.readLine()) != null) {  
  68.                 if (i!=0) {  
  69.                     res.append('\n');  
  70.                 }  
  71.                 res.append(line);  
  72.                 i++;  
  73.             }  
  74.             reader.close();  
  75.         } catch (FileNotFoundException e) {  
  76.             e.printStackTrace();  
  77.         } catch (IOException e) {  
  78.             e.printStackTrace();  
  79.         }  
  80.         return res.toString();  
  81.     }  
  82.   
  83.     public static boolean write(String cont, File dist) {  
  84.         try {  
  85.             BufferedWriter writer = new BufferedWriter(new FileWriter(dist));  
  86.             writer.write(cont);  
  87.             writer.flush();  
  88.             writer.close();  
  89.             return true;  
  90.         } catch (IOException e) {  
  91.             e.printStackTrace();  
  92.             return false;  
  93.         }  
  94.     }  
  95. }  

方法三:执行脚本

通过shell删除那行js代码,非常简洁方便,比上面的的java方便100倍,不过不能删掉第一段js代码。

[plain] view plaincopyprint?
  1. find . -name "*.html"|xargs grep -l "jsapi"|xargs sed -i '/jsapi/d'  
我没试过。


人生苦短,我用Python

方法四:python代码批量删除

思路:遍历doc或docs目录及子目录,查找所有.html文件,打开这些文件,读取文件内容,替换上边的js内容为空,把修改内容写回文件,结束。

说着很复杂,用python实现真的很简单。

[python] view plaincopyprint?
  1. import os  
  2. s1 = '''''<link rel="stylesheet" 
  3. href="http://fonts.googleapis.com/css?family=Roboto:regular,medium,thin,italic,mediumitalic,bold" title="roboto">'''  
  4. s2 = '''''<script src="http://www.google.com/jsapi" type="text/javascript"></script>'''  
  5. s3 = '''''<script type="text/javascript" async="" src="https://apis.google.com/js/plusone.js"></script>'''  
  6. s4 = '''''<script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script>'''  
  7. for root,dirs,files in os.walk(r'C:\AndroidSdk\docs'):  
  8.     for file in files:  
  9.         fd = root + os.sep + file  
  10.         if ".html" in fd:  
  11.             print fd  
  12.             f = open(fd, 'r')  
  13.             s = f.read().replace(s1, "").replace(s2, "").replace(s3, "").replace(s4, "")  
  14.             f.close()  
  15.             f = open(fd, 'w')  
  16.             f.write(s)  
  17.             f.close()  
  18.           

献丑一条条解释一下,假定我的Android的开发帮助文档在c:\androidsdk\docs下,遍历其下所有目录和文件名只需用os的walk函数即可完成。

[python] view plaincopyprint?
  1. for root,dirs,files in os.walk(r'C:\AndroidSdk\docs'):  
walk返回值,当前遍历的目录root、其下有哪些子目录dirs、有哪些文件files,重要的是递归的遍历指定目录C:\AndroidSdk\docs。

去掉html文件里两条影响速度的js,得先有html文件,

[python] view plaincopyprint?
  1. for file in files:  
  2.     fd = root + os.sep + file  
则构造出了所有文件名,找到(匹配).html文件只需这样

[python] view plaincopyprint?
  1. if ".html" in fd:  
  2.     print fd  
接下来就是干掉影显示(速度)的js了,替换文件里相应内容为空即可。

[python] view plaincopyprint?
  1. f = open(fd, 'r')  
  2.             s = f.read().replace(s1, "").replace(s2, "").replace(s3, "").replace(s4, "")  
  3.             f.close()  
  4.             f = open(fd, 'w')  
  5.             f.write(s)  
  6.             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

0 0
原创粉丝点击