Octopress中的图片标题

本文介绍了一种在Octopress中实现图片标题的方法,通过创建自定义插件image_caption_tag.rb,使得用户可以方便地为图片添加标题。同时,文章还提供了样式调整的方法,确保图片及其标题在页面布局中的美观。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我想给图片加上标题,然后在这里找到了答案,我把Ted Kulp的灵活解决方法整理了一下。

如下markdown

{% imgcap http://some.url.com/pic.jpg Leonhard Euler %} 

运行结果为:

也可以像img命令一样使用left和right.

译注:图片位于blog文字内容的左边或者右边(在命令后面加入 left或者right)。如下:

{% imgcap left http://some.url.com/pic.jpg Leonhard Euler %} 


具体变化

首先,在plugins子文件夹下创建image_caption_tag.rb插件。

plugins/image_caption_tag.rb :

# Title: Image tag with caption for Jekyll
# Description: Easily output images with captions

module Jekyll

  class CaptionImageTag < Liquid::Tag
    @img = nil
    @title = nil
    @class = ''
    @width = ''
    @height = ''

    def initialize(tag_name, markup, tokens)
      if markup =~ /(\S.*\s+)?(https?:\/\/|\/)(\S+)(\s+\d+\s+\d+)?(\s+.+)?/i
        @class = $1 || ''
        @img = $2 + $3
        if $5
          @title = $5.strip
        end
        if $4 =~ /\s*(\d+)\s+(\d+)/
          @width = $1
          @height = $2
        end
      end
      super
    end

    def render(context)
      output = super
      if @img
        "<span class='#{('caption-wrapper ' + @class).rstrip}'>" +
          "<img class='caption' src='#{@img}' width='#{@width}' height='#{@height}' title='#{@title}'>" +
          "<span class='caption-text'>#{@title}</span>" +
        "</span>"
      else
        "Error processing input, expected syntax: {% img [class name(s)] /url/to/image [width height] [title text] %}"
      end
    end
  end
end

Liquid::Template.register_tag('imgcap', Jekyll::CaptionImageTag)

然后,修改_utilities.scss文件。

sass/base/_utilities.scss :

   border: $border;
 }

+@mixin reset-shadow-box() {
+  @include shadow-box(0px, 0px, 0px);
+}
+
 @mixin selection($bg, $color: inherit, $text-shadow: none){
   * {
     &::-moz-selection { background: $bg; color: $color; text-shadow: $text-shadow; }

最后,将下列改变应用到_blog.scss文件。

sass/partials/_blog.scss :

   article {
     font-size: 2.0em; font-style: italic;
     line-height: 1.3em;
   }
-  img, video, .flash-video {
+  img, video, .flash-video, .caption-wrapper {
     @extend .flex-content;
     @extend .basic-alignment;
     @include shadow-box;
+    &.caption {
+      @include reset-shadow-box;
+    }
+  }
+  .caption-wrapper {
+    display: inline-block;
+    margin-bottom: 10px;
+    .caption-text {
+      background: #fff;
+      text-align: center;
+      font-size: .8em;
+      color: #666;
+      display: block;
+    }
   }
   video, .flash-video { margin: 0 auto 1.5em; }
   video { display: block; width: 100%; }

重新生成你的Octopress网站,就可以开始用imgcap来代替img了。

译注:修改后img和imgcap两种方式都可以使用。要加标题,就用imgcap,不加则用img。

原文地址:http://blog.zerosharp.com/image-captions-for-octopress/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值