搜索此博客

2007年3月30日星期五

又从 Typo 搬到 Mephisto 了……

Hack 1: 文章 meta 的修改



这里的 meta 是指每个 entry 下面一小块区域,包括文章作者、所属分类等信息,以及指向评论的链接。最初的模板没有订阅评论的链接,也没有文章所属 tag 的信息,所以改动模板的 _shared.liquid 文件如下,加入:






1
2

<span class="tag">Tagged {{ article | linked_tag_list }}</span>



以及:






1
2
3
4
5

6
7
8

{% unless mode == 'single' %}
{% if article.accept_comments %}
<a href="{{ article.url }}#comments" class="commentslink">

{{ article.comments_count | pluralize: 'comments' }} &#187;</a> &nbsp;
<a href="{{ article.comments_feed_url }}" class="commentslink">

Subscribe to comments</a>{% endif %}
{% endunless %}


代码基本是自解释的,我就不说啥了。



Hack 2: 每次都要折腾的分类 feed 链接



分类的链接比较简单,Mephisto 的 wiki 上有说明,另有一个 articles_feed 的 filter 可以帮助获取用于自动识别的 feed,加上后会在 html 的 <head&rt; 部分加入 feed 地址,像 Firefox 这样的浏览器就可以自动发现这些 feed。但为了让使用 IE 之类浏览器的人也能看到 feed,就比较麻烦,google 了半天之后发现 mephisto 提供了一个 helper,叫 section_feed_url,终于搞定,代码是这个样子:






1
2
3

4
5
6
7
8
9
10


<ul>
{% for category in site.blog_sections %}
{% unless category.name == 'Home' %}
<li>{{ category | link_to_section }} ({{ category.articles_count }})
{{ category | articles_feed }}
<a href="{{ category | section_feed_url }}" class="tiny">(atom)</a>

</li>
{% endunless %}{% endfor %}
</ul>


折腾了一通之后发现有人写了插件提供这个功能……另外 Home 分类是默认的,就是全部文章,这里用 unless 把它去掉。



Hack 3: 最近评论



这是 0.7.3 最新支持的功能,在 CHANGELOG 里他们给出了一种用法,但无论怎么改参数,给出的评论数总是跟默认的页面文章数一致,似乎是参数没有传递过去。后来还是在 Mephisto Wiki 上的提问与回答部分找到了一段代码,依葫芦画瓢:






1
2
3

4
5
6
7
8

{{ site | latest_comments:5 | assign_to:"comments" }}

<ul>
{% for comment in comments %}
<li>{{ comment.author_link }} on {{ comment | link_to_article }} -
{{ comment.body | truncatewords: 10 }}</li>
{% endfor %}

</ul>


至今也不明白为什么像 {{ site | latest_comments :5 }} 这样的代码不行,非得用 assign_to



Hack 4: 每月文章



这个也是 Mephisto Wiki 上直接给了解决办法的,直接贴出代码:






1
2
3
4
5

6
7
8
9
10

<ul>
{% for month in site.home_section.months %}

{{ site.home_section | monthly_articles: month | size | assign_to: 'articles_count' }}
{% if articles_count > 0 %}
<li>{{ site.home_section | link_to_month: month }}
({{ articles_count }})</li>
{% endif %}

{% endfor %}
</ul>


Hack 5: 分类存档



我最得意的 hack :)。Mephisto 的作者非常痛恨 blog 分页,据说是因为这样文章的链接会随其页面位置改变,不方便别人用搜索引擎查找,所以 Mephisto 默认没有分页功能,也没有“上一页”这样的选项。还好每月的文章是可以全部显示的,但查看分类、搜索和首页的文章数等等都要受同一个变量的控制。首页没有早期文章的链接还可以忍,分类也只能看十几篇就比较过分了,好在摸索之后发现每个分类也是可以按照月份显示所有文章的,通过以下代码:






1

2
3
4
5
6
7
8
9
10


