<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.4.3">Jekyll</generator><link href="https://yuehuanjue.github.io//atom.xml" rel="self" type="application/atom+xml" /><link href="https://yuehuanjue.github.io//" rel="alternate" type="text/html" /><updated>2017-05-02T06:45:22+00:00</updated><id>https://yuehuanjue.github.io//</id><title type="html">首页</title><subtitle>A lightweight site starter for Jekyll</subtitle><entry><title type="html">在Ubuntu14.04上搭建jekyll环境</title><link href="https://yuehuanjue.github.io//jekyll/init_jekyll_for_ubuntu/" rel="alternate" type="text/html" title="在Ubuntu14.04上搭建jekyll环境" /><published>2017-05-01T00:00:00+00:00</published><updated>2017-05-01T00:00:00+00:00</updated><id>https://yuehuanjue.github.io//jekyll/init_jekyll_for_ubuntu</id><content type="html" xml:base="https://yuehuanjue.github.io//jekyll/init_jekyll_for_ubuntu/">&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

&lt;p&gt;时隔五个月,又一次的开始写博客了. 在这五个月里, 真是变化万千,但是作为一个程序猿,我们还是离不开的努力和脚踏实地.&lt;/p&gt;

&lt;p&gt;古人常说,工欲善其事,必先利其器. 本着”利其器”的原则,  在现在的Ubuntu14.04的环境下, 搭建jekyll, 以便于随时更新一些学习笔记.&lt;/p&gt;

&lt;p&gt;然而,不用不知道,原来有那么多的坑,  好吧, 现在就来聊聊踩过的坑.&lt;/p&gt;

&lt;h2 id=&quot;搭建步骤&quot;&gt;搭建步骤:&lt;/h2&gt;

&lt;p&gt;像我这种使用Python完成项目的,只知其然,不知道其所以热的程序猿, 会经常使用ubuntu 系统, 为我们提供的apt-get指令, 曾一度认为, 在Ubuntu下, 搭建环境,就没有apt-get安装不好的, 然而我也将因为自己的幼稚的想法,而付出代价.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo apt-get install jekyll
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;当然, 用这个方法安装jekyll, 一般安装是没有问题的, 如果你遇到了问题, 就请自行Google吧,  这种概率实在是小. 但是安装好,不一定就可以使用了, 我们要去项目中,用jekyll server 跑一下看看.   如果你一次成功,那么恭喜你, 不用看下去了; 如果没成, 那么ok, 这是我们现在要讨论的部分.&lt;/p&gt;

&lt;p&gt;不知道,大家遇到的都是什么问题,  简单说一下我遇到的问题.&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;从路径上可以看出来的是, 使用apt-get 安装的jekyll, 默认使用的是/usr/lib/ruby的版本,  这个是系统自带的ruby.(在这里简单说一下, 使用jekyll, 要基于ruby的, 一般使用gem去安装的).
因为ruby版本的问题,  而产生的错误,  目前使用apt-get是解决不了的, 因为版本库里的ruby就到1.9.3版了,  貌似有一种不想升级的架势.&lt;/p&gt;

&lt;p&gt;so, 这里来提供另外一种解决方式.&lt;/p&gt;

&lt;p&gt;方法是使用rvm(试过这个方法不行的朋友,可以拐弯返航了)&lt;/p&gt;

&lt;p&gt;1, 使用 rvm -v 看一下系统有没有安装rvm&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;rvm 1.29.1 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io/]
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;2, 安装完rvm后, 使用rvm install 2.4, 来安装ruby.  这里要说的是,  我很好奇, 为什么只用版本号就默认的是安装ruby了啊,   猜想rvm是ruby的专属安装工具.&lt;/p&gt;

&lt;p&gt;这里可能没有那么顺利安装好,
更新过系统的朋友, 可能会遇到下面这个问题,&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;hoo@ubuntu:~/yuehuanjue.github.io$ rvm install 2.4
......
下列信息可能会对解决问题有所帮助：

下列软件包有未满足的依赖关系：
 libffi-dev : 依赖: libffi6 (= 3.1~rc1+r3.0.13-12) 但是 3.1~rc1+r3.0.13-12ubuntu0.1 正要被安装
E: 无法修正错误，因为您要求某些软件包保持现状，就是它们破坏了软件包间的依赖关系。
++ return 100
++ return 100
Requirements installation failed with status: 100.
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;意思就是, libffi6的版本过高, 需要一个低版本的.&lt;/p&gt;

&lt;p&gt;解决方案是;使用aptitude来调整,  用sudo apt-get install aptitude来安装, 使用是sudo aptitude install libffi-dev&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;....
下列软件包存在未满足的依赖关系：
 libffi-dev : 依赖: libffi6 (= 3.1~rc1+r3.0.13-12) 但是 3.1~rc1+r3.0.13-12ubuntu0.1 已安装。
下列动作将解决这些依赖关系：

     保持 下列软件包于其当前版本：
1)     libffi-dev [未安装的]      


是否接受该解决方案？[Y/n/q/?] n
下列动作将解决这些依赖关系：

     降级 下列软件包：                                                           
1)     libffi6 [3.1~rc1+r3.0.13-12ubuntu0.1 (now) -&amp;gt; 3.1~rc1+r3.0.13-12 (trusty)]



是否接受该解决方案？[Y/n/q/?] y
下列软件包将被“降级”：
  libffi6 
.....

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;这里很明显看到,aptitude会为我们提供各种解决方案.&lt;/p&gt;

&lt;p&gt;那么下面就好说了,  安装 rvm install 2.4 (pass)
使用2.4版本的ruby:  rvm use 2.4&lt;br /&gt;
安装jekyll: gem install jekyll&lt;/p&gt;

&lt;p&gt;安装好jekyll后, 我们jekyll server时, 会有个小错:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/home/hoo/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- bundler (LoadError)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;那么 我们需要 安装bundler: gem install bundler
安装好bundler后, 需要安装一下依赖:bundle install 
此后 jekyll  server 可以正常使用啦.
当然, 如果最后几步还有其他问题, 可以参考一下其他文章,  这里就不再重复了.  如果其他文章也没有解决, 那就是我还没有遇到过的情况了, 可以留言,我会亲自实验一下,找出正解.&lt;/p&gt;</content><author><name></name></author><category term="[&quot;jekyll&quot;]" /><category term="github" /><category term="jekyll" /><summary type="html">前言</summary></entry><entry><title type="html">Django orm的核心理论</title><link href="https://yuehuanjue.github.io//python/Django_orm%E7%9A%84%E6%A0%B8%E5%BF%83%E7%90%86%E8%AE%BA/" rel="alternate" type="text/html" title="Django orm的核心理论" /><published>2016-11-15T00:00:00+00:00</published><updated>2016-11-15T00:00:00+00:00</updated><id>https://yuehuanjue.github.io//python/Django_orm%E7%9A%84%E6%A0%B8%E5%BF%83%E7%90%86%E8%AE%BA</id><content type="html" xml:base="https://yuehuanjue.github.io//python/Django_orm%E7%9A%84%E6%A0%B8%E5%BF%83%E7%90%86%E8%AE%BA/">&lt;h2 id=&quot;1-什么是orm&quot;&gt;1 什么是orm？&lt;/h2&gt;

&lt;p&gt;ORM 即Object-Relational Mapping(对象关系映射)，它的作用是在&lt;code class=&quot;highlighter-rouge&quot;&gt;关系型数据库&lt;/code&gt;和&lt;code class=&quot;highlighter-rouge&quot;&gt;业务实体对象&lt;/code&gt;之间做一个&lt;code class=&quot;highlighter-rouge&quot;&gt;映射&lt;/code&gt;，这样我们在具体的操作业务对象的时候，就&lt;code class=&quot;highlighter-rouge&quot;&gt;不需要&lt;/code&gt;再去和复杂的SQL语句打交道，只需要简单的操作对象的属性和方法。&lt;/p&gt;

&lt;p&gt;Django ORM用到的三个类：Manager、QuerySet、Model。&lt;/p&gt;

&lt;p&gt;Manager：定义&lt;code class=&quot;highlighter-rouge&quot;&gt;表级方法&lt;/code&gt;（表级方法就是影响一条或多条记录的方法），我们可以以models.Manager为父类，定义自己的Manager，增强表级方法；&lt;/p&gt;

&lt;p&gt;QuerySet：Manager类的一些方法会返回QuerySet实例，QuerySet是一个&lt;code class=&quot;highlighter-rouge&quot;&gt;可遍历&lt;/code&gt;的结构，包含&lt;code class=&quot;highlighter-rouge&quot;&gt;一个或者多个元素&lt;/code&gt;，每个元素都是一个Model实例，它里面的方法也是&lt;code class=&quot;highlighter-rouge&quot;&gt;表级方法&lt;/code&gt;。Django给我们提供了&lt;code class=&quot;highlighter-rouge&quot;&gt;增加表级方法&lt;/code&gt;的途径，那就是&lt;code class=&quot;highlighter-rouge&quot;&gt;自定义Manager类&lt;/code&gt;，而不是自定义的QuerySet类，一般的我们没有自定义QuerySet类的必要；&lt;/p&gt;

&lt;p&gt;Model：django.db.models模块中的Model类，我们定义表的model时，就是继承它，它的功能很强大，通过自定义model的&lt;code class=&quot;highlighter-rouge&quot;&gt;instance&lt;/code&gt;可以获取外键实体等，它的方法都是&lt;code class=&quot;highlighter-rouge&quot;&gt;记录级方法&lt;/code&gt;（都是实例方法，貌似没有类方法），不要在里面定义类方法，比如计算记录的总数，查看所有记录，这些应该放在&lt;code class=&quot;highlighter-rouge&quot;&gt;自定义的manager类&lt;/code&gt;中。&lt;/p&gt;

&lt;h2 id=&quot;2-orm的优缺点&quot;&gt;2 ORM的优缺点&lt;/h2&gt;

&lt;p&gt;优点：&lt;code class=&quot;highlighter-rouge&quot;&gt;摆脱&lt;/code&gt;复杂的SQL操作，适应快速开发；让&lt;code class=&quot;highlighter-rouge&quot;&gt;数据结构&lt;/code&gt;变得简洁；数据库迁移成本更低（如从MySQL将数据迁移到Oracle中）&lt;/p&gt;

&lt;p&gt;缺点：&lt;code class=&quot;highlighter-rouge&quot;&gt;性能&lt;/code&gt;较差、&lt;code class=&quot;highlighter-rouge&quot;&gt;不适用&lt;/code&gt;于大型应用；复杂的SQL操作还&lt;code class=&quot;highlighter-rouge&quot;&gt;需要&lt;/code&gt;通过SQL语句实现。&lt;/p&gt;

&lt;h2 id=&quot;3-关联关系&quot;&gt;3 关联关系&lt;/h2&gt;

&lt;p&gt;ORM有&lt;code class=&quot;highlighter-rouge&quot;&gt;三种&lt;/code&gt;关联关系，分别为OneToOne（一对一），ManyToMany（多对多）， OneToMany（一对多，也就是我们常说的&lt;code class=&quot;highlighter-rouge&quot;&gt;外键&lt;/code&gt;）&lt;/p&gt;

&lt;h2 id=&quot;4-几种特点作者自己的总结&quot;&gt;4 几种特点（作者自己的总结）&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Filter机制：&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;多表的级联查询（‘__’）：&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Manager的方法扩展机制：&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;只有在使用QuerySet时，才会去数据库调用数据的机制：&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Q F的处理方式：&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;select_related() 和 prefetch_related()&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;5-select_related-和-prefetch_related-函数对-queryset-查询的优化&quot;&gt;5 select_related 和 prefetch_related 函数对 QuerySet 查询的优化&lt;/h2&gt;

&lt;h3 id=&quot;1select_related&quot;&gt;（1）select_related&lt;/h3&gt;

&lt;p&gt;对于一对一字段（OneToOneField）和外键字段（ForeignKey），可以使用select_related 来对QuerySet进行优化&lt;/p&gt;

&lt;h4 id=&quot;作用和方法&quot;&gt;作用和方法&lt;/h4&gt;

