MWeb 1.4 新功能介绍二:静态博客功能增强

<p>MWeb 比较有特色的是一键生成静态博客功能,然后从 MWeb 最开始规划要做静态博客生成功能时,我就希望 MWeb 的静态博客生成功能在易用的同时,还要有很强大的扩展性。</p>

<p>比如说能自己增加网站公告,这个公告可以随时更换和取消。再比如说能自己为网站增加广告,而且可以增加多个广告位,google 的或者自定的广告都可以。这些之前都可以通过修改模板解决,但是有些是解决不了的:比如说把文章列表做成像 theverge <a href="http://www.theverge.com/reviews">http://www.theverge.com/reviews</a> 的页的效果,就是列表中有设定的特色图片,有自定的简介,有评分,有价格时会显示价格等。比如说设定 SEO 的一些信息,设定分享到 Facebook 的一些信息。又比如说播客类的网站,要生成 Apple 规格的 RSS。</p>

<p>1.4 版的静态博客增强就是解决这些问题的。我一直考虑如何介绍这个功能,然后因为之前比特新声的主播之一 <a href="http://weibo.com/haohailong">郝海龙</a> 随意问了一下是否支持生成 Podcast 的 RSS。所以我想来想去,还是介绍用 MWeb 自带的 greyshade 网站模板做一个可以写博客又可以做为播客的新模板好了。</p>

<p>这个新模板的 Demo 网址是:<a href="http://coderforart.com/bitvoice/">http://coderforart.com/bitvoice/</a>,Podcast RSS 为:<a href="http://coderforart.com/bitvoice/podcast-atom.xml">http://coderforart.com/bitvoice/podcast-atom.xml</a>。 一般情况下只要到这个网址体验一下,并看一下《音乐《梁祝》及 MWeb 静态博客功能增强的使用体验》这篇文章即可,因为下边的内容是跟自定模板有关。</p>

<!--more-->

<p>下面正式开始做这个新模板。<br/> 首先在 MWeb 中新增一个静态网站分类,名为比特新声。然后设置如下,我是想放在我的博客中做 Demo,所以网址为:<a href="http://coderforart.com/bitvoice/">http://coderforart.com/bitvoice/</a>。评论和分类代码、GA 代码都跟 coderforart 一样。</p>

<p><img src="http://static.oschina.net/uploads/img/201505/08144129_uEPn.jpg" alt=""/></p>

<p>点上图中最下边的 <code>Advanced Settings</code> 后会出来如下图的网站扩展设置。</p>

<p><img src="http://static.oschina.net/uploads/img/201505/08144125_Wjre.jpg" alt=""/></p>

<p>一开始打开时,<code>Site Setting</code> 这个 Tab 是没有内容的,可以看到 MWeb 支持整个网站的字段扩展(Site Extension)和网站所属文档的字段扩展(Document Extension)。</p>

<p>如下图,我在 <code>Site Extension</code> 这个 Tab 根据 <a href="https://www.apple.com/itunes/podcasts/specs.html">Apple Podcast RSS</a> 自定了一些必要字段,然后切到 <code>Site Setting</code> 这个 Tab,就会看到按照自定的字段生成可以输入的文本框了。</p>

<p><img src="http://static.oschina.net/uploads/img/201505/08144112_r7zA.jpg" alt=""/></p>

<p>如右边,我可以填入相关文案。然后如果有图片怎么办,简单的办法是打开比特新声分类内的一个文档,直接把图拖入编辑器中,从生成的 Markdown 语法中取其地址贴上就好。如上图右边的图中的 <code>Fb Image</code> 字段。</p>

<p>然后我也在 <code>Document Extension</code> 这个 Tab 也根据 <a href="https://www.apple.com/itunes/podcasts/specs.html">Apple Podcast RSS</a> 自定了一些必要字段,这些字段可以点软件右上角的设置按钮,并点第三个 Tab <code>Extension</code> 就能看到了按自定字段生成的可输入文本框了,如图。</p>

<p><img src="http://static.oschina.net/uploads/img/201505/08144126_bE7E.jpg" alt=""/></p>

<p>这样的话,如果想新增新一期播客,就点开第三个 <code>Extension</code> Tab,填入一些这一期播客的相关信息即可。如果新增的文档只是普通文章,则可以不用管扩展的信息。</p>

<p>下图是关于 <code>Base</code> Tab 和 <code>Media</code> Tab 的情况。</p>

