Octpress自定义配置和主题安装


layout: post
title: "Octpress自定义配置和主题安装"
date: 2016-04-30 20:57:43 +0800
comments: true
categories:


Octpress 基本配置

在_config.yml文件中。可以直接修改title,author等基本信息。

<pre class="prettyprint linenums">

-----------------------

Main Configs

-----------------------

url: http://ewanreton.github.io
title: Ewan Reton
subtitle: Never be touched by yourself
author: Ewan Reton
simple_search: https://www.baidu.com
description:

Default date format is "ordinal" (resulting in "July 22nd 2007")

You can customize the format as defined in

http://www.ruby-doc.org/core-1.9.2/Time.html#method-i-strftime

Additionally, %o will give you the ordinal representation of the day

date_format: "ordinal"

RSS / Email (optional) subscription links (change if using something like Feedburner)

subscribe_rss: /atom.xml
subscribe_email:

RSS feeds can list your email address if you like

email: liukedi001@gmail.com
</pre>
下一部分的Jekyll & Plugins 在没有对Octpress进行深入了解时不介意修改。
在下一部分的3rd Party Settings 就是一些部件的配置,我们今天主要的内容就是涉及这一块的配置。

使用第三方插件

  • 增加评论功能
  • 增加社会化分享
  • 自定义404页面
  • 自定义导航
  • 安装使用主题
  • 设置链接在新窗口打开
  • 首页文章以摘要形式展示
  • 添加侧边栏文章分类(category)
  • 自动为图片添加url前缀
  • 添加访客统计

增加评论

默认使用discuss,但是由于discuss在国内使用效果不好所以用国内多说来替代
  1. 在多说网站注册过后,在工具->获取代码 将这块代码复制。
  2. 在配置文件中添加
    <pre class="prettyprint linenums">

    duoshuo

    duoshuo_comment = true //开启多说
    在sourse 中 找到_layout post.html
    </pre>
    {% if site.disqus_short_name and page.comments == true %}
    <section id="comment">
    <h1 class="title">Comments</h1>
    <div id="disqus_thread" aria-live="polite">{% include post/disqus_thread.html %}</div>
    </section>
    {% endif %}

<pre class="prettyprint text">
将其中的site.disqus_short_name and page.comments改为我们刚刚配置的duoshuo_comment
并将其中引用的文件disqus_thread.html改为duoshuo_comment.html
如下:
</pre>

    {% if duoshuo_comment == true %}
    <section id="comment">
    <h1 class="title">Comments</h1>
    <div id="disqus_thread" aria-live="polite">{% include post/duoshuo_comment.html %}</div>
    </section>
    {% endif %}
    

并修改duoshuo_comment.html的内容

<!-- 多说评论框 start -->
    <div class="ds-thread" data-thread-key="{{page.id}}" data-title="{{page.title}}" data-url="{{site.url}}{{page.url}}"></div>