&lt;p&gt;在对QuerySet使用select_related()函数后，Django会获取相应外键对应的对象，从而在之后需要的时候不必再查询数据库了。以上例说明，如果我们需要打印数据库中的所有市及其所属省份，最直接的做法是：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; citys = City.objects.all()
&amp;gt;&amp;gt;&amp;gt; for c in citys:
...   print c.province
...
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;这样会导致线性的SQL查询，如果对象数量n太多，每个对象中有k个外键字段的话，就会导致n*k+1次SQL查询。在本例中，因为有3个city对象就导致了4次SQL查询：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
SELECT `QSOptimize_city`.`id`, `QSOptimize_city`.`name`, `QSOptimize_city`.`province_id`
FROM `QSOptimize_city`

SELECT `QSOptimize_province`.`id`, `QSOptimize_province`.`name` 
FROM `QSOptimize_province`
WHERE `QSOptimize_province`.`id` = 1 ;

SELECT `QSOptimize_province`.`id`, `QSOptimize_province`.`name` 
FROM `QSOptimize_province`
WHERE `QSOptimize_province`.`id` = 2 ;

SELECT `QSOptimize_province`.`id`, `QSOptimize_province`.`name` 
FROM `QSOptimize_province`
WHERE `QSOptimize_province`.`id` = 1 ;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;注：这里的SQL语句是直接从Django的logger:‘django.db.backends’输出出来的&lt;/p&gt;

&lt;p&gt;如果我们使用select_related()函数：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; citys = City.objects.select_related().all()
&amp;gt;&amp;gt;&amp;gt; for c in citys:
...   print c.province
...
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;就只有一次SQL查询，显然大大减少了SQL查询的次数：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SELECT `QSOptimize_city`.`id`, `QSOptimize_city`.`name`, 
`QSOptimize_city`.`province_id`, `QSOptimize_province`.`id`, `QSOptimize_province`.`name` 
FROM`QSOptimize_city` 
INNER JOIN `QSOptimize_province` ON (`QSOptimize_city`.`province_id` = `QSOptimize_province`.`id`) ;

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;这里我们可以看到，Django使用了INNER JOIN来获得省份的信息。顺便一提这条SQL查询得到的结果如下：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;+----+-----------+-------------+----+-----------+
| id | name      | province_id | id | name      |
+----+-----------+-------------+----+-----------+
|  1 | 武汉市    |           1 |  1 | 湖北省    |
|  2 | 广州市    |           2 |  2 | 广东省    |
|  3 | 十堰市    |           1 |  1 | 湖北省    |
+----+-----------+-------------+----+-----------+
3 rows in set (0.00 sec)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h4 id=&quot;使用方法&quot;&gt;使用方法&lt;/h4&gt;

&lt;p&gt;函数支持如下三种用法：
*fields 参数&lt;/p&gt;

&lt;p&gt;select_related() 接受可变长参数，每个参数是需要获取的外键（父表的内容）的字段名，以及外键的外键的字段名、外键的外键的外键…。若要选择外键的外键需要使用两个下划线“__”来连接。&lt;/p&gt;

&lt;p&gt;例如我们要获得张三的现居省份，可以用如下方式：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; zhangs = Person.objects.select_related('living__province').get(firstname=u&quot;张&quot;,lastname=u&quot;三&quot;)
&amp;gt;&amp;gt;&amp;gt; zhangs.living.province
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;触发的SQL查询如下：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SELECT `QSOptimize_person`.`id`, `QSOptimize_person`.`firstname`, 
`QSOptimize_person`.`lastname`, `QSOptimize_person`.`hometown_id`, `QSOptimize_person`.`living_id`, 
`QSOptimize_city`.`id`, `QSOptimize_city`.`name`, `QSOptimize_city`.`province_id`, `QSOptimize_province`.`id`, 
`QSOptimize_province`.`name` 
FROM `QSOptimize_person` 
INNER JOIN `QSOptimize_city` ON (`QSOptimize_person`.`living_id` = `QSOptimize_city`.`id`) 
INNER JOIN `QSOptimize_province` ON (`QSOptimize_city`.`province_id` = `QSOptimize_province`.`id`) 
WHERE (`QSOptimize_person`.`lastname` = '三'  AND `QSOptimize_person`.`firstname` = '张' );
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;可以看到，Django使用了2次 INNER JOIN 来完成请求，获得了city表和province表的内容并添加到结果表的相应列，这样在调用 zhangs.living的时候也不必再次进行SQL查询。&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;+----+-----------+----------+-------------+-----------+----+-----------+-------------+----+-----------+
| id | firstname | lastname | hometown_id | living_id | id | name      | province_id | id | name      |
+----+-----------+----------+-------------+-----------+----+-----------+-------------+----+-----------+
|  1 | 张        | 三       |           3 |         1 |  1  | 武汉市     |   1         |  1 | 湖北省     |
+----+-----------+----------+-------------+-----------+----+-----------+-------------+----+-----------+
1 row in set (0.00 sec)

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;然而，未指定的外键则不会被添加到结果中。这时候如果需要获取张三的故乡就会进行SQL查询了：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; zhangs.hometown.province
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SELECT `QSOptimize_city`.`id`, `QSOptimize_city`.`name`, 
`QSOptimize_city`.`province_id` 
FROM `QSOptimize_city` 
WHERE `QSOptimize_city`.`id` = 3 ;

SELECT `QSOptimize_province`.`id`, `QSOptimize_province`.`name` 
FROM `QSOptimize_province` 
WHERE `QSOptimize_province`.`id` = 1
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;同时，如果不指定外键，就会进行两次查询。如果深度更深，查询的次数更多。&lt;/p&gt;

&lt;p&gt;值得一提的是，从Django 1.7开始，select_related()函数的作用方式改变了。在本例中，如果要同时获得张三的故乡和现居地的省份，在1.7以前你只能这样做：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; zhangs = Person.objects.select_related('hometown__province','living__province').get(firstname=u&quot;张&quot;,lastname=u&quot;三&quot;)
&amp;gt;&amp;gt;&amp;gt; zhangs.hometown.province
&amp;gt;&amp;gt;&amp;gt; zhangs.living.province

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;但是1.7及以上版本，你可以像和queryset的其他函数一样进行链式操作：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; zhangs = Person.objects.select_related('hometown__province').select_related('living__province').get(firstname=u&quot;张&quot;,lastname=u&quot;三&quot;)
&amp;gt;&amp;gt;&amp;gt; zhangs.hometown.province
&amp;gt;&amp;gt;&amp;gt; zhangs.living.province
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;如果你在1.7以下版本这样做了，你只会获得最后一个操作的结果，在本例中就是只有现居地而没有故乡。在你打印故乡省份的时候就会造成两次SQL查询。&lt;/p&gt;

&lt;h4 id=&quot;depth-参数&quot;&gt;depth 参数&lt;/h4&gt;

&lt;p&gt;select_related() 接受depth参数，depth参数可以确定select_related的深度。Django会递归遍历指定深度内的所有的OneToOneField和ForeignKey。以本例说明：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; zhangs = Person.objects.select_related(depth = d)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;d=1  相当于 select_related(‘hometown’,’living’)&lt;/p&gt;

&lt;p&gt;d=2  相当于 select_related(‘hometown__province’,’living__province’)&lt;/p&gt;

&lt;h4 id=&quot;无参数&quot;&gt;无参数&lt;/h4&gt;

&lt;p&gt;select_related() 也可以不加参数，这样表示要求Django尽可能深的select_related。例如：zhangs = Person.objects.select_related().get(firstname=u”张”,lastname=u”三”)。但要注意两点：&lt;/p&gt;

&lt;p&gt;Django本身内置一个上限，对于特别复杂的表关系，Django可能在你不知道的某处跳出递归，从而与你想的做法不一样。具体限制是怎么工作的我表示不清楚。
Django并不知道你实际要用的字段有哪些，所以会把所有的字段都抓进来，从而会造成不必要的浪费而影响性能。&lt;/p&gt;

&lt;h4 id=&quot;小结&quot;&gt;小结&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;select_related主要针一对一和多对一关系进行优化。&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;select_related使用SQL的JOIN语句进行优化，通过减少SQL查询的次数来进行优化、提高性能。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;可以通过可变长参数指定需要select_related的字段名。也可以通过使用双下划线“__”连接字段名来实现指定的递归查询。没有指定的字段不会缓存，没有指定的深度不会缓存，如果要访问的话Django会再次进行SQL查询。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;也可以通过depth参数指定递归的深度，Django会自动缓存指定深度内所有的字段。如果要访问指定深度外的字段，Django会再次进行SQL查询。&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;也接受无参数的调用，Django会尽可能深的递归查询所有的字段。但注意有Django递归的限制和性能的浪费。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;jango &amp;gt;= 1.7，链式调用的select_related相当于使用可变长参数。Django &amp;lt; 1.7，链式调用会导致前边的select_related失效，只保留最后一个。&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;2prefetch_related&quot;&gt;（2）prefetch_related&lt;/h3&gt;

&lt;p&gt;对于多对多字段（ManyToManyField）和一对多字段，可以使用prefetch_related()来进行优化。或许你会说，没有一个叫OneToManyField的东西啊。实际上 ，ForeignKey就是一个多对一的字段，而被ForeignKey关联的字段就是一对多字段了。&lt;/p&gt;

&lt;h4 id=&quot;作用和方法-1&quot;&gt;作用和方法&lt;/h4&gt;

&lt;p&gt;prefetch_related()和select_related()的设计目的很相似，都是为了减少SQL查询的数量，但是实现的方式不一样。后者是通过JOIN语句，在SQL查询内解决问题。但是对于多对多关系，使用SQL语句解决就显得有些不太明智，因为JOIN得到的表将会很长，会导致SQL语句运行时间的增加和内存占用的增加。若有n个对象，每个对象的多对多字段对应Mi条，就会生成Σ(n)Mi 行的结果表。&lt;/p&gt;

&lt;p&gt;prefetch_related()的解决方法是，分别查询每个表，然后用Python处理他们之间的关系。继续以上边的例子进行说明，如果我们要获得张三所有去过的城市，使用prefetch_related()应该是这么做：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; zhangs = Person.objects.prefetch_related('visitation').get(firstname=u&quot;张&quot;,lastname=u&quot;三&quot;)
&amp;gt;&amp;gt;&amp;gt; for city in zhangs.visitation.all() :
...   print city
...
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;上述代码触发的SQL查询如下：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;SELECT `QSOptimize_person`.`id`, `QSOptimize_person`.`firstname`,
`QSOptimize_person`.`lastname`, `QSOptimize_person`.`hometown_id`, `QSOptimize_person`.`living_id` 
FROM `QSOptimize_person` 
WHERE (`QSOptimize_person`.`lastname` = '三'  AND `QSOptimize_person`.`firstname` = '张'); 

SELECT (`QSOptimize_person_visitation`.`person_id`) AS `_prefetch_related_val`, `QSOptimize_city`.`id`, 
`QSOptimize_city`.`name`, `QSOptimize_city`.`province_id` 
FROM `QSOptimize_city` 
INNER JOIN `QSOptimize_person_visitation` ON (`QSOptimize_city`.`id` = `QSOptimize_person_visitation`.`city_id`)
WHERE `QSOptimize_person_visitation`.`person_id` IN (1);
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;第一条SQL查询仅仅是获取张三的Person对象，第二条比较关键，它选取关系表QSOptimize_person_visitation中person_id为张三的行，然后和city表内联（INNER JOIN 也叫等值连接）得到结果表。&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;+----+-----------+----------+-------------+-----------+
| id | firstname | lastname | hometown_id | living_id |
+----+-----------+----------+-------------+-----------+
|  1 | 张        | 三       |           3 |         1 |
+----+-----------+----------+-------------+-----------+
1 row in set (0.00 sec)

+-----------------------+----+-----------+-------------+
| _prefetch_related_val | id | name      | province_id |
+-----------------------+----+-----------+-------------+
|                     1 |  1 | 武汉市    |           1 |
|                     1 |  2 | 广州市    |           2 |
|                     1 |  3 | 十堰市    |           1 |
+-----------------------+----+-----------+-------------+
3 rows in set (0.00 sec)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;显然张三武汉、广州、十堰都去过。&lt;/p&gt;