<p><img src="http://static.oschina.net/uploads/img/201505/08144110_wANF.jpg" alt=""/></p>

<p><code>Media</code> Tab 会列出所有你引入到当前文档中的图片、附件等等。比如要增加一张某期的播客的封面图片,文章里用不到,但是 <code>Extension</code> Tab 里要填入,就可以先把图拖入编辑器内,然后删除,再点 <code>Media</code> Tab 就会看到所拖入的图了,点 <code>Copy Path</code> 然后贴到 <code>Extension</code> Tab 的某字段即可。</p>

<p>字段设置好了,下面要开始修改 greyshade 网站模板了。目前看来只需要为属于播客的文章增加一个播放器,让它可以播放 MP3。然后修改 RSS,让 RSS 按 Apple 播客的格式即可。</p>

<p>以下为修改模板的内容,建议先参考 <a href="http://zh.mweb.im/custom-site-theme-detail-zh.html">自定网站或博客主题简单说明</a>。</p>

<p>首先增加播放器,我随便找了一下,看到 <a href="http://www.schillmania.com/projects/soundmanager2/demo/bar-ui/">http://www.schillmania.com/projects/soundmanager2/demo/bar-ui/</a> 这个播放器不错,所以就决定用这个了,首先复制 greyshade 模板,并重命名为:podcast-greyshade。把 soundmanager 的相关代码放到 asset/soundmanager 中。然后打开 header.html 文档,在上面增加 soundmanager2 播放器的代码,大概是这样:</p>

<pre><code class="language-js">&lt;link href=&quot;asset/soundmanager/css/bar-ui.css&quot; media=&quot;screen, projection&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;&gt; &lt;script type=&quot;text/javascript&quot; src=&quot;asset/soundmanager/soundmanager2-jsmin.js&quot;&gt;&lt;/script&gt; &lt;script type=&quot;text/javascript&quot; src=&quot;asset/soundmanager/bar-ui.js&quot;&gt;&lt;/script&gt; &lt;script&gt; soundManager.setup({ preferFlash: false, onready: function() { } }); &lt;/script&gt; </code></pre>

<p>因为我需要在文章列表和单篇文章都支持播放功能,所以为 category.html 和 post.html 增加播放支持。代码大概如下(我只选关键代码)。</p>

<pre><code class="language-html">{% if post.ext_enclosureURL.length %} &lt;div class=&quot;sm2-bar-ui compact full-width&quot;&gt; .... &lt;div class=&quot;sm2-inline-duration&quot;&gt;{{ post.ext_itunes_duration }}&lt;/div&gt; &lt;div class=&quot;sm2-playlist-wrapper&quot;&gt; &lt;ul class=&quot;sm2-playlist-bd&quot;&gt; &lt;li&gt;&lt;a href=&quot;{{ post.ext_enclosureURL }}&quot;&gt;&lt;b&gt;{{ post.ext_itunes_author }}&lt;/b&gt; - {{ post.ext_title }}&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; ... &lt;/div&gt; {% /if %} </code></pre>

<p>下面解释一下上面这段代码。<br/> MWeb 中 <code>Site Setting</code> 设置的所有值会传到所有模板页,比如说要输出上面设置的 SEO Des 的值,用 <code>{{ext_seo_des}}</code> 即可。可以看到,规则是: <code>ext_ + variable Name</code> 。然后用软件右上角第一个按钮设置的文档中的 <code>Extension</code> 的值,会传到所有 <code>文档对象变量</code> 中,如上面代码,文档对象变量为 <code>post</code>,访问规则为 <code>post.ext_ + variable Name</code>。 然后请注意 <code>{% if post.ext_enclosureURL.length %} {% /if %}</code> 这种写法,这是为了判断这文章是否有设置了播客的相关值,有设置,说明这文章为播客文章。</p>

<p>下面介绍一下如何增加输出 Apple 支持的播客的 RSS 格式。 </p>

<p>MWeb 一般情况下只生成全站文章的 RSS,名字为 atom.xml。然后 MWeb 为了满足扩展需要,增加了另外的规则,为:如果网站模板中存在有 <code>分类的 HTML File Name 的值 + -atom.xml</code> 这样的模板文档,则会为这个分类用 <code>分类的 HTML File Name 的值 + -atom.xml</code> 这个模板生成名为 <code>分类的 HTML File Name 的值 + -atom.xml</code> 的 RSS。</p>