<!-- 多说评论框 end -->
<!-- 多说公共JS代码 start (一个网页只需插入一次) -->
<script type="text/javascript">
var duoshuoQuery = {short_name:"ewanreton"};
    (function() {
        var ds = document.createElement('script');
        ds.type = 'text/javascript';ds.async = true;
        ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
        ds.charset = 'UTF-8';
        (document.getElementsByTagName('head')[0] 
         || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
    </script>
<!-- 多说公共JS代码 end -->

社会化分享

同上一样,先在_config.yml中添加

#jiashare
jia_share = true;

打开source->_include->post->sharing.html

<div class="sharing">

    {% if site.facebook_like %}
    <a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
    {% endif %}
    {% if site.twitter_tweet_button %}
    <a class="addthis_button_tweet"></a>
    {% endif %}
    {% if site.google_plus_one %}
    <a class="addthis_button_google_plusone" g:plusone:size="{{ site.google_plus_one_size }}"></a>
    {% endif %}
    
</div>

添加代码。如下:

<div class="sharing">

    {% if site.facebook_like %}
    <a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
    {% endif %}
    {% if site.twitter_tweet_button %}
    <a class="addthis_button_tweet"></a>
    {% endif %}
    {% if site.google_plus_one %}
    <a class="addthis_button_google_plusone" g:plusone:size="{{ site.google_plus_one_size }}"></a>
    {% endif %}
    {% if site.jia_share %}
    {% include post/jia_share.html %}
    {% endif %}
</div>

并创建jia_share.html

    <!-- JiaThis Button BEGIN -->
<div class="jiathis_style_32x32">
    <a class="jiathis_button_qzone"></a>
    <a class="jiathis_button_tsina"></a>
    <a class="jiathis_button_tqq"></a>
    <a class="jiathis_button_weixin"></a>
    <a class="jiathis_button_renren"></a>
    <a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
    <a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js" charset="utf-8"></script>
<!-- JiaThis Button END -->

自定义404页面

直接打开source下的404.markdown进行编辑即可

自定义导航

新建一个页面 rake new_page['about me'];

在source->_inlcude->custom->navigation.html

    <ul class="main">
    <li><a href="{{ root_url }}/">Blog</a></li>
    <li><a href="{{ root_url }}/blog/archives">Archives</a></li>
</ul>

添加代码如下:

    <ul class="main">
    <li><a href="{{ root_url }}/">Blog</a></li>
    <li><a href="{{ root_url }}/blog/archives">Archives</a></li>
    <li><a href="{{ root_url }}/aboutme">About Me</a></li>
</ul>

安装使用主题

 到GitHub上找主题文件
 git clone git://github.com/tommy351/Octopress-Theme-Slash.git .themes/slash //会在theme主题下生成对应名称的文件夹
 rake install ['主题名称']

注意:
在上面安装主题前记得备份,因为安装主题过后你之前配置的信息都会被重置(如评论和分享)


设置链接在新窗口打开

在博文中,如果点击链接直接在本窗口打开了,那么用户体验就不是很好。而markdown的标准语法是不支持链接在新窗口打开的,虽然可以通过在markdown中直接写html标签来解决这个问题,但是这与markdown的简洁书写特性不符。但是我们可以通过设置Octopress来达到这种效果,即在 \source_includes\custom\head.html 文件中添加如下一段代码

<script>
  function addBlankTargetForLinks () {
    $('a[href^="http"]').each(function(){
      $(this).attr('target', '_blank');
    });
  }
  $(document).bind('DOMNodeInserted', function(event) {
    addBlankTargetForLinks();
  });
</script>

首页文章以摘要形式展示

  1. 在文章对应的markdown文件中,在需要显示在首页的文字后面添加 ,执行rake generate后在首页上会看到只显示<!—more—>前面的文字,文字后面会显示 Read on 链接,点击后进入文字的详细页面;

  2. 如果想将Read on修改为中文,可以修改_config.yml文件

excerpt_link: "Read on →"  # "Continue reading" link text at the bottom of excerpted articles
修改为:
excerpt_link: "阅读全文→"  # "Continue reading" link text at the bottom of excerpted articles

添加侧边栏文章分类(category)

  1. 在 plugins 目录下创建 category_list_tag.rb 文件,内容如下:
module Jekyll 
  class CategoryListTag < Liquid::Tag 
    def render(context) 
      html = "" 
      categories = context.registers[:site].categories.keys 
      categories.sort.each do |category| 
        posts_in_category = context.registers[:site].categories[category].size 
        category_dir = context.registers[:site].config['category_dir'] 
        category_url = File.join(category_dir, category.gsub(/_|\P{Word}/, '-').gsub(/-{2,}/, '-').downcase) 
        html << "<li class='category'><a href='/#{category_url}/'>#{category} (#{posts_in_category})</a></li>\n" 
      end 
      html 
    end 
  end 
end

Liquid::Template.register_tag('category_list', Jekyll::CategoryListTag)
  1. 添加 source/_includes/asides/category_list.html 文件,内容如下:
<section>
  <h1>文章分类</h1>
  <ul id="categories">
    \{\% category_list \%\} //去掉\
  </ul>
</section>
  1. 修改 _config.yml 文件,在 default_asides 项中添加 asides/category_list.html ,值之间以逗号隔开,值的先后顺序代表了侧边栏展现的先后顺序。
default_asides: [asides/category_list.html, asides/recent_posts.html, asides/github.html, asides/delicious.html, asides/pinboard.html, asides/googleplus.html]

自动为图片添加url前缀

把图片资源放在了七牛云存储 上,写博客时候就是用七牛的外链。但是这样有几个问题:

  • 每次写博客插入图片外链地址时候都很麻烦,需要给每张图片都添加七牛外链地址url前缀;
  • 如果以后更换了存储,那就麻烦了,需要依次编辑替换每个图片的url前缀
    现在我们就使用一种灵活的方式来配置并自动生成图片的URL前缀:

首先修改 /plugins/image_tag.rb 文件,在 @img['class'].gsub!(/"/, '') if @img['class'] 后添加下面一行代码:

@img['src'] = Jekyll.configuration({})['static_file_prefix'] + @img['src'] if @img['src'][0] == '/' 

然后再修改根目录下的 _config.yml 文件,添加如下配置:

# Add url prefix for image automatically
static_file_prefix: http://7u2i08.com1.z0.glb.clouddn.com

最后我们在插入图片的时候要记住不能再使用Markdown语法来写了,要使用Ocotpress自定义的IMG标签来插入图片 。

{% img http://placekitten.com/890/280 %}
{% img left http://placekitten.com/320/250 Place Kitten #2 %}
{% img right http://placekitten.com/300/500 150 250 Place Kitten #3 %}
{% img right http://placekitten.com/300/500 150 250 'Place Kitten #4' 'An image of a very cute kitten' %}

本地预览先generate后preview,这样一来插入图片就灵活方便多了。

添加访客统计

本博客的访客统计系统使用的是Flag Counter,所以要 先去Flag Counter获取代码 。

拿到代码后添加.\source\_includes\custom\asides\flag_counter.html 文件:

<section>
  <h1>访客统计</h1>
  <br/>
  <a href="http://s07.flagcounter.com/more/2SH"><img src="http://s07.flagcounter.com/count/2SH/bg_FFFFFF/txt_000000/border_CCCCCC/columns_2/maxflags_12/viewers_0/labels_0/pageviews_1/flags_0/" alt="Flag Counter" border="0"></a>
</section>

将页面添加到侧边栏,在 ./_config.yml 配置文件中添加下面一行配置:

default_asides: [……, custom/asides/flag_counter.html]

最后添加控制开关,在 ./_config.yml 配置文件中添加下面一行配置:

# Flag Counter
flag_counter: true
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,366评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,521评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,689评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,925评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,942评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,727评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,447评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,349评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,820评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,990评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,127评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,812评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,471评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,017评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,142评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,388评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,066评论 2 355

推荐阅读更多精彩内容