&lt;p&gt;又或者，我们要获得湖北的所有城市名，可以这样：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; hb = Province.objects.prefetch_related('city_set').get(name__iexact=u&quot;湖北省&quot;)
&amp;gt;&amp;gt;&amp;gt; for city in hb.city_set.all():
...   city.name
...
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;触发的SQL查询：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
SELECT `QSOptimize_province`.`id`, `QSOptimize_province`.`name` 
FROM `QSOptimize_province` 
WHERE `QSOptimize_province`.`name` LIKE '湖北省' ;

SELECT `QSOptimize_city`.`id`, `QSOptimize_city`.`name`, `QSOptimize_city`.`province_id` 
FROM `QSOptimize_city` 
WHERE `QSOptimize_city`.`province_id` IN (1);
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;得到的表：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;+----+-----------+
| id | name      |
+----+-----------+
|  1 | 湖北省    |
+----+-----------+
1 row in set (0.00 sec)

+----+-----------+-------------+
| id | name      | province_id |
+----+-----------+-------------+
|  1 | 武汉市    |           1 |
|  3 | 十堰市    |           1 |
+----+-----------+-------------+
2 rows in set (0.00 sec)

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;我们可以看见，prefetch使用的是 IN 语句实现的。这样，在QuerySet中的对象数量过多的时候，根据数据库特性的不同有可能造成性能问题。&lt;/p&gt;

&lt;h4 id=&quot;使用方法-1&quot;&gt;使用方法&lt;/h4&gt;

&lt;h5 id=&quot;lookups-参数&quot;&gt;*lookups 参数&lt;/h5&gt;

&lt;p&gt;prefetch_related()在Django &amp;lt; 1.7 只有这一种用法。和select_related()一样，prefetch_related()也支持深度查询，例如要获得所有姓张的人去过的省：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; zhangs = Person.objects.prefetch_related('visitation__province').filter(firstname__iexact=u'张')
&amp;gt;&amp;gt;&amp;gt; for i in zhangs:
...   for city in i.visitation.all():
...     print city.province
...

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;触发的SQL：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
SELECT `QSOptimize_person`.`id`, `QSOptimize_person`.`firstname`, 
`QSOptimize_person`.`lastname`, `QSOptimize_person`.`hometown_id`, `QSOptimize_person`.`living_id` 
FROM `QSOptimize_person` 
WHERE `QSOptimize_person`.`firstname` LIKE '张' ;

SELECT (`QSOptimize_person_visitation`.`person_id`) AS `_prefetch_related_val`, `QSOptimize_city`.`id`,
`QSOptimize_city`.`name`, `QSOptimize_city`.`province_id` FROM `QSOptimize_city` 
INNER JOIN `QSOptimize_person_visitation` ON (`QSOptimize_city`.`id` = `QSOptimize_person_visitation`.`city_id`)
WHERE `QSOptimize_person_visitation`.`person_id` IN (1, 4);

SELECT `QSOptimize_province`.`id`, `QSOptimize_province`.`name` 
FROM `QSOptimize_province` 
WHERE `QSOptimize_province`.`id` IN (1, 2);

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;获得的结果：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
+----+-----------+----------+-------------+-----------+
| id | firstname | lastname | hometown_id | living_id |
+----+-----------+----------+-------------+-----------+
|  1 | 张        | 三       |           3 |         1 |
|  4 | 张        | 六       |           2 |         2 |
+----+-----------+----------+-------------+-----------+
2 rows in set (0.00 sec)

+-----------------------+----+-----------+-------------+
| _prefetch_related_val | id | name      | province_id |
+-----------------------+----+-----------+-------------+
|                     1 |  1 | 武汉市    |           1 |
|                     1 |  2 | 广州市    |           2 |
|                     4 |  2 | 广州市    |           2 |
|                     1 |  3 | 十堰市    |           1 |
+-----------------------+----+-----------+-------------+
4 rows in set (0.00 sec)

+----+-----------+
| id | name      |
+----+-----------+
|  1 | 湖北省    |
|  2 | 广东省    |
+----+-----------+
2 rows in set (0.00 sec)

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;值得一提的是，链式prefetch_related会将这些查询添加起来，就像1.7中的select_related那样。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;要注意的是&lt;/code&gt;，在使用QuerySet的时候，一旦在链式操作中改变了数据库请求，之前用prefetch_related缓存的数据将会被&lt;code class=&quot;highlighter-rouge&quot;&gt;忽略掉&lt;/code&gt;。这会导致Django重新请求数据库来获得相应的数据，从而造成性能问题。这里提到的改变数据库请求指各种filter()、exclude()等等最终会改变SQL代码的操作。而all()并不会改变最终的数据库请求，因此是不会导致重新请求数据库的。&lt;/p&gt;

&lt;p&gt;举个例子，要获取所有人访问过的城市中带有“市”字的城市，这样做会导致大量的SQL查询：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
plist = Person.objects.prefetch_related('visitation')
[p.visitation.filter(name__icontains=u&quot;市&quot;) for p in plist]

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;因为数据库中有4人，导致了2+4次SQL查询：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
SELECT `QSOptimize_person`.`id`, `QSOptimize_person`.`firstname`, `QSOptimize_person`.`lastname`, 
`QSOptimize_person`.`hometown_id`, `QSOptimize_person`.`living_id` 
FROM `QSOptimize_person`;

SELECT (`QSOptimize_person_visitation`.`person_id`) AS `_prefetch_related_val`, `QSOptimize_city`.`id`,
`QSOptimize_city`.`name`, `QSOptimize_city`.`province_id` 
FROM `QSOptimize_city` 
INNER JOIN `QSOptimize_person_visitation` ON (`QSOptimize_city`.`id` = `QSOptimize_person_visitation`.`city_id`)
WHERE `QSOptimize_person_visitation`.`person_id` IN (1, 2, 3, 4);

SELECT `QSOptimize_city`.`id`, `QSOptimize_city`.`name`, `QSOptimize_city`.`province_id` 
FROM `QSOptimize_city` 
INNER JOIN `QSOptimize_person_visitation` ON (`QSOptimize_city`.`id` = `QSOptimize_person_visitation`.`city_id`) 
WHERE(`QSOptimize_person_visitation`.`person_id` = 1  AND `QSOptimize_city`.`name` LIKE '%市%' );

SELECT `QSOptimize_city`.`id`, `QSOptimize_city`.`name`, `QSOptimize_city`.`province_id` 
FROM `QSOptimize_city` 
INNER JOIN `QSOptimize_person_visitation` ON (`QSOptimize_city`.`id` = `QSOptimize_person_visitation`.`city_id`) 
WHERE (`QSOptimize_person_visitation`.`person_id` = 2  AND `QSOptimize_city`.`name` LIKE '%市%' ); 

SELECT `QSOptimize_city`.`id`, `QSOptimize_city`.`name`, `QSOptimize_city`.`province_id` 
FROM `QSOptimize_city`
INNER JOIN `QSOptimize_person_visitation` ON (`QSOptimize_city`.`id` = `QSOptimize_person_visitation`.`city_id`) 
WHERE (`QSOptimize_person_visitation`.`person_id` = 3  AND `QSOptimize_city`.`name` LIKE '%市%' );

SELECT `QSOptimize_city`.`id`, `QSOptimize_city`.`name`, `QSOptimize_city`.`province_id` 
FROM `QSOptimize_city` 
INNER JOIN `QSOptimize_person_visitation` ON (`QSOptimize_city`.`id` = `QSOptimize_person_visitation`.`city_id`) 
WHERE (`QSOptimize_person_visitation`.`person_id` = 4  AND `QSOptimize_city`.`name` LIKE '%市%' );

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;详细分析一下这些请求事件。&lt;/p&gt;

&lt;p&gt;众所周知，QuerySet是lazy的，要用的时候才会去访问数据库。运行到第二行Python代码时，for循环将plist看做iterator，这会触发数据库查询。最初的两次SQL查询就是prefetch_related导致的。&lt;/p&gt;

&lt;p&gt;虽然已经查询结果中包含所有所需的city的信息，但因为在循环体中对Person.visitation进行了filter操作，这显然改变了数据库请求。因此这些操作会忽略掉之前缓存到的数据，重新进行SQL查询。&lt;/p&gt;

&lt;p&gt;但是如果有这样的需求了应该怎么办呢？在Django &amp;gt;= 1.7，可以通过下一节的Prefetch对象来实现，如果你的环境是Django &amp;lt; 1.7，可以在Python中完成这部分操作。&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;plist = Person.objects.prefetch_related('visitation')
[[city for city in p.visitation.all() if u&quot;市&quot; in city.name] for p in plist]

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h5 id=&quot;prefetch-对象&quot;&gt;Prefetch 对象&lt;/h5&gt;

&lt;p&gt;在Django &amp;gt;= 1.7，可以用Prefetch对象来控制prefetch_related函数的行为。&lt;/p&gt;

&lt;p&gt;注：由于我没有安装1.7版本的Django环境，本节内容是参考Django文档写的，没有进行实际的测试。&lt;/p&gt;

&lt;h5 id=&quot;prefetch对象的特征&quot;&gt;Prefetch对象的特征：&lt;/h5&gt;

&lt;p&gt;一个Prefetch对象只能指定一项prefetch操作。
Prefetch对象对字段指定的方式和prefetch_related中的参数相同，都是通过双下划线连接的字段名完成的。
可以通过 queryset 参数手动指定prefetch使用的QuerySet。
可以通过 to_attr 参数指定prefetch到的属性名。
Prefetch对象和字符串形式指定的lookups参数可以混用。&lt;/p&gt;

&lt;p&gt;继续上面的例子，获取所有人访问过的城市中带有“武”字和“州”的城市：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wus = City.objects.filter(name__icontains = u&quot;武&quot;)
zhous = City.objects.filter(name__icontains = u&quot;州&quot;)
plist = Person.objects.prefetch_related(
    Prefetch('visitation', queryset = wus, to_attr = &quot;wu_city&quot;),
    Prefetch('visitation', queryset = zhous, to_attr = &quot;zhou_city&quot;),)
[p.wu_city for p in plist]
[p.zhou_city for p in plist]

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;注：这段代码没有在实际环境中测试过，若有不正确的地方请指正。&lt;/p&gt;

&lt;p&gt;顺带一提，Prefetch对象和字符串参数可以混用。&lt;/p&gt;

&lt;h5 id=&quot;none&quot;&gt;None&lt;/h5&gt;

&lt;p&gt;可以通过传入一个None来清空之前的prefetch_related。就像这样：&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; prefetch_cleared_qset = qset.prefetch_related(None)

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h4 id=&quot;小结-1&quot;&gt;小结&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;prefetch_related主要针一对多和多对多关系进行优化。&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;prefetch_related通过分别获取各个表的内容，然后用Python处理他们之间的关系来进行优化。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;可以通过可变长参数指定需要select_related的字段名。指定方式和特征与select_related是相同的。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;在Django &amp;gt;= 1.7可以通过Prefetch对象来实现复杂查询，但低版本的Django好像只能自己实现。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;作为prefetch_related的参数，Prefetch对象和字符串可以混用。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;prefetch_related的链式调用会将对应的prefetch添加进去，而非替换，似乎没有基于不同版本上区别。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;可以通过传入None来清空之前的prefetch_related。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;3select_related-和-prefetch_related-函数比较&quot;&gt;（3）select_related 和 prefetch_related 函数比较&lt;/h3&gt;

&lt;h4 id=&quot;区别&quot;&gt;区别&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;因为select_related()总是在单次SQL查询中解决问题，而prefetch_related()会对每个相关表进行SQL查询，因此select_related()的效率通常比后者高。&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;鉴于第一条，尽可能的用select_related()解决问题。只有在select_related()不能解决问题的时候再去想prefetch_related()。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;你可以在一个QuerySet中同时使用select_related()和prefetch_related()，从而减少SQL查询的次数。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;只有prefetch_related()之前的select_related()是有效的，之后的将会被无视掉。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;</content><author><name></name></author><summary type="html">1 什么是orm？</summary></entry><entry><title type="html">Supervisor插件</title><link href="https://yuehuanjue.github.io//django%E6%8F%92%E4%BB%B6/%E8%BF%90%E7%BB%B4/supervisor/" rel="alternate" type="text/html" title="Supervisor插件" /><published>2016-10-28T00:00:00+00:00</published><updated>2016-10-28T00:00:00+00:00</updated><id>https://yuehuanjue.github.io//django%E6%8F%92%E4%BB%B6/%E8%BF%90%E7%BB%B4/supervisor</id><content type="html" xml:base="https://yuehuanjue.github.io//django%E6%8F%92%E4%BB%B6/%E8%BF%90%E7%BB%B4/supervisor/">&lt;p&gt;###&lt;/p&gt;

