Skip to content

Commit

Permalink
Labels and references
Browse files Browse the repository at this point in the history
  • Loading branch information
grauschnabel authored and paulrobertlloyd committed Sep 23, 2023
1 parent 7d14028 commit 31bc029
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,27 @@ Content
</figure>
```

You can add labels and reference to it.

```liquid
{% figure caption:"This is some caption." label:myfigure %}
Content
{% endfigure %}
...
You can see in figure {% figref myfigure %}, ...
```

```html
<figure id="myfigure">
<p>Content</p>
<figcaption><em>Figure 1:</em> This is some caption.</figcaption>
</figure>
...
<p>You can see in figure <a href="#myfigure">1</a></p>
```

The word ‘Figure’ in the figcaption is translated according to the `lang` you set in the yaml header of your post. If your language is not supported simple set `figure` to the yaml header of your post to the value you want to use instead of 'Figure'.

## Configuration

Any markdown provided within the `{% figure %}` block is rendered using Jekyll's Markdown parser, [Kramdown](https://kramdown.gettalong.org). However, this means images and other content will be wrapped within `<p>` tags, like so:
Expand Down
22 changes: 22 additions & 0 deletions lib/jekyll-figure-ref.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'utils'

module Jekyll
module Figure

class FigRefTag < Liquid::Tag
include Utils

def initialize(tag_name, markup, tokens)
@label = markup
super
end

def render(context)
@context = context
print_reference(@label)
end
end
end
end

Liquid::Template.register_tag("figref", Jekyll::Figure::FigRefTag)
12 changes: 11 additions & 1 deletion lib/jekyll-figure.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
require 'utils'
require 'jekyll-figure-ref'

module Jekyll
module Figure

class FigureTag < Liquid::Block
include Utils

def initialize(tag_name, markup, tokens)
@markup = markup
super
Expand All @@ -24,11 +29,14 @@ def render(context)
@settings = site.config["jekyll-figure"]
@caption = attributes["caption"]
@class = attributes["class"]
@label = attributes["label"]
@context = context

# Caption: convert markdown and remove paragraphs
unless @caption.nil?
figure_caption = @caption.gsub!(/\A"|"\Z/, "")
figure_caption = converter.convert(figure_caption).gsub(/<\/?p[^>]*>/, "").chomp
figure_caption = print_figure_counter(@label) + figure_caption unless @label.nil? || @label.empty?
figure_caption = " <figcaption>#{figure_caption}</figcaption>\n"
end

Expand All @@ -38,6 +46,8 @@ def render(context)
figure_class = " class\=\"#{figure_class}\""
end

figure_label = @label.nil? || @label.empty? ? "" : ' id="' + @label.gsub(/A"|"\Z/, "") + '"'

# Content
if @settings && @settings["paragraphs"] == false
# Strip paragraphs
Expand All @@ -51,7 +61,7 @@ def render(context)
markdown_escape = "\ "

# Render <figure>
figure_tag = "<figure#{figure_class}>"
figure_tag = "<figure#{figure_class}#{figure_label}>"
figure_tag += "#{figure_main}"
figure_tag += "#{figure_caption}"
figure_tag += "</figure>"
Expand Down
47 changes: 47 additions & 0 deletions lib/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Jekyll
module Figure
module Utils

class I18n
@figure_i18n = {'en' => "Figure",
'de' => "Abbildung",
'sv' => "Figur",
'fr' => "Figure"}
def self.figure(lang)
return @figure_i18n[lang] if @figure_i18n.key?(lang)
@figure_i18n['en']
end
end

def lang
return @context.registers[:page]["lang"].to_s if @context.registers[:page].key?("lang")
"en".to_s
end

def figure
return @context.registers[:page]["figure"].to_s if @context.registers[:page].key?("figure")
I18n.figure(lang)
end

def print_figure_counter(label)
label.gsub!(/\s/, '')
@context.registers[:page]["figure_labels"] ||= {}

if @context.registers[:page]["figure_labels"].key?(label)
value = @context.registers[:page]["figure_labels"][label]
else
value = @context.registers[:page]["figure_labels"].length + 1
@context.registers[:page]["figure_labels"][label] = value
end
"<em>" + figure + " " + value.to_s + ":</em> "
end

def print_reference(label)
label.gsub!(/\s/, '')
"<a href=\"\##{label.to_s}\">" +
@context.registers[:page]["figure_labels"][label.to_s].to_s +
"</a>"
end
end
end
end

0 comments on commit 31bc029

Please sign in to comment.