<ul>
{% for month in section.months %}
{{ section | monthly_articles: month | size | assign_to: 'articles_count' }}
{% if articles_count > 0 %}

<li class="month">{{ section | link_to_month: month }}
({{ articles_count }}) </li>
{% endif %}
{% endfor %}

</ul>


建立一个分类页面专用的模板,在 sidebar 部分加入即可通过每月文章访问到分类中所有的文章。但是这样跟我所要的效果还有一些差距。怎样能看到所有文章的标题呢?参考上面的代码,我依葫芦画瓢写了如下的代码,试验了一下,运气不错:






1
2
3

4
5
6
7
8
9
10
11
12

13
14
15

<ul>
{% for month in section.months %}
{{ section | monthly_articles: month | size | assign_to: 'articles_count' }}

{% if articles_count > 0 %}
<li class="month">{{ section | link_to_month: month }}
({{ articles_count }})
{{ section | monthly_articles: month | assign_to: 'articlesmonth' }}

{% for article in articlesmonth %}
<li>{{ article | link_to_article }}</li>
{% endfor %}
</li>
{% endif %}

{% endfor %}
</ul>


增加了一重循环,在每月文章内再分别列出文章的题目,第二个 assign_to 的用法比较有趣。



Hack 6: Gravatar 支持



Mephisto 的管理界面直接支持 Gravatar 的头像,但我一开始没有找到对应的 filter,而插件工作也始终不正常。困扰了许久后在 Google Groups 上的 Mephisto Blog 小组找到了答案,原来 filter 就在 mephisto/apps/helper/url_filters.rb 这个文件中:






1
2
3
4
5

6
7
8
9
10
11
12


####################################################
def gravatar_author(author, size=80, default=nil)
return '' unless author['email']

url = "http://www.gravatar.com/avatar.php?
size=
#{size}&gravatar_id=#{Digest::MD5.hexdigest(author['email'])}"


url << "&default=#{default}" if default

image_tag url, :class => 'gravatar', :size =>

"#{size}x#{size}", :alt => "author['login']"
end

####################################################


修改一下评论模板,在合适的地方加上 {{ comment | gravatar: 32, "http://snakehsu.info/contacts.png"}},即可看到评论者的 gravatar。两个参数分别是头像的大小和没有头像时的默认图像。



顺便说一句,前几天 gravatar 工作不正常是因为数据库和服务器的配合问题,目前已经基本解决,但是需要用户自己登录 gravatar 再重新提交一次头像。



Hack 7 & 8: 搜索和 Tag Cloud



如上面所说,内置搜索的结果只能给出最初十几个,所以我试图用 Google 定制搜索引擎 来解决这个问题,不过看来 Google 还没有来得及索引这个新地址,所以这几天内估计用 Google 搜不到这里的东西,且看看过一周后怎么样。



Tag Cloud 也是用的现成的插件,唯一的问题是域名不一致,因为它认为我的 blog 的基础地址是 snakehsu.info 而不是 snakehsu.info/main/,我也没空钻研 URL 重写的问题,就直接改了插件的源文件了事,这个是我唯一的一处”硬“修改。



现在这个新 blog 基本实现了过去 Wordpress blog 的所有功能,有些地方还更方便,比如内置的 Gravatar 支持和 Markdown (类似 emacs-wiki 的标记语言)。折腾了好久,现在基本可以熟练地在共享主机上安装 Ruby on Rails 程序了……

2 条评论:

  1. 目前解决了一半,自动发现的 atom 可以用了。之前错误原因是导入原来的数据库时 userid 与现在的不一致,导致 ruby 出错。
    现在页面上 sidebar 里的 atom 链接还有问题,要把 /main 去掉才能用,可能跟我之前硬改设置有关系,但改回来后问题依旧,可能是 cache 没有清除的缘故。

    回复删除
  2. 你Blog的RSS源全部出错,无任何输出。快修复吧~

    回复删除