kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

正则表达式常用字符

字符 描述
\ 将下一个字符标记为一个特殊字符、或一个原义字符、或一个 后向引用、或一个八进制转义符。例如,'n' 匹配字符 "n"。'\n' 匹配一个换行符。序列 '\\' 匹配 "\" 而 "\(" 则匹配 "("。
^ 匹配输入字符串的开始位置。如果设置了 RegExp 对象的 Multiline 属性,^ 也匹配 '\n' 或 '\r' 之后的位置。
$ 匹配输入字符串的结束位置。如果设置了RegExp 对象的 Multiline 属性,$ 也匹配 '\n' 或 '\r' 之前的位置。
\A 指定匹配必须出现在字符串的开头(忽略   Multiline   选项)。
\Z 指定匹配必须出现在字符串的结尾或字符串结尾的     之前(忽略   Multiline   选项)。
\z 指定匹配必须出现在字符串的结尾(忽略   Multiline   选项)。
* 匹配前面的子表达式零次或多次。例如,zo* 能匹配 "z" 以及 "zoo"。 * 等价于{0,}。
+ 匹配前面的子表达式一次或多次。例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。+ 等价于 {1,}。
? 匹配前面的子表达式零次或一次。例如,"do(es)?" 可以匹配 "do" 或 "does" 中的"do" 。? 等价于 {0,1}。
{n} n 是一个非负整数。匹配确定的 n 次。例如,'o{2}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的两个 o。
{n,} n 是一个非负整数。至少匹配n 次。例如,'o{2,}' 不能匹配 "Bob" 中的 'o',但能匹配 "foooood" 中的所有 o。'o{1,}' 等价于 'o+'。'o{0,}' 则等价于 'o*'。
{n,m} m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。刘, "o{1,3}" 将匹配 "fooooood" 中的前三个 o。'o{0,1}' 等价于 'o?'。请注意在逗号和两个数之间不能有空格。
? 当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。例如,对于字符串 "oooo",'o+?' 将匹配单个 "o",而 'o+' 将匹配所有 'o'。
. 匹配除 "\n" 之外的任何单个字符。要匹配包括 '\n' 在内的任何字符,请使用象 '[.\n]' 的模式。
(pattern) 匹配pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在Visual Basic Scripting Edition 中则使用 $0$9 属性。要匹配圆括号字符,请使用 '\(' 或 '\)'。
(?:pattern) 匹配 pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用 "或" 字符 (|) 来组合一个模式的各个部分是很有用。例如, 'industr(?:y|ies) 就是一个比 'industry|industries' 更简略的表达式。
(?=pattern) 正向预查,在任何匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如, 'Windows (?=95|98|NT|2000)' 能匹配 "Windows 2000" 中的 "Windows" ,但不能匹配 "Windows 3.1" 中的 "Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。
(?!pattern) 负向预查,在任何不匹配Negative lookahead matches the search string at any point where a string not matching pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如'Windows (?!95|98|NT|2000)' 能匹配 "Windows 3.1" 中的 "Windows",但不能匹配 "Windows 2000" 中的 "Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始
x|y 匹配 x 或 y。例如,'z|food' 能匹配 "z" 或 "food"。'(z|f)ood' 则匹配 "zood" 或 "food"。
[xyz] 字符集合。匹配所包含的任意一个字符。例如, '[abc]' 可以匹配 "plain" 中的 'a'。
[^xyz] 负值字符集合。匹配未包含的任意字符。例如, '[^abc]' 可以匹配 "plain" 中的'p'。
[a-z] 字符范围。匹配指定范围内的任意字符。例如,'[a-z]' 可以匹配 'a' 到 'z' 范围内的任意小写字母字符。
[^a-z] 负值字符范围。匹配任何不在指定范围内的任意字符。例如,'[^a-z]' 可以匹配任何不在 'a' 到 'z' 范围内的任意字符。
\b 匹配一个单词边界,也就是指单词和空格间的位置。例如, 'er\b' 可以匹配"never" 中的 'er',但不能匹配 "verb" 中的 'er'。
\B 匹配非单词边界。'er\B' 能匹配 "verb" 中的 'er',但不能匹配 "never" 中的 'er'。
\cx 匹配由x指明的控制字符。例如, \cM 匹配一个 Control-M 或回车符。 x 的值必须为 A-Z 或 a-z 之一。否则,将 c 视为一个原义的 'c' 字符。
\d 匹配一个数字字符。等价于 [0-9]。
\D 匹配一个非数字字符。等价于 [^0-9]。
\f 匹配一个换页符。等价于 \x0c 和 \cL。
\n 匹配一个换行符。等价于 \x0a 和 \cJ。
\r 匹配一个回车符。等价于 \x0d 和 \cM。
\s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。
\S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。
\t 匹配一个制表符。等价于 \x09 和 \cI。
\v 匹配一个垂直制表符。等价于 \x0b 和 \cK。
\w 匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'。
\W 匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'。
\xn 匹配 n,其中 n 为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如, '\x41' 匹配 "A"。'\x041' 则等价于 '\x04' & "1"。正则表达式中可以使用 ASCII 编码。.
\num 匹配 num,其中 num 是一个正整数。对所获取的匹配的引用。例如,'(.)\1' 匹配两个连续的相同字符。
\n 标识一个八进制转义值或一个后向引用。如果 \n 之前至少 n 个获取的子表达式,则 n 为后向引用。否则,如果 n 为八进制数字 (0-7),则 n 为一个八进制转义值。
\nm 标识一个八进制转义值或一个后向引用。如果 \nm 之前至少有is preceded by at least nm 个获取得子表达式,则 nm 为后向引用。如果 \nm 之前至少有 n 个获取,则 n 为一个后跟文字 的后向引用。如果前面的条件都不满足,若  n 和 m 均为八进制数字 (0-7),则 \nm 将匹配八进制转义值 nm
\nml 如果 n 为八进制数字 (0-3),且 m 和 l 均为八进制数字 (0-7),则匹配八进制转义值 nml。
\un 匹配 n,其中 n 是一个用四个十六进制数字表示的 Unicode 字符。例如, \u00A9 匹配版权符号 (?)。

jekyll的一些全局变量

http://jekyllrb.com/docs/variables/
octopress是基于jekyll,所以也可以用这些内容

Variables

Jekyll traverses your site looking for files to process. Any files with YAML front matter are subject to processing. For each of these files, Jekyll makes a variety of data available via the Liquid templating system. The following is a reference of the available data.

Global Variables
Variable Description

site

Sitewide information + configuration settings from _config.yml. See below for details.

page

Page specific information + the YAML front matter. Custom variables set via the YAML Front Matter will be available here. See below for details.

content

In layout files, the rendered content of the Post or Page being wrapped. Not defined in Post or Page files.

paginator

When the paginate configuration option is set, this variable becomes available for use.

Site Variables
Variable Description

site.time

The current time (when you run the jekyll command).

site.pages

A list of all Pages.

site.posts

A reverse chronological list of all Posts.

site.related_posts

If the page being processed is a Post, this contains a list of up to ten related Posts. By default, these are low quality but fast to compute. For high quality but slow to compute results, run the jekyll command with the --lsi (latent semantic indexing) option.

site.static_files

A list of all static files (i.e. files not processed by Jekyll's converters or the Liquid renderer). Each file has three properties: path, modified_time and extname.

site.html_pages

A list of all HTML Pages.

site.collections

A list of all the collections.

site.data

A list containing the data loaded from the YAML files located in the _data directory.

site.documents

A list of all the documents in every collection.

site.categories.CATEGORY

The list of all Posts in category CATEGORY.

site.tags.TAG

The list of all Posts with tag TAG.

site.[CONFIGURATION_DATA]

All the variables set via the command line and your _config.yml are available through the site variable. For example, if you have url: http://mysite.com in your configuration file, then in your Posts and Pages it will be stored in site.url. Jekyll does not parse changes to _config.yml in watch mode, you must restart Jekyll to see changes to variables.

Page Variables
Variable Description

page.content

The content of the Page, rendered or un-rendered depending upon what Liquid is being processed and what page is.

page.title

The title of the Page.

page.excerpt

The un-rendered excerpt of the Page.

page.url

The URL of the Post without the domain, but with a leading slash, e.g. /2008/12/14/my-post.html

page.date

The Date assigned to the Post. This can be overridden in a Post’s front matter by specifying a new date/time in the format YYYY-MM-DD HH:MM:SS (assuming UTC), or YYYY-MM-DD HH:MM:SS +/-TTTT (to specify a time zone using an offset from UTC. e.g. 2008-12-14 10:30:00 +0900).

page.id

An identifier unique to the Post (useful in RSS feeds). e.g. /2008/12/14/my-post

page.categories

The list of categories to which this post belongs. Categories are derived from the directory structure above the _posts directory. For example, a post at /work/code/_posts/2008-12-24-closures.md would have this field set to ['work', 'code']. These can also be specified in the YAML Front Matter.

page.tags

The list of tags to which this post belongs. These can be specified in the YAML Front Matter.

page.path

The path to the raw post or page. Example usage: Linking back to the page or post’s source on GitHub. This can be overridden in the YAML Front Matter.

page.next

The next post relative to the position of the current post in site.posts. Returns nil for the last entry.

page.previous

The previous post relative to the position of the current post in site.posts. Returns nil for the first entry.

Paginator
Variable Description

paginator.per_page

Number of Posts per page.

paginator.posts

Posts available for that page.

paginator.total_posts

Total number of Posts.

paginator.total_pages

Total number of Pages.

paginator.page

The number of the current page.

paginator.previous_page

The number of the previous page.

paginator.previous_page_path

The path to the previous page.

paginator.next_page

The number of the next page.

paginator.next_page_path

The path to the next page.

octopress优化

octopress优化

能够让octopress在50篇文章下跑进5s,不优化要跑60s左右
300篇15s左右

运行过程

看octopress目录下的Rakefile,里面有generate,preview,watch等。

输入rake generate是就是按照Rakefile中task :generate do执行的。

最主要的两条:

1
2
system "compass compile --css-dir #{source_dir}/stylesheets"
system "jekyll build"

第一条是编译css,第二条是生成文章。

第一条不知道如何优化,略过。

第二条接着执行到

/usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/commands/build.rb

文件的 init_with_program -> process -> build 然后到

/usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/command.rb 文件的 process_site 然后到

/usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/site.rb 文件的 process,

process代码如下:

1
2
3
4
5
6
7
8
47     def process
48       reset
49       read
50       generate
51       render
52       cleanup
53       write
54     end

这里的函数都在这个文件里,主要费时在 generate 和 render。

1. generate

generate 会执行 octopress/plugins 目录下的 tag_generator.rb 和 category_generator.rb,

这两个文件的 write_tag_indexes 和 write_category_indexes 分别回构建 tag 和 category 的分类首页。

所以 tag 和 category 越多构建越慢。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from /home/kk/kk/github/octopress/plugins/rubypants.rb:261:in `to_html'
from /home/kk/kk/github/octopress/plugins/octopress_filters.rb:31:in `post_filter'
from /home/kk/kk/github/octopress/plugins/octopress_filters.rb:41:in `post_render'
from /usr/local/lib/ruby/gems/2.1.0/gems/octopress-hooks-2.2.1/lib/octopress-hooks.rb:255:in `block in post_render'
from /usr/local/lib/ruby/gems/2.1.0/gems/octopress-hooks-2.2.1/lib/octopress-hooks.rb:254:in `each'
from /usr/local/lib/ruby/gems/2.1.0/gems/octopress-hooks-2.2.1/lib/octopress-hooks.rb:254:in `post_render'
from /usr/local/lib/ruby/gems/2.1.0/gems/octopress-hooks-2.2.1/lib/octopress-hooks.rb:224:in `do_layout'
from /usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/page.rb:122:in `render'
from /home/kk/kk/github/octopress/plugins/category_generator.rb:100:in `write_category_index'
from /home/kk/kk/github/octopress/plugins/category_generator.rb:112:in `block in write_category_indexes'
from /home/kk/kk/github/octopress/plugins/category_generator.rb:111:in `each'
from /home/kk/kk/github/octopress/plugins/category_generator.rb:111:in `write_category_indexes'
from /home/kk/kk/github/octopress/plugins/category_generator.rb:141:in `generate'
from /usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/site.rb:280:in `block in generate'
from /usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/site.rb:279:in `each'
from /usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/site.rb:279:in `generate'
from /usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/site.rb:50:in `process'

2. render

1
2
3
4
5
6
7
8
9
10
11
from /home/kk/kk/github/octopress/plugins/rubypants.rb:261:in `to_html'
from /home/kk/kk/github/octopress/plugins/octopress_filters.rb:31:in `post_filter'
from /home/kk/kk/github/octopress/plugins/octopress_filters.rb:41:in `post_render'
from /usr/local/lib/ruby/gems/2.1.0/gems/octopress-hooks-2.2.1/lib/octopress-hooks.rb:249:in `block in pre_render'
from /usr/local/lib/ruby/gems/2.1.0/gems/octopress-hooks-2.2.1/lib/octopress-hooks.rb:248:in `each'
from /usr/local/lib/ruby/gems/2.1.0/gems/octopress-hooks-2.2.1/lib/octopress-hooks.rb:248:in `pre_render'
from /usr/local/lib/ruby/gems/2.1.0/gems/octopress-hooks-2.2.1/lib/octopress-hooks.rb:222:in `do_layout'
from /usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/page.rb:122:in `render'
from /usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/site.rb:299:in `block in render'
from /usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/site.rb:298:in `each'
from /usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/site.rb:298:in `render'

都是慢在 octopress/plugins/rubypants.rb 的 to_html 函数。

优化

1、to_html 函数的 tokenize 和 gsub 很慢。

to_html 函数只是把一些' “之类的转成html,试了一下不执行to_html,diff出来差别不大,页面上显示也都还OK。单纯的 < 在其他地方已经转好了。

所以 octopress/plugins/octopress_filters.rb 中不执行 to_html

2、每次计算侧边栏太慢

所有页面都是按照其 layout 的格式找对应 source/_layout/ 下的模板来生成的。

注意 post.html 和 page.html 的最后都有 { { include_array XXX } }, 这就是说每个页面都要运行 plugins/include_array.rb 中的 render(context) 来生成侧边栏。

但是侧边栏应该是(?)都一样的。所以改成隔一定时间计算一次

1
2
3
4
5
6
7
8
9
 26     @@caltime = 0
 27     @@retstore = ""
 28     def render(context)
 29         if Time.now.to_f - @@caltime < context.registers[:site].config['recaltime']
 30                 return @@retstore;
 31         end
 32         @@caltime = Time.now.to_f
...
 58       @@retstore = rtn

为什么要隔一段时间?因为在 preview 中有改动任何文件就会重新生成一次,这时侧边栏也要重新计算

3、减小文件大小

我的侧边栏有二级目录,整个侧边栏比较大,50篇时已经有20k。所以将侧边栏独立成一个文件,用js来load。

plugins/include_array.rb 中加:

1
2
3
4
5
59       fp = File.new('sidebar.html', 'w');
60       fp.puts(rtn);
61       fp.puts('<script src="/javascripts/category.js" type="text/javascript"></script>');
62       fp.puts('<script type="text/javascript"> hadOpenDiv();</script>');
63       fp.close();

source/_layout/post.html和source/_layout/page.html 的最后部分改成:

1
2
3
4
5
35 <aside class="sidebar" id='load_sidebar'>
36 </aside>
37 <script type="text/javascript">
38   $('#load_sidebar').load('/sidebar.html');
39 </script>

因为 source/index.html 里也有 { { include_array XXX } },所以不用担心不执行 plugins/include_array.rb。

问题1:就是"最近评论" 要从 _config.yml 中的 default_asides: 中移到 source/index.html 中。因为js load进的文件中的 document.write 不执行了。这也就是为什么 source/index.html 不采用js load。如果是 <aside> 中增加 div 用来 load 的话,侧边栏缩到底部就没有三列的效果。

问题2:sidebar.html 要写到主目录,source/sidebar.html 中用 ln 软链接到 sidebar.html。因为如果在watch状态直接写到 source/ 目录的话,他就会再次认为文件有改变,又重新生成。造成在不断生成的死循环。细节是在Rakefile文件的 generate 和 preview 中先加软链接再运行其他

1
2
3
56 task :generate do
57   system('>`pwd`/sidebar.html')
58   system('ln -f -s `pwd`/sidebar.html `pwd`/source/sidebar.html')

优化效果

在文件中加入时间输出代码

1
p "    #{ __FILE__} line:#{__LINE__} time:#{Time.now.to_f}"

50篇文章,40个tag,61个category,运行如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
kk@kk-laptop:~/kk/github/octopress(source)$ rake generate
## Generating Site with Jekyll
"/home/kk/kk/github/octopress/Rakefile line:62 time:1416933869.1204205"
identical source/stylesheets/screen.css 
"/home/kk/kk/github/octopress/Rakefile line:64 time:1416933870.019113"
Configuration file: /home/kk/kk/github/octopress/_config.yml
	        Source: source
	   Destination: public
	  Generating... 
"    /home/kk/kk/github/octopress/plugins/category_generator.rb line:110 time:1416933870.7509217"
"    /home/kk/kk/github/octopress/plugins/category_generator.rb line:114 time:1416933871.8838022"
"    /home/kk/kk/github/octopress/plugins/tag_generator.rb line:93 time:1416933871.8838577"
"    /home/kk/kk/github/octopress/plugins/tag_generator.rb line:97 time:1416933872.389736"
"    /usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/site.rb line:297 time:1416933872.4271524"
"    /usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/site.rb line:301 time:1416933873.8506286"
	                done.
 Auto-regeneration: disabled. Use --watch to enable.
"/home/kk/kk/github/octopress/Rakefile line:66 time:1416933873.9671116"
"total = 4.846851825714111"

total = 4.846851825714111,不优化要60s左右。

300篇

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
kk@kk-laptop:~/kk/github/octopress(source)$ rake generate
## Generating Site with Jekyll
"/home/kk/kk/github/octopress/Rakefile line:62 time:1426179501.0918927"
identical source/stylesheets/screen.css 
"/home/kk/kk/github/octopress/Rakefile line:64 time:1426179502.0703895"
Configuration file: /home/kk/kk/github/octopress/_config.yml
	        Source: source
	   Destination: public
	  Generating... 
"    /home/kk/kk/github/octopress/plugins/category_generator.rb line:110 time:1426179503.376222"
"    /home/kk/kk/github/octopress/plugins/category_generator.rb line:114 time:1426179508.2033086"
"    /home/kk/kk/github/octopress/plugins/tag_generator.rb line:93 time:1426179508.2033658"
"    /home/kk/kk/github/octopress/plugins/tag_generator.rb line:97 time:1426179508.3304708"
"    /usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/site.rb line:297 time:1426179508.3851612"
"    /usr/local/lib/ruby/gems/2.1.0/gems/jekyll-2.5.1/lib/jekyll/site.rb line:301 time:1426179515.877446"
	                done.
 Auto-regeneration: disabled. Use --watch to enable.
"/home/kk/kk/github/octopress/Rakefile line:66 time:1426179516.2085445"
"total = 15.116710424423218"