<p>如上面,我增加了两个子分类:文章和播客。然后我设置了播客子分类的 <code>HTML File Name</code> 为 <code>podcast</code>。然后我在网站模板中也增加了名为 <code>podcast-atom.xml</code> 的模板,下面是这个模板的内容。</p>

<pre><code class="language-xml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; &lt;rss xmlns:itunes=&quot;http://www.itunes.com/dtds/podcast-1.0.dtd&quot; version=&quot;2.0&quot;&gt; &lt;channel&gt; &lt;title&gt;&lt;![CDATA[{{ siteName }}]]&gt;&lt;/title&gt; &lt;link&gt;{{ siteURL }}&lt;/link&gt; &lt;language&gt;zh-CN&lt;/language&gt; {% if ext_copyright.length %} &lt;copyright&gt;{{ ext_copyright }}&lt;/copyright&gt; {% /if %} {% if ext_itunes_subtitle.length %} &lt;itunes:subtitle&gt;{{ ext_itunes_subtitle }}&lt;/itunes:subtitle&gt; {% /if %} {% if ext_itunes_author.length %} &lt;itunes:author&gt;{{ ext_itunes_author }}&lt;/itunes:author&gt; {% /if %} {% if ext_itunes_summary.length %} &lt;itunes:summary&gt;{{ ext_itunes_summary }}&lt;/itunes:summary&gt; {% /if %} {% if ext_itunes_description.length %} &lt;description&gt; {{ ext_itunes_description }} &lt;/description&gt; {% /if %} &lt;itunes:owner&gt; {% if ext_itunes_author.length %}&lt;itunes:name&gt;{{ ext_itunes_author }}&lt;/itunes:name&gt;{% /if %} {% if ext_itunes_email.length %}&lt;itunes:email&gt;{{ ext_itunes_email }}&lt;/itunes:email&gt;{% /if %} &lt;/itunes:owner&gt; {% if ext_itunes_image.length %} &lt;itunes:image href=&quot;{{ ext_itunes_image }}&quot; /&gt; {% /if %} {% if ext_itunes_category.length %} &lt;itunes:category text=&quot;{{ ext_itunes_category }}&quot;/&gt; {% /if %} {% for p in posts %} {% if p.ext_enclosureURL.length %} &lt;item&gt; &lt;title&gt;{{ p.ext_title }}&lt;/title&gt; &lt;itunes:author&gt; {{ p.ext_itunes_author }}&lt;/itunes:author&gt; &lt;itunes:subtitle&gt; {{ p.ext_itunes_subtitle }} &lt;/itunes:subtitle&gt; &lt;itunes:summary&gt;&lt;![CDATA[ {% if p.ext_itunes_summary.length %} {{ p.ext_itunes_summary }} {% else %} {{ p.content }} {% /if %} ]]&gt; &lt;/itunes:summary&gt; &lt;itunes:image href=&quot;{{ p.ext_itunes_image }}&quot; /&gt; &lt;enclosure url=&quot;{{ p.ext_enclosureURL }}&quot; length=&quot;{{ p.ext_enclosure_length }}&quot; type=&quot;{{ p.ext_enclosure_type }}&quot; /&gt; &lt;guid&gt;{{ p.ext_guid }}&lt;/guid&gt; &lt;pubDate&gt;{{ p.ext_pubDate }}&lt;/pubDate&gt; &lt;itunes:duration&gt;{{ p.ext_itunes_duration }}&lt;/itunes:duration&gt; &lt;/item&gt; {% /if %} {% /for %} &lt;/channel&gt; &lt;/rss&gt; </code></pre>

<p>至此,podcast-greyshade 这个新模板已经完成了,增加了播放器支持和把所有播客文章生成为 Apple 播客格式的 RSS。</p>

<p>podcast-greyshade 这个网站模板已传到 github:<a href="https://github.com/oulvhai/MWeb-Themes/tree/master/podcast-greyshade">https://github.com/oulvhai/MWeb-Themes/tree/master/podcast-greyshade</a><br/> 使用 podcast-greyshade 这个网站模板的 Demo 的网址:<a href="http://coderforart.com/bitvoice/">http://coderforart.com/bitvoice/</a>。我只增加了两篇播客文章。然后 Apple 规格的 RSS 输出网址为:<a href="http://coderforart.com/bitvoice/podcast-atom.xml">http://coderforart.com/bitvoice/podcast-atom.xml</a></p>

转载于:https://my.oschina.net/oulvhai/blog/412268

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值