&lt;p&gt;####一、安装指令&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip install supervisor


&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;####二、配置环境&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;echo_supervisord_conf &amp;gt; supervisor.conf  # 生成 supervisor 默认配置文件

vim  supervisor.conf                   # 修改 supervisor 配置文件，添加 gunicorn 进程管理

在 supervisor.conf 底部下添加 myweb.py 的配置 `/home/***/supervisor/super` 项目目录」

添加如下：
[program:***]   # ***代表进程的名字，用ps aux可查询
command=/home/**/supervisor/super/bin/gunicorn -w 4 -b 0.0.0.0:8000 myweb:app                                                                     
directory=/home/wang/supervisor                                             
startsecs=0                                                                   
stopwaitsecs=0                                                                  
autostart=false                                                                
autorestart=false                                                                 
user= 。。                                                                     
stdout_logfile=/home/wang/supervisor/log/gunicorn.log                  
stderr_logfile=/home/wang/supervisor/log/gunicorn.err

注：注释是;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;####三、常见的命令&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;supervisord -c supervisor.conf                           查看supervisor的运行状态

supervisorctl -c supervisor.conf status                  查看supervisor的状态                        
              
supervisorctl -c supervisor.conf reload                  重新载入 配置文件

supervisorctl -c supervisor.conf start [all]|[appname]   启动指定/所有 supervisor 管理的程序进程

supervisorctl -c supervisor.conf stop [all]|[appname]    关闭指定/所有 supervisor 管理的程序进程

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;####四、web管理界面的配置&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[inet_http_server]     ; inet (TCP) server disabled by default

port=127.0.0.1:9001    ; (ip_address:port specifier, *:port for alliface)

username=。。          ; (default is no username (open server)

password=123           ; (default is no password (open server))

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket

serverurl=http://127.0.0.1:9001       ; use an http:// url to specify an inet socket

username=。。                         ; should be same as http_username if set

password=123                          ; should be same as http_password if set

;prompt=mysupervisor                  ; cmd line prompt (default &quot;supervisor&quot;)

;history_file=~/.sc_history           ; use readline history if available

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;####五、运行supervisor&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;supervisord -c supervisor.conf

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;</content><author><name></name></author><summary type="html">###</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://yuehuanjue.github.io//%7B%22feature%22=%3Enil,%20%22teaser%22=%3Enil,%20%22credit%22=%3E%22yuehuanjue%22%7D" /></entry><entry><title type="html">Git常用命令</title><link href="https://yuehuanjue.github.io//git/git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" rel="alternate" type="text/html" title="Git常用命令" /><published>2016-10-20T00:00:00+00:00</published><updated>2016-10-20T00:00:00+00:00</updated><id>https://yuehuanjue.github.io//git/git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4</id><content type="html" xml:base="https://yuehuanjue.github.io//git/git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/">&lt;h3 id=&quot;查看添加提交删除找回重置修改文件&quot;&gt;查看、添加、提交、删除、找回，重置修改文件&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git help &amp;lt; command&amp;gt; # 显示command的help

git show # 显示某次提交的内容 git show $id

git co -- &amp;lt; file&amp;gt; # 抛弃工作区修改

git co . # 抛弃工作区修改

git add &amp;lt; file&amp;gt; # 将工作文件修改提交到本地暂存区

git add . # 将所有修改过的工作文件提交暂存区

git rm &amp;lt; file&amp;gt; # 从版本库中删除文件

git rm &amp;lt; file&amp;gt; --cached # 从版本库中删除文件，但不删除文件

git reset &amp;lt; file&amp;gt; # 从暂存区恢复到工作文件
33
git reset -- . # 从暂存区恢复到工作文件

git reset --hard # 恢复最近一次提交过的状态，即放弃上次提交后的所有本次修改

git ci &amp;lt; file&amp;gt; git ci . git ci -a # 将git add, git rm和git ci等操作都合并在一起做　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　　git ci -am &quot;some comments&quot;

git ci --amend # 修改最后一次提交记录

git revert &amp;lt;$id&amp;gt; # 恢复某次提交的状态，恢复动作本身也创建次提交对象

git revert HEAD # 恢复最后一次提交的状态
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;查看文件diff&quot;&gt;查看文件diff&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
git diff &amp;lt;file&amp;gt; # 比较当前文件和暂存区文件差异 git diff

git diff &amp;lt;id1&amp;gt;&amp;lt;id1&amp;gt;&amp;lt;id2&amp;gt; # 比较两次提交之间的差异

git diff &amp;lt;branch1&amp;gt;..&amp;lt;branch2&amp;gt; # 在两个分支之间比较

git diff --staged # 比较暂存区和版本库差异

git diff --cached # 比较暂存区和版本库差异

git diff --stat # 仅仅比较统计信息
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;查看提交记录&quot;&gt;查看提交记录.&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
git log   git log &amp;lt; file&amp;gt; # 查看该文件每次提交记录

git log -p &amp;lt; file&amp;gt; # 查看每次详细修改内容的diff

git log -p -2 # 查看最近两次详细修改内容的diff

git log --stat #查看提交统计信息
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;tig&quot;&gt;tig&lt;/h3&gt;

&lt;p&gt;Mac上可以使用tig代替diff和log，brew install tig&lt;/p&gt;

&lt;h2 id=&quot;git-本地分支管理&quot;&gt;Git 本地分支管理&lt;/h2&gt;

&lt;h3 id=&quot;查看切换创建和删除分支&quot;&gt;查看、切换、创建和删除分支&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
git br -r # 查看远程分支

git br &amp;lt;new_branch&amp;gt; # 创建新的分支

git br -v # 查看各个分支最后提交信息

git br --merged # 查看已经被合并到当前分支的分支

git br --no-merged # 查看尚未被合并到当前分支的分支

git co &amp;lt; branch&amp;gt; # 切换到某个分支

git co -b &amp;lt;new_branch&amp;gt; # 创建新的分支，并且切换过去

git co -b &amp;lt;new_branch&amp;gt; &amp;lt; branch&amp;gt; # 基于branch创建新的new_branch

git co $id # 把某次历史提交记录checkout出来，但无分支信息，切换到其他分支会自动删除

git co $id -b &amp;lt;new_branch&amp;gt; # 把某次历史提交记录checkout出来，创建成一个分支

git br -d &amp;lt;branch&amp;gt; # 删除某个分支

git br -D &amp;lt;branch&amp;gt; # 强制删除某个分支 (未被合并的分支被删除的时候需要强制)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;分支合并和rebase&quot;&gt;分支合并和rebase&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git merge &amp;lt;branch&amp;gt; # 将branch分支合并到当前分支

git merge origin/master --no-ff # 不要Fast-Foward合并，这样可以生成merge提交

git rebase master &amp;lt;branch&amp;gt; # 将master rebase到branch，相当于： git co &amp;lt;branch&amp;gt; &amp;amp;&amp;amp; git rebase master &amp;amp;&amp;amp; git co master &amp;amp;&amp;amp; git merge &amp;lt;branch&amp;gt;

 Git补丁管理(方便在多台机器上开发同步时用)

git diff &amp;gt; ../sync.patch # 生成补丁

git apply ../sync.patch # 打补丁

git apply --check ../sync.patch #测试补丁能否成功
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;git暂存管理&quot;&gt;Git暂存管理&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git stash # 暂存

git stash list # 列所有stash

git stash apply # 恢复暂存的内容

git stash drop # 删除暂存区
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;git远程分支管理&quot;&gt;Git远程分支管理&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git pull # 抓取远程仓库所有分支更新并合并到本地

git pull --no-ff # 抓取远程仓库所有分支更新并合并到本地，不要快进合并

git fetch origin # 抓取远程仓库更新

git merge origin/master # 将远程主分支合并到本地当前分支

git co --track origin/branch # 跟踪某个远程分支创建相应的本地分支

git co -b &amp;lt;local_branch&amp;gt; origin/&amp;lt;remote_branch&amp;gt; # 基于远程分支创建本地分支，功能同上

git push # push所有分支

git push origin master # 将本地主分支推到远程主分支

git push -u origin master # 将本地主分支推到远程(如无远程主分支则创建，用于初始化远程仓库)

git push origin &amp;lt;local_branch&amp;gt; # 创建远程分支， origin是远程仓库名

git push origin &amp;lt;local_branch&amp;gt;:&amp;lt;remote_branch&amp;gt; # 创建远程分支

git push origin :&amp;lt;remote_branch&amp;gt; #先删除本地分支(git br -d &amp;lt;branch&amp;gt;)，然后再push删除远程分支
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;git远程仓库管理&quot;&gt;Git远程仓库管理&lt;/h3&gt;

&lt;h3 id=&quot;github&quot;&gt;GitHub&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git remote -v # 查看远程服务器地址和仓库名称

git remote show origin # 查看远程服务器仓库状态

git remote add origin git@ github:robbin/robbin_site.git # 添加远程仓库地址

git remote set-url origin git@ github.com:robbin/robbin_site.git # 设置远程仓库地址(用于修改远程仓库地址) git remote rm &amp;lt;repository&amp;gt; # 删除远程仓库
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;创建远程仓库&quot;&gt;创建远程仓库&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone --bare robbin_site robbin_site.git # 用带版本的项目创建纯版本仓库

scp -r my_project.git git@ git.csdn.net:~ # 将纯仓库上传到服务器上

mkdir robbin_site.git &amp;amp;&amp;amp; cd robbin_site.git &amp;amp;&amp;amp; git --bare init # 在服务器创建纯仓库

git remote add origin git@ github.com:robbin/robbin_site.git # 设置远程仓库地址

git push -u origin master # 客户端首次提交

git push -u origin develop # 首次将本地develop分支提交到远程develop分支，并且track

git remote set-head origin master # 设置远程仓库的HEAD指向master分支

也可以命令设置跟踪远程库和本地库

git branch --set-upstream master origin/master

git branch --set-upstream develop origin/develop
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;文章来源于博客：http://www.cnblogs.com/cspku/articles/Git_cmds.html  想了解原文的可以去看一下。&lt;/p&gt;</content><author><name></name></author><summary type="html">查看、添加、提交、删除、找回，重置修改文件</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://yuehuanjue.github.io//%7B%22feature%22=%3E%22git.png%22,%20%22teaser%22=%3E%22git.png%22,%20%22credit%22=%3E%22yuehuanjue%22%7D" /></entry><entry><title type="html">HTTP协议（入门）</title><link href="https://yuehuanjue.github.io//http%E5%8D%8F%E8%AE%AE/basic_open_class_one/" rel="alternate" type="text/html" title="HTTP协议（入门）" /><published>2016-10-20T00:00:00+00:00</published><updated>2016-10-20T00:00:00+00:00</updated><id>https://yuehuanjue.github.io//http%E5%8D%8F%E8%AE%AE/basic_open_class_one</id><content type="html" xml:base="https://yuehuanjue.github.io//http%E5%8D%8F%E8%AE%AE/basic_open_class_one/">&lt;h2 id=&quot;http-协议是互联网的基础协议也是网页开发的必备知识最新版本-http2-更是让它成为技术热点&quot;&gt;HTTP 协议是互联网的基础协议，也是网页开发的必备知识，最新版本 HTTP/2 更是让它成为技术热点。&lt;/h2&gt;

&lt;h3 id=&quot;一http09&quot;&gt;一、http（0.9）&lt;/h3&gt;

&lt;p&gt;HTTP是基于&lt;strong&gt;TCP/IP&lt;/strong&gt;协议的应用层协议。它不涉及数据包（packet）的传输，主要规定了客户端和服务器之间的通信格式，默认使用80端口。&lt;/p&gt;

&lt;p&gt;最早版本是1991年发布的0.9版，该版本只提供了命令GET。&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;GET/index.html

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;该命令表示，TCP连接建立后，客户端向服务器请求（request）网页index.html。&lt;/p&gt;

&lt;p&gt;此时的协议规定，服务器只能回应HTML格式的字符串，不能回应别的格式。&lt;/p&gt;

&lt;p&gt;服务器发送&lt;strong&gt;完毕&lt;/strong&gt;，就__关闭__了TCP的连接。&lt;/p&gt;

&lt;h3 id=&quot;二http10&quot;&gt;二、http(1.0)&lt;/h3&gt;

&lt;h5 id=&quot;21-新增内容&quot;&gt;2.1 新增内容&lt;/h5&gt;

&lt;p&gt;1996年5月，1.0的版本发布，内容做了大量的增加。&lt;/p&gt;

&lt;p&gt;1，任何格式的内容都可以发送。此时的互联网可以传送文字，图像，视频，二进制文件。&lt;/p&gt;

&lt;p&gt;2，除了&lt;code class=&quot;highlighter-rouge&quot;&gt;GET&lt;/code&gt;命令，还引入了&lt;code class=&quot;highlighter-rouge&quot;&gt;POST&lt;/code&gt;命令和&lt;code class=&quot;highlighter-rouge&quot;&gt;HEAD&lt;/code&gt;命令。&lt;/p&gt;

&lt;p&gt;3，HTTP请求和回应的格式也变了，除了数据部分，每次通信都必须包括头信息（HTTP header), 用来描述一些元数据。&lt;/p&gt;

&lt;p&gt;4，其他新增的功能还包括状态码（status code）、多字符集支持、多部分发送（multi-part type）、权限（authorization）、缓存（cache）、内容编码（content encoding）等。&lt;/p&gt;

&lt;h5 id=&quot;22-请求格式&quot;&gt;2.2 请求格式&lt;/h5&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;GET / HTTP/1.0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5)
Accept: */*

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;第一行是请求命令，必须在尾部添加协议版本&lt;code class=&quot;highlighter-rouge&quot;&gt;（HTTP/1.0）&lt;/code&gt;。后面就是多行头信息，描述客户端的情况。&lt;/p&gt;</content><author><name></name></author><summary type="html">HTTP 协议是互联网的基础协议，也是网页开发的必备知识，最新版本 HTTP/2 更是让它成为技术热点。</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://yuehuanjue.github.io//%7B%22feature%22=%3Enil,%20%22teaser%22=%3Enil,%20%22credit%22=%3E%22yuehuanjue%22%7D" /></entry><entry><title type="html">年轻司机介绍补预告片系统架构与cgi</title><link href="https://yuehuanjue.github.io//python/project-01-trailer-and-cgi/" rel="alternate" type="text/html" title="年轻司机介绍补预告片系统架构与cgi" /><published>2016-05-05T00:00:00+00:00</published><updated>2016-05-05T00:00:00+00:00</updated><id>https://yuehuanjue.github.io//python/project-01-trailer-and-cgi</id><content type="html" xml:base="https://yuehuanjue.github.io//python/project-01-trailer-and-cgi/">&lt;h2 id=&quot;概要&quot;&gt;概要&lt;/h2&gt;

&lt;p&gt;这篇文章简单的记录做的一个简单的系统, 并介绍一下cgi相关的知识点.&lt;/p&gt;

&lt;h2 id=&quot;项目背景&quot;&gt;项目背景&lt;/h2&gt;

&lt;p&gt;视频是一个用户粘性很低的行业.&lt;br /&gt;
而我们部门在这个行业刚起步, 很多视频内容都不完善, 即只有一个空专辑(比如XX电影的名字).&lt;br /&gt;
我曾问上级, 为什么不买往年的热门电影. &lt;br /&gt;
回答是: 对于往年的电影 如果现在去花钱买那些版权收益也不大, 对于以后的, 都是我们的, 都会买.&lt;br /&gt;
但是一个电影的预告片往往是不需要版权的, 所以这里就希望可以对往年的电影补充预告片, 这样用户进到这个专辑, 至少可以看几分钟相关的宣传内容.&lt;/p&gt;

&lt;h2 id=&quot;项目介绍&quot;&gt;项目介绍&lt;/h2&gt;

&lt;p&gt;这个项目的目的是给空专辑补充预告片.&lt;br /&gt;
由于是从无到有的过程, 需要快速支持, 不需要做一个完善的闭环系统.&lt;/p&gt;

&lt;p&gt;这里分两个子项目:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;已经有空专辑和预告片视频了. 我们需要通过某种方法识别他们(豆瓣ID), 然后关联起来.&lt;/li&gt;
  &lt;li&gt;只有一个空专辑, 没有预告片. 我们需要去互联网上抓取预告片, 然后关联起来.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;系统的架构图大概如下:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://tiankonguse.com/lab/cloudLink/baidupan.php?url=/1915453531/449928177.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;项目实现&quot;&gt;项目实现&lt;/h2&gt;

&lt;p&gt;项目实际上分为几个模块(实际项目中对步骤进行调整优化了).&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;把空专辑与单视频通过豆瓣ID关联起来.&lt;br /&gt;
大概步骤如下:&lt;br /&gt;
1). 从A表中找到符合指定条件的空专辑&lt;br /&gt;
2). 从B表中找到专辑对应的豆瓣ID, 做豆瓣ID到专辑的反向关系(存在多个时, 保留最后一个专辑) &lt;br /&gt;
3). 从C表找到符合条件的单视频&lt;br /&gt;
4). 从D表找到单视频对应的豆瓣ID, 做豆瓣ID到视频的反向关系(存在多个时, 全部保留)&lt;br /&gt;
5). 扫表专辑的豆瓣ID列表, 在视频列表中看是否存在, 存在则把关系保存起来&lt;br /&gt;
6). 把找到的专辑和视频通过接口关联起来.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;对于没关联的空专辑抓取视频&lt;br /&gt;
 这个步骤看上面的流程图就很清晰了.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;需要有个简单的系统供编辑处理&lt;br /&gt;
1). 添加任务 &lt;br /&gt;
2). 展示任务列表&lt;br /&gt;
3). 编辑处理任务(编辑是否补上预告片)&lt;br /&gt;
4). 每日邮件发送统计情况&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;做这个项目的时候, 第一步和第三步是一期做的.这样可以快速的补充大量的预告片, 且准确度很高.&lt;br /&gt;
第二部分是二期做的, 需要抓取团队开发对应的功能, 而且由于是使用标题等资料搜索抓取的, 准确度很低很低.&lt;/p&gt;

&lt;h3 id=&quot;优化&quot;&gt;优化&lt;/h3&gt;

&lt;p&gt;这个项目有个特点: 快速实现.&lt;br /&gt;
对于内部空专辑和单视频关联逻辑, 没什么优化的, 历史数据跑完了, 关联的数据就几乎为0了.&lt;br /&gt;
抓取视频的准确度较低, 倒是可以优化, 但是我们是以产品驱动的, 这个项目还没上线时就已经投入其他项目了(还好上线后编辑使用没出任何问题).&lt;br /&gt;
所以这个项目的总体架构就是上图介绍的.&lt;/p&gt;

&lt;h2 id=&quot;项目结语&quot;&gt;项目结语&lt;/h2&gt;

&lt;p&gt;做上面项目第一期的时候, 实际上工作分为两部分:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;一个简单的系统&lt;/li&gt;
  &lt;li&gt;编写关联程序, 调用系统接口添加任务.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;做系统的时候, 发现周围的人都使用C++当做WEB服务器的后台开发语言, 他们还称此为cgi.&lt;br /&gt;
于是有必要了解一下cgi.&lt;/p&gt;

&lt;h2 id=&quot;cgi介绍&quot;&gt;cgi介绍&lt;/h2&gt;

&lt;p&gt;CGI 的中文是&lt;a href=&quot;https://en.wikipedia.org/wiki/Common_Gateway_Interface&quot;&gt;通用网关接口&lt;/a&gt;.&lt;br /&gt;
阅读了WIKI, 发现把我们写的c++程序称为CGI是不恰当的.&lt;br /&gt;
因为CGI只是WEB服务器与动态处理程序之间的一个标准或协议, 我们平时写的php, python, perl和c++的二进制没有太大的区别, 都是接收请求, 处理, 返回结果.&lt;/p&gt;

&lt;p&gt;当认识到CGI只是一个协议时, 我们心里自然就会想问另一个问题: WEB服务器与动态处理程序之间是什么关系?&lt;/p&gt;

&lt;p&gt;答案也很快得到验证.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://tiankonguse.com/lab/cloudLink/baidupan.php?url=/1915453531/2724143093.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;接着想到的第二个问题是: WEB服务器怎么识别我们的动态处理程序的.&lt;/p&gt;

&lt;p&gt;这个问题其实是两个问题.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;WEB服务器怎么知道这个请求需要动态处理程序处理.&lt;/li&gt;
  &lt;li&gt;WEB服务器怎么知道怎么执行这个动态处理程序.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;对于第一个问题, WEB服务器往往是通过请求URL匹配的方式识别的.&lt;br /&gt;
对于第二个问题, 其实在linux上很容易回答: linux是怎么识别我们的脚本的?&lt;/p&gt;

&lt;p&gt;在apache上, 我们通常使用&lt;code class=&quot;highlighter-rouge&quot;&gt;ScriptAlias&lt;/code&gt;来指定程序的位置.&lt;br /&gt;
而在nginx上, 我们则需要一个额外的调度器来调用对应的程序.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://tiankonguse.com/lab/cloudLink/baidupan.php?url=/1915453531/173826083.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;可以看看下面的测试程序, 我们用bash脚本也可以做网站了.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[user_00@V_10_157_52_39 /data/release/vunion.oa.com/cgi-bin]$ cat ./test.sh
#!/bin/sh
echo -e &quot;Content-type:text/html\n\n&quot;
echo -e &quot;hello&amp;lt;br&amp;gt;&quot;
echo -e &quot;$0  $*&amp;lt;br&amp;gt;&quot;
echo -e &quot;${SCRIPT_NAME}&amp;lt;br&amp;gt;&quot;
echo -e &quot;${QUERY_STRING}&amp;lt;br&amp;gt;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;http://tiankonguse.com/lab/cloudLink/baidupan.php?url=/1915453531/757572620.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;最后, 我们的问题是: 怎么传输数据的?&lt;/p&gt;

&lt;p&gt;这个问题其实反而好回答了.&lt;br /&gt;
我们看看上面的bash脚本, 输入数据并没有使用参数传输.&lt;br /&gt;
对于bash, 传参有两种: 参数和环境变量.&lt;br /&gt;
CGI标准使用的是环境变量来传参的.&lt;/p&gt;

&lt;p&gt;而对于输出数据, 直接使用标准输出来传送数据了(不考虑 std_err).&lt;/p&gt;

&lt;p&gt;扩展问题: 我们使用什么框架来编写cgi&lt;/p&gt;

&lt;p&gt;开源, 开源, 开源!!!&lt;br /&gt;
我们cgi基础框架是开源软件: &lt;a href=&quot;http://www.clearsilver.net/&quot;&gt;clearsilver&lt;/a&gt;, 它封装了&lt;a href=&quot;http://www.clearsilver.net/docs/man_cgi.hdf&quot;&gt;cgi&lt;/a&gt;相关操作. &lt;br /&gt;
然后前辈在clearsilver的基础上, 封装了一个cgiframe框架. 开发只需要实现两个函数即可: init初始化函数, process 逻辑处理函数.&lt;br /&gt;
这里就不多说了, 封装的好处是大家开发快, 缺点是很多人都不知道底层的原理了.&lt;/p&gt;

&lt;h2 id=&quot;问题定位&quot;&gt;问题定位&lt;/h2&gt;

&lt;p&gt;这里把c++编写的程序称为WEB程序更合适点.&lt;br /&gt;
对于C++ 编译型WEB程序, 定位问题一般是固定的.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;万能定位法: 打日志&lt;/li&gt;
  &lt;li&gt;常驻进程定位法: gdb + strace.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;有人可能会说, cgi一般会先 fork 一个进程出来, 这个怎么定位呢?&lt;/p&gt;

&lt;p&gt;其实答案很简单, gdb进入fork对应的进程即可, strace 进入fork对应的进程即可.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gdb: set follow-fork-mode child
strace: man strace  # 我不会告诉你是-f参数
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;结语&quot;&gt;结语&lt;/h2&gt;

&lt;p&gt;年轻的司机语言表达能力不好, 结构的组织能力也不好.&lt;/p&gt;

&lt;p&gt;其实要表达的有两点:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;我们所在的部门起步较晚, 很多基础功能都没有, 但这不是理由, 我们会努力去做好一切的, 有问题可以随时反馈.&lt;/li&gt;
  &lt;li&gt;cgi是进来接触的第一个知识点, 了解了原理后, 会发现什么语言编写的其实都一样的.&lt;/li&gt;
&lt;/ol&gt;</content><author><name></name></author><summary type="html">概要</summary></entry><entry><title type="html">在本地运行jekyll网站</title><link href="https://yuehuanjue.github.io//jekyll/jekyll-run-in-local/" rel="alternate" type="text/html" title="在本地运行jekyll网站" /><published>2015-10-08T00:00:00+00:00</published><updated>2015-10-08T00:00:00+00:00</updated><id>https://yuehuanjue.github.io//jekyll/jekyll-run-in-local</id><content type="html" xml:base="https://yuehuanjue.github.io//jekyll/jekyll-run-in-local/">&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

&lt;p&gt;最近一年多, 一直使用&lt;a href=&quot;https://github.com/tiankonguse/tiankonguse.github.io&quot;&gt;github&lt;/a&gt; 来当作自己的 &lt;a href=&quot;http://github.tiankonguse.com/&quot;&gt;记录集&lt;/a&gt;,俗称博客.&lt;br /&gt;
使用github当作记录集有一个问题就是:网站有任何改动, 只有提交了才能看到效果.&lt;br /&gt;
如果发现有问题了, 需要修改后再次提交.&lt;br /&gt;
这也是为什么我的提交次数很高的缘故. &lt;a href=&quot;https://github.com/tiankonguse/tiankonguse.github.io/commits/master&quot;&gt;1&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;实际上刚开始搭建 自己的 &lt;a href=&quot;http://github.tiankonguse.com/&quot;&gt;记录集&lt;/a&gt;时, 我在我的电脑上搭建了一个本地的jekyll环境, 然是后来换电脑了, 就再也没搭建了.&lt;br /&gt;
现在重新搭建一下, 顺便记录一下.&lt;/p&gt;

&lt;h2 id=&quot;环境准备&quot;&gt;环境准备&lt;/h2&gt;

&lt;p&gt;由于我使用的是 ubuntu 系统, 所以安装很简单, 一条命令即可.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo apt-get install jekyll
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;当然, 你也可以去下载源码编译安装,这里就不做介绍了.&lt;/p&gt;

&lt;h2 id=&quot;网站准备&quot;&gt;网站准备&lt;/h2&gt;

&lt;p&gt;之前我有&lt;a href=&quot;https://www.google.com/search?&amp;amp;q=site%3Atiankonguse.com+jekyll&quot;&gt;三篇很水的记录&lt;/a&gt;, 大家可以简单的看一下.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://github.tiankonguse.com/blog/2014/09/26/jekyll-base-record/&quot;&gt;Jekyll 模板 简单语法 笔记&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://github.tiankonguse.com/blog/2014/07/10/make-github-website/&quot;&gt;在github上制作一个轻量级免费的静态网站&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://github.tiankonguse.com/blog/2014/11/10/jekyll-study/&quot;&gt;Jekyll 语法简单笔记&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;运行网站&quot;&gt;运行网站&lt;/h2&gt;

&lt;p&gt;网站搭建好了, 环境准备好了, 我们就需要生成页面了.&lt;/p&gt;

&lt;p&gt;关于执行命令时遇到的一些问题, 可以参考下面的常见问题.&lt;/p&gt;

&lt;p&gt;生成网页命令:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;tiankonguse:tiankonguse.github.io $ sudo jekyll serve --watch --config _config.yml,_config_dev.yml
Configuration file: _config.yml
Configuration file: _config_dev.yml
            Source: .
       Destination: ./_site
      Generating... 
                    done.
 Auto-regeneration: enabled for '.'
Configuration file: _config.yml
Configuration file: _config_dev.yml
    Server address: http://127.0.0.1:80//
  Server running... press ctrl-c to stop.
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;常见问题&quot;&gt;常见问题&lt;/h2&gt;

&lt;h3 id=&quot;本地与正式环境的配置不一样&quot;&gt;本地与正式环境的配置不一样&lt;/h3&gt;

&lt;p&gt;比如我的配置中, 有个 url 字段, 在正式环境中配置的是 &lt;a href=&quot;http://github.tiankonguse.com/&quot;&gt;http://github.tiankonguse.com/&lt;/a&gt; , 但是在本地, 应该配置成 &lt;a href=&quot;http://github.tiankonguse.com/&quot;&gt;http://127.0.0.1&lt;/a&gt; .&lt;br /&gt;
有时候本地测试完了, 就直接提交github了, 结果正式环境悲剧了, 很多文件404了.&lt;/p&gt;

&lt;p&gt;现在找到一个解决方案: 测试版本的配置文件&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo jekyll serve --watch --config _config.yml,_config_dev.yml
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;文档如下&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;tiankonguse:tiankonguse.github.io $ jekyll serve --help
jekyll serve -- Serve your site locally

Usage:

  jekyll serve [options]

Options:
            --config CONFIG_FILE[,CONFIG_FILE2,...]  Custom configuration file
            --future       Publishes posts with a future date
            --limit_posts MAX_POSTS  Limits the number of posts to parse and publish
        -w, --[no-]watch   Watch for changes and rebuild
            --force_polling  Force watch to use polling
            --lsi          Use LSI for improved related posts
        -D, --drafts       Render posts in the _drafts folder
            --unpublished  Render posts that were marked as unpublished
        -q, --quiet        Silence output.
        -V, --verbose      Print verbose output.
        -B, --detach       Run the server in the background (detach)
        -P, --port [PORT]  Port to listen on
        -H, --host [HOST]  Host to bind to
        -b, --baseurl [URL]  Base URL
            --skip-initial-build  Skips the initial site build which occurs before the server is started.
        -h, --help         Show this message
        -v, --version      Print the name and version
        -t, --trace        Show the full backtrace when an error occurs
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;测试版本的配置文件放在后面, 后面的值覆盖前面的值.&lt;/p&gt;

&lt;h3 id=&quot;layout-nil-不存在&quot;&gt;Layout nil 不存在&lt;/h3&gt;

&lt;p&gt;有时会遇到这样一个警告:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Build Warning: Layout 'nil' requested in atom.xml does not exist.
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;原因是我们使用旧版的语法, 新版本使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;null&lt;/code&gt; 代替了 &lt;a href=&quot;https://github.com/jekyll/jekyll/issues/2712&quot;&gt;2&lt;/a&gt;.&lt;br /&gt;
所以我们把 &lt;code class=&quot;highlighter-rouge&quot;&gt;nil&lt;/code&gt; 换成 &lt;code class=&quot;highlighter-rouge&quot;&gt;null&lt;/code&gt; 即可.&lt;/p&gt;

&lt;h3 id=&quot;auto-regeneration-不再支持&quot;&gt;Auto-regeneration 不再支持&lt;/h3&gt;

&lt;p&gt;这个也是新版本不支持的缘故.注释&lt;code class=&quot;highlighter-rouge&quot;&gt;auto&lt;/code&gt;配置即可&lt;code class=&quot;highlighter-rouge&quot;&gt;#auto: true&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Deprecation: Auto-regeneration can no longer be set from your configuration file(s). Use the --[no-]watch/-w command-line option instead.

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;pygments-被命名为-highlighter&quot;&gt;pygments 被命名为 highlighter&lt;/h3&gt;

&lt;p&gt;这个还是新版本的缘故.&lt;/p&gt;

&lt;p&gt;注释掉 &lt;code class=&quot;highlighter-rouge&quot;&gt;pygments: true&lt;/code&gt;, 增加 &lt;code class=&quot;highlighter-rouge&quot;&gt;highlighter: pygments&lt;/code&gt;即可.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Deprecation: The 'pygments' configuration option has been renamed to 'highlighter'. Please update your config file accordingly. The allowed values are 'rouge', 'pygments' or null.
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;name-or-service-not-known&quot;&gt;Name or service not known&lt;/h3&gt;

&lt;p&gt;这个是由于我们配置的域名或者host错了,改成&lt;code class=&quot;highlighter-rouge&quot;&gt;host: 127.0.0.1&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;jekyll 2.5.3 | Error:  getaddrinfo: Name or service not known
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;permission-denied&quot;&gt;Permission denied&lt;/h3&gt;

&lt;p&gt;这是由于我们缺少权限, 使用&lt;code class=&quot;highlighter-rouge&quot;&gt;sudo&lt;/code&gt;即可.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;jekyll 2.5.3 | Error:  Permission denied - bind(2)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;</content><author><name></name></author><category term="[&quot;jekyll&quot;]" /><category term="github" /><category term="jekyll" /><summary type="html">前言</summary></entry><entry><title type="html">初步学习 ruby</title><link href="https://yuehuanjue.github.io//jekyll/ruby-study/" rel="alternate" type="text/html" title="初步学习 ruby" /><published>2014-12-01T00:00:00+00:00</published><updated>2014-12-01T00:00:00+00:00</updated><id>https://yuehuanjue.github.io//jekyll/ruby-study</id><content type="html" xml:base="https://yuehuanjue.github.io//jekyll/ruby-study/">&lt;p&gt;&lt;img src=&quot;http://tiankonguse.com/lab/cloudLink/baidupan.php?url=/1915453531/1777921927.jpg&quot; alt=&quot;ruby1&quot; /&gt;
&lt;img src=&quot;http://tiankonguse.com/lab/cloudLink/baidupan.php?url=/1915453531/1780973295.jpg&quot; alt=&quot;ruby2&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

&lt;p&gt;我这里的记录主要面向至少已经掌握一两种编程语言的同学。&lt;br /&gt;
记录的比较简单，而且会随着以后 ruby 的使用而更新这篇文章。&lt;/p&gt;

&lt;h2 id=&quot;环境&quot;&gt;环境&lt;/h2&gt;

&lt;p&gt;ruby 的安装这里就不讲了。&lt;br /&gt;
一般执行 ruby 有两种方法： 命令行方式和脚本方式。&lt;br /&gt;
命令行方式和其他脚本语言不同，不是输入 ruby, 而是输入 irb 命令。&lt;/p&gt;

&lt;h3 id=&quot;命令行方式&quot;&gt;命令行方式&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;skyyuan@skyyuan-PC3:~ $ irb
irb(main):001:0&amp;gt; &quot;hello word&quot;
=&amp;gt; &quot;hello word&quot;
irb(main):002:0&amp;gt; puts(&quot;hello word&quot;)
hello word
=&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;其中 &lt;code class=&quot;highlighter-rouge&quot;&gt;=&amp;gt;&lt;/code&gt; 后面代表上一个命令的返回值。&lt;br /&gt;
所以第一个命令只有一个返回值，第二个命令有先输出一句话，然后输出返回值。&lt;/p&gt;

&lt;h3 id=&quot;脚本文件方式&quot;&gt;脚本文件方式&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;skyyuan@skyyuan-PC3:~ $ cat test.rb
puts(&quot;hello word&quot;);

skyyuan@skyyuan-PC3:~ $ ruby ./test.rb
hello word
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;当然，如果在文件的第一行指定 ruby 的位置， 则可以直接执行。&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;skyyuan@skyyuan-PC3:~ $ cat test.rb
#! /bin/ruby
puts(&quot;hello word&quot;);

skyyuan@skyyuan-PC3:~ $ ./test.rb
hello word
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;基本语法&quot;&gt;基本语法&lt;/h2&gt;

&lt;h3 id=&quot;输出&quot;&gt;输出&lt;/h3&gt;

&lt;p&gt;puts 可以输出一个字符串&lt;/p&gt;

&lt;h3 id=&quot;变量-与-赋值&quot;&gt;变量 与 赋值&lt;/h3&gt;

&lt;p&gt;和其他语言一样，直接赋值即可。&lt;br /&gt;
一般情况下脚本语言的类型都是弱类型。&lt;br /&gt;
变量不需要声明即可使用。&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ans = &quot;hello word&quot;;
ans = 3 * 6;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;计算&quot;&gt;计算&lt;/h3&gt;

&lt;p&gt;加法&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ans = a + b
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;乘法&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ans = a * b
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;幂乘&lt;/p&gt;

&lt;p&gt;等价于c语言的 &lt;code class=&quot;highlighter-rouge&quot;&gt;pow(a, b)&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a**b
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;开方&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Math.sqrt(a) 
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;函数&quot;&gt;函数&lt;/h2&gt;

&lt;p&gt;函数使用 def 定义， 所有的块都使用 end 结束。&lt;br /&gt;
函数参数可以指定默认值。&lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;#{}&lt;/code&gt; 的作用时格式化字符串时，一个占位符，里面的名字是一个变量。&lt;br /&gt;
调用函数没有参数时，直接一个名字即可。&lt;br /&gt;
有参数时，可以加上括号， 也可以直接列在函数名字后面。&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def fun(name = &quot;World&quot;)
    puts &quot;Hello #{name}!&quot;
end

fun; 
=&amp;gt; Hello World!

fun(&quot;tiankonguse&quot;);
=&amp;gt;Hello tiankonguse!

fun &quot;tiankonguse&quot;
=&amp;gt;Hello tiankonguse!
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;类&quot;&gt;类&lt;/h2&gt;

&lt;p&gt;initialize 是类的构造函数。&lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;@ name&lt;/code&gt; 是类的成员变量，默认是私有的。&lt;br /&gt;
还有，我们访问类的成员需要加个 &lt;code class=&quot;highlighter-rouge&quot;&gt;@&lt;/code&gt; 符号。&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Greeter
    def initialize(name = &quot;World&quot;)
        @ name = name
    end
    def say
        puts &quot;hello #{@ name}!&quot;
    end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;判断是不是公有成员&quot;&gt;判断是不是公有成员&lt;/h3&gt;

&lt;p&gt;想要判断一个成员是不是共有的可以使用下面的方法&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;g = Greeter.new()

g.respond_to?(&quot;say&quot;)
=&amp;gt; true

g.respond_to?(&quot;name&quot;)
=&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;公有成员&quot;&gt;公有成员&lt;/h3&gt;

&lt;p&gt;想要是一个成员变量为共有的，只需要用下面的方法即可。&lt;br /&gt;
另外 ruby 的class 可以分开写，会自动合并的。&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class Greeter
    attr_accessor :name
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;赋值与取值&quot;&gt;赋值与取值&lt;/h3&gt;

&lt;p&gt;对于一个变量 name，实际上我们有两个操作：赋值、取值。&lt;br /&gt;
赋值是 &lt;code class=&quot;highlighter-rouge&quot;&gt;name=&lt;/code&gt;, 取值是 &lt;code class=&quot;highlighter-rouge&quot;&gt;name&lt;/code&gt; 。&lt;/p&gt;

&lt;p&gt;比如可以测是否是公有的。&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;g.respond_to?(&quot;name&quot;)
=&amp;gt; true

g.respond_to?(&quot;name=&quot;)
=&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;数组&quot;&gt;数组&lt;/h2&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;names = [&quot;Albert&quot;, &quot;Brenda&quot;, &quot;Charles&quot;, &quot;Dave&quot;, &quot;Engelbert&quot;]

puts(names.join(&quot;, &quot;));
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;循环&quot;&gt;循环&lt;/h2&gt;

&lt;p&gt;ruby 的循环可以理解为 js 的回调函数，参数是数组的值。&lt;br /&gt;
而且 ruby 没有 for 和 while 循环。&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;names.each do |name|
    puts &quot;Hello #{name}!&quot;
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;等价于&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;names.each(function(name){
    console.log(&quot;Hello &quot; + name);
});
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;安全开关&quot;&gt;安全开关&lt;/h2&gt;

&lt;p&gt;对于脚本，一般会有一个命令。&lt;/p&gt;

&lt;p&gt;比如 python 有下面的命令&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if __name__ == &quot;__main__&quot;: 
    main()
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;而 ruby 是&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if __FILE__ == $0
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;参考资料&quot;&gt;参考资料&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.ruby-lang.org/en/documentation/&quot;&gt;ruby 官网文档&lt;/a&gt;&lt;/p&gt;</content><author><name></name></author><category term="ruby" /><category term="教程" /><category term="学习笔记" /><category term="ruby" /><category term="Jekyll" /><summary type="html"></summary></entry><entry><title type="html">Syntax Highlighting Test</title><link href="https://yuehuanjue.github.io//articles/syntax-highlighting-test/" rel="alternate" type="text/html" title="Syntax Highlighting Test" /><published>2014-11-30T03:08:14+00:00</published><updated>2014-11-30T03:08:14+00:00</updated><id>https://yuehuanjue.github.io//articles/syntax-highlighting-test</id><content type="html" xml:base="https://yuehuanjue.github.io//articles/syntax-highlighting-test/">&lt;p&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Syntax_highlighting&quot;&gt;Syntax highlighting&lt;/a&gt; is a feature that displays source code, in different colors and fonts according to the category of terms. This feature facilitates writing in a structured language such as a programming language or a markup language as both structures and syntax errors are visually distinct. Highlighting does not affect the meaning of the text itself; it is intended only for human readers.&lt;/p&gt;

&lt;nav class=&quot;toc&quot;&gt;

&lt;/nav&gt;

&lt;h3 id=&quot;code-blocks&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;code&amp;gt;&lt;/code&gt; Blocks&lt;/h3&gt;

&lt;pre&gt;
	&lt;code&gt;
.site-title {
	@include row;
	@include media($large) {
		@include span-columns(4);
	}
	padding: (.25 * $masthead-height) 0; // fourth the height to center vertically
	height: $masthead-height;
	text-decoration: none;
	color: $black;
	font-family: $alt-font;
	font-weight: 700;
	@include font-size(20,no);
	line-height: .5 * $masthead-height; // half the height to center vertically
	text-transform: uppercase;
}
	&lt;/code&gt;
&lt;/pre&gt;

&lt;h3 id=&quot;highlighted-code-blocks&quot;&gt;Highlighted Code Blocks&lt;/h3&gt;

&lt;p&gt;To modify styling and highlight colors edit &lt;code class=&quot;highlighter-rouge&quot;&gt;/_sass/_syntax.scss&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;#container&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;-240px&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;100%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Jekyll&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TagIndex&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Page&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;site&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@site&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;site&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dir&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'index.html'&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read_yaml&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'_layouts'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'tag_index.html'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'tag'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tag_title_prefix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;site&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'tag_title_prefix'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'Tagged: '&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tag_title_suffix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;site&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'tag_title_suffix'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&amp;amp;#8211;'&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'title'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag_title_prefix&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'description'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;An archive of posts tagged &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;.&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;standard-code-block&quot;&gt;Standard Code Block&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;nav class=&quot;pagination&quot; role=&quot;navigation&quot;&amp;gt;
    {% if page.previous %}
        &amp;lt;a href=&quot;{{ site.url }}{{ page.previous.url }}&quot; class=&quot;btn&quot; title=&quot;{{ page.previous.title }}&quot;&amp;gt;Previous article&amp;lt;/a&amp;gt;
    {% endif %}
    {% if page.next %}
        &amp;lt;a href=&quot;{{ site.url }}{{ page.next.url }}&quot; class=&quot;btn&quot; title=&quot;{{ page.next.title }}&quot;&amp;gt;Next article&amp;lt;/a&amp;gt;
    {% endif %}
&amp;lt;/nav&amp;gt;&amp;lt;!-- /.pagination --&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;fenced-code-blocks&quot;&gt;Fenced Code Blocks&lt;/h3&gt;

&lt;div class=&quot;language-css highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;#container&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;-240px&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nl&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;100%&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;nav&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;pagination&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;role=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;navigation&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
    {% if page.previous %}
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;{{ site.url }}{{ page.previous.url }}&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;btn&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;title=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;{{ page.previous.title }}&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Previous article&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    {% endif %}
    {% if page.next %}
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;{{ site.url }}{{ page.next.url }}&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;btn&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;title=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;{{ page.next.title }}&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Next article&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    {% endif %}
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/nav&amp;gt;&lt;/span&gt;&lt;span class=&quot;c&quot;&gt;&amp;lt;!-- /.pagination --&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Jekyll&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TagIndex&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Page&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;site&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@site&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;site&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dir&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'index.html'&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read_yaml&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;File&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'_layouts'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'tag_index.html'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'tag'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tag_title_prefix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;site&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'tag_title_prefix'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'Tagged: '&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;tag_title_suffix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;site&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'tag_title_suffix'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&amp;amp;#8211;'&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'title'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag_title_prefix&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'description'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;An archive of posts tagged &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;.&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;github-gist-embed&quot;&gt;GitHub Gist Embed&lt;/h3&gt;

&lt;p&gt;An example of a Gist embed below.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/mmistakes/6589546.js&quot;&gt; &lt;/script&gt;

&lt;script src=&quot;https://gist.github.com/8531391.js&quot;&gt; &lt;/script&gt;</content><author><name></name></author><summary type="html">Demo post to test the various ways of using syntax highlighting.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://yuehuanjue.github.io//%7B%22feature%22=%3Enil,%20%22teaser%22=%3Enil,%20%22thumb%22=%3Enil%7D" /></entry><entry><title type="html">Jekyll 语法简单笔记</title><link href="https://yuehuanjue.github.io//jekyll/jekyll-study/" rel="alternate" type="text/html" title="Jekyll 语法简单笔记" /><published>2014-11-10T00:00:00+00:00</published><updated>2014-11-10T00:00:00+00:00</updated><id>https://yuehuanjue.github.io//jekyll/jekyll-study</id><content type="html" xml:base="https://yuehuanjue.github.io//jekyll/jekyll-study/">&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

&lt;p&gt;如果你只想快速搭建一个 github 的静态网站, 而暂时没有时间来研究 jekyll 语法的话,建议直接 fork 我的&lt;a href=&quot;https://github.com/tiankonguse/tiankonguse.github.io&quot;&gt;这个&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;当然,阅读一下之前我记录的一些笔记也可以增长一些知识.  注意,必须在子节点的顶部&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://github.tiankonguse.com/blog/2014/09/26/jekyll-base-record/&quot;&gt;Jekyll 模板 简单语法 笔记&lt;/a&gt; 只介绍了最最基本的语法&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://github.tiankonguse.com/blog/2014/07/10/make-github-website/&quot;&gt;在github上制作一个静态网站&lt;/a&gt; 只介绍了最简单的 github 操作.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;不过,现在我要记录一个比较完整的语法来建立一个功能比较健全记录型博客.&lt;/p&gt;

&lt;p&gt;具体官方文档地址请参考 &lt;a href=&quot;http://jekyllrb.com/docs/home/&quot;&gt;官方文档&lt;/a&gt;.&lt;br /&gt;
这里只介绍关于 jekyll 的语法,不介绍其他内容.&lt;/p&gt;

&lt;h2 id=&quot;开始&quot;&gt;开始&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Jekyll 是什么?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;jekyll 是一个静态网站生成器.&lt;br /&gt;
jekyll 通过 标记语言 &lt;a href=&quot;http://daringfireball.net/projects/markdown/&quot;&gt;markdown&lt;/a&gt; 或 &lt;a href=&quot;http://redcloth.org/textile&quot;&gt;textile&lt;/a&gt; 和 模板引擎 &lt;a href=&quot;https://github.com/Shopify/liquid/wiki&quot;&gt;liquid&lt;/a&gt; 转换生成网页.&lt;br /&gt;
&lt;a href=&quot;http://pages.github.com/&quot;&gt;github&lt;/a&gt; 为我们提供了这个一个地方, 可以使用 jekyll 做一个我们自己的网站.&lt;/p&gt;

&lt;p&gt;这里不介绍怎么在本地安装使用 jekyll, 如果你想在本地使用,请参考官方文档的 &lt;a href=&quot;http://jekyllrb.com/docs/installation/&quot;&gt;安装教程&lt;/a&gt; 和 &lt;a href=&quot;http://jekyllrb.com/docs/usage/&quot;&gt;使用教程&lt;/a&gt;.&lt;br /&gt;
不过这里可以透漏一下, jekyll 依赖于 &lt;a href=&quot;http://www.ruby-lang.org/en/downloads/&quot;&gt;ruby&lt;/a&gt; .&lt;/p&gt;

&lt;h2 id=&quot;配置&quot;&gt;配置&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;注意,配置不用使用 tab . 否则可能会忽略那条命令.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;文件介绍&quot;&gt;文件介绍&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;_config.yml&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;jekyll 的全局配置在 _config.yml 文件中配置.&lt;br /&gt;
比如网站的名字, 网站的域名, 网站的链接格式等等.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;_includes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;对于网站的头部, 底部, 侧栏等公共部分, 为了维护方便, 我们可能想提取出来单独编写, 然后使用的时候包含进去即可.&lt;br /&gt;
这时我们可以把那些公共部分放在这个目录下.&lt;br /&gt;
使用时只需要引入即可.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;_layouts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;对于网站的布局,我们一般会写成模板的形式,这样对于写实质性的内容时,比如文章,只需要专心写文章的内容, 然后加个标签指定用哪个模板即可.&lt;br /&gt;
对于内容,指定模板了模板后,我们可以称内容是模板的儿子.&lt;br /&gt;
为什么这样说呢?  因为这个模板时可以多层嵌套的, 内容实际上模板,只不过是叶子节点而已.&lt;/p&gt;

&lt;p&gt;在模板中, 引入儿子的内容.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;在儿子中,指定父节点模板&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;注意,必须在子节点的顶部.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---
layout: post
---
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;_posts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;写的内容,比如博客,常放在这里面, 而且一般作为叶子节点.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;_data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;也用于配置一些全局变量,不过数据比较多,所以放在这里。&lt;/p&gt;

&lt;p&gt;比如这个网站如果是多人开发, 我们通常会在这里面定义一个 members.yml 文件.&lt;/p&gt;

&lt;p&gt;文件内容为&lt;/p&gt;

&lt;div class=&quot;language-yml highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;袁小康&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;github&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;tiankonguse&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;oldnick&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;shen1000&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;nick&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;skyyuan&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;然后在模板中我们就可以通过下面语法使用这些数据了.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;site.data.members
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;_site&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;jekyll 生成网站输出的地方, 一般需要在 .gitignore  中屏蔽掉这个目录.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;index.html&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;主页文件, 后缀有时也用 index.md 等.&lt;br /&gt;
这个需要根据自己的需要来写, 因为不同的格式之间在某些情况下还是有一些细微的差别的.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;静态资源&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;对于其他静态资源, 可以直接放在根目录或任何其他目录, 然后路径和平常的网站一样, 按路径来找链接中的文件.&lt;/p&gt;

&lt;h2 id=&quot;配置全局变量&quot;&gt;配置全局变量&lt;/h2&gt;

&lt;p&gt;虽然全局变量都有自己的默认配置, 但是我们往往会手动配置为自己心中最好的效果.&lt;/p&gt;

&lt;h3 id=&quot;源代码的位置&quot;&gt;源代码的位置&lt;/h3&gt;

&lt;p&gt;这个一般不配置, 默认即可.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;source: DIR
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;当然编译的时候也可以指定,但是使用 github 我们是不能指定参数的.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-s, --source DIR
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;输出网站位置&quot;&gt;输出网站位置&lt;/h3&gt;

&lt;p&gt;这个一般也默认.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# 编译参数 -d, --destination DIR
destination: DIR #配置语法
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;safe开关&quot;&gt;Safe开关&lt;/h3&gt;

&lt;p&gt;官方文档上就一句话.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Disable custom plugins, and ignore symbolic links.
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;大概意思是禁用常用的插件,忽略符号链接.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# 编译参数  --safe
safe: BOOL
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;忽略文件&quot;&gt;忽略文件&lt;/h3&gt;

&lt;p&gt;这个很有用, 有时候你写了一个文件, 里面的一个东西可能会被 jekyll 处理, 但是你不想让 jekyll 处理的话, 就使用这个语法忽略那些文件吧.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;exclude: [DIR, FILE, ...]
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;强制处理文件&quot;&gt;强制处理文件&lt;/h3&gt;

&lt;p&gt;有时候我们的一些文件的名字由于不在 jekyll 处理的文件名字范围内,这时候就需要强制处理这些文件了.&lt;br /&gt;
比如 .htaccess 文件.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;include: [DIR, FILE, ...]
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;时区&quot;&gt;时区&lt;/h3&gt;

&lt;p&gt;我们模板中经常会对时间进行转换,这个时候如果至指定时区的话,可能得到的时间会和我们想要的时间错几个小时.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# timezone: Asia/Shanghai
timezone: TIMEZONE
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;编码&quot;&gt;编码&lt;/h3&gt;

&lt;p&gt;大家都是程序员,就不用多说了.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# encoding : utf-8
encoding: ENCODING
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;模板语法&quot;&gt;模板语法&lt;/h2&gt;

&lt;p&gt;模板语法实际上分两部分, 一部分是头部定义,另一部分是语法.&lt;/p&gt;

&lt;h3 id=&quot;头部定义&quot;&gt;头部定义&lt;/h3&gt;

&lt;p&gt;头部定义主要用于指定模板(layout)和定义一些变量, 比如 标题(title), 描述(description), 分类(category/categories), tags, 是否发布(published), 自定义变量.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---
layout:     post
title:      title
category: blog
description: description
published: true # default true
---
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;模板语法-1&quot;&gt;模板语法&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;使用变量&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;所有的变量是都一个树节点, 比如模板中定义的头部变量,需要使用下面的语法获得&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;page.title
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;page 是当前页面的根节点.&lt;/p&gt;

&lt;p&gt;其中全局根结点有&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;site _config.yml 中配置的信息&lt;/li&gt;
  &lt;li&gt;page 页面的配置信息&lt;/li&gt;
  &lt;li&gt;content 模板中,用于引入子节点的内容&lt;/li&gt;
  &lt;li&gt;paginator 分页信息&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;site-下的变量&quot;&gt;site 下的变量&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;site.time 运行 jekyll 的时间&lt;/li&gt;
  &lt;li&gt;site.pages 所有页面&lt;/li&gt;
  &lt;li&gt;site.posts 所有文章&lt;/li&gt;
  &lt;li&gt;site.related_posts 类似的10篇文章,默认最新的10篇文章,指定lsi为相似的文章&lt;/li&gt;
  &lt;li&gt;site.static_files 没有被 jekyll 处理的文章,有属性 path, modified_time 和 extname.&lt;/li&gt;
  &lt;li&gt;site.html_pages 所有的 html 页面&lt;/li&gt;
  &lt;li&gt;site.collections 新功能,没使用过&lt;/li&gt;
  &lt;li&gt;site.data _data 目录下的数据&lt;/li&gt;
  &lt;li&gt;site.documents 所有 collections 里面的文档&lt;/li&gt;
  &lt;li&gt;site.categories 所有的 categorie&lt;/li&gt;
  &lt;li&gt;site.tags 所有的 tag&lt;/li&gt;
  &lt;li&gt;site.[CONFIGURATION_DATA] 自定义变量&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;page-下的变量&quot;&gt;page 下的变量&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;page.content 页面的内容&lt;/li&gt;
  &lt;li&gt;page.title 标题&lt;/li&gt;
  &lt;li&gt;page.excerpt 摘要&lt;/li&gt;
  &lt;li&gt;page.url 链接&lt;/li&gt;
  &lt;li&gt;page.date 时间&lt;/li&gt;
  &lt;li&gt;page.id 唯一标示&lt;/li&gt;
  &lt;li&gt;page.categories 分类&lt;/li&gt;
  &lt;li&gt;page.tags 标签&lt;/li&gt;
  &lt;li&gt;page.path 源代码位置&lt;/li&gt;
  &lt;li&gt;page.next 下一篇文章&lt;/li&gt;
  &lt;li&gt;page.previous 上一篇文章&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;paginator-下的变量&quot;&gt;paginator 下的变量&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;paginator.per_page 每一页的数量&lt;/li&gt;
  &lt;li&gt;paginator.posts 这一页的数量&lt;/li&gt;
  &lt;li&gt;paginator.total_posts 所有文章的数量&lt;/li&gt;
  &lt;li&gt;paginator.total_pages 总的页数&lt;/li&gt;
  &lt;li&gt;paginator.page 当前页数&lt;/li&gt;
  &lt;li&gt;paginator.previous_page 上一页的页数&lt;/li&gt;
  &lt;li&gt;paginator.previous_page_path 上一页的路径&lt;/li&gt;
  &lt;li&gt;paginator.next_page 下一页的页数&lt;/li&gt;
  &lt;li&gt;paginator.next_page_path 下一页的路径&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;字符转义&quot;&gt;字符转义&lt;/h3&gt;

&lt;p&gt;有时候想输出 { 了,怎么办,使用 \ 转义即可.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;\{ =&amp;gt; {
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;输出变量&quot;&gt;输出变量&lt;/h3&gt;

&lt;p&gt;输出变量直接使用两个大括号括起来即可.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;page.title&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;循环&quot;&gt;循环&lt;/h3&gt;

&lt;p&gt;和平常的解释性语言很想.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;site.posts&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;lt;a&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;{ { post.url } }&quot;&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;post.title&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&amp;lt;/a&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;endfor&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;自动生成摘要&quot;&gt;自动生成摘要&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;site.posts&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
     &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;post.url&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;post.title&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;post.excerpt&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;remove:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'test'&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;endfor&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;删除指定文本&quot;&gt;删除指定文本&lt;/h3&gt;

&lt;p&gt;remove 可以删除变量中的指定内容&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;post.url&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;remove:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'http'&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;删除-html-标签&quot;&gt;删除 html 标签&lt;/h3&gt;

&lt;p&gt;这个在摘要中很有用.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;post.excerpt&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;strip_html&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;代码高亮&quot;&gt;代码高亮&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;highlight&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;ruby&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;linenos&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;\#&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;some&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;ruby&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;endhighlight&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;数组的大小&quot;&gt;数组的大小&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;赋值&quot;&gt;赋值&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;assign&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;格式化时间&quot;&gt;格式化时间&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;site.time&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;date_to_xmlschema&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2008-11-07&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;07&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;54-08&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;00&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;site.time&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;date_to_rfc822&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;Mon,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;07&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;Nov&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2008&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;07&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;54&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;-0800&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;site.time&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;date_to_string&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;07&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;Nov&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2008&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;site.time&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;date_to_long_string&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;07&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;November&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2008&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;搜索指定key&quot;&gt;搜索指定key&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Select all the objects in an array where the key has the given value.
{ { site.members | where:&quot;graduation_year&quot;,&quot;2014&quot; } } 
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;排序&quot;&gt;排序&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;site.pages&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;sort:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'title',&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'last'&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;to-json&quot;&gt;to json&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;site.data.projects&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;jsonify&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;序列化&quot;&gt;序列化&lt;/h3&gt;

&lt;p&gt;把一个对象变成一个字符串&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;page.tags&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;array_to_sentence_string&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;单词的个数&quot;&gt;单词的个数&lt;/h3&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;page.content&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;number_of_words&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;指定个数&quot;&gt;指定个数&lt;/h3&gt;

&lt;p&gt;得到数组指定范围的结果集&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;site.posts&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;limit:20&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;内容名字规范&quot;&gt;内容名字规范&lt;/h2&gt;

&lt;p&gt;对于博客,名字必须是 YEAR-MONTH-DAY-title.MARKUP 的格式.&lt;/p&gt;

&lt;p&gt;比如&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;2014-11-06-memcached-code.md
2014-11-06-memcached-lib.md
2014-11-06-sphinx-config-and-use.md
2014-11-07-memcached-hash-table.md
2014-11-07-memcached-string-hash.md
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;</content><author><name></name></author><category term="jekyll" /><category term="github" /><category term="语法" /><summary type="html">前言</summary></entry></feed>