Class: Jekyll::UJPostTag

Inherits:
Liquid::Tag
  • Object
show all
Includes:
Jekyll::UJPowertools::VariableResolver
Defined in:
lib/tags/post.rb

Instance Method Summary collapse

Methods included from Jekyll::UJPowertools::VariableResolver

#is_quoted?, #parse_options, #resolve_input, #resolve_variable

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ UJPostTag

Returns a new instance of UJPostTag.



8
9
10
11
# File 'lib/tags/post.rb', line 8

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

Instance Method Details

#render(context) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tags/post.rb', line 13

def render(context)
  # Parse arguments
  args = parse_arguments_with_quotes(@markup)
  post_input = args[0]
  property_input = args[1] || "'title'"  # Default to title if no property specified

  # Strip quotes from property if present
  property = property_input.gsub(/^['"]|['"]$/, '')

  # Resolve post ID
  post_id = is_quoted?(post_input) ?
            post_input.gsub(/^['"]|['"]$/, '') :
            resolve_post_id(context, post_input)
  return '' unless post_id

  # Find post in site collections
  site = context.registers[:site]
  post = find_post(site, post_id)
  return '' unless post

  # Return the requested property
  case property
  when 'title'
    post.data['title'] || ''
  when 'description'
    post.data['description'] || post.data['excerpt'] || ''
  when 'url'
    site_url = site.config['url'] || ''
    site_url + post.url
  when 'path'
    post.url
  when 'date'
    post.data['date'] ? post.data['date'].strftime('%Y-%m-%d') : ''
  when 'author'
    (post.data['post'] && post.data['post']['author']) || post.data['author'] || ''
  when 'category'
    post.data['category'] || post.data['categories']&.first || ''
  when 'categories'
    Array(post.data['categories']).join(', ')
  when 'tags'
    Array(post.data['tags']).join(', ')
  when 'id'
    post.id
  when 'image'
    # Use the custom post.post.id if available, otherwise fall back to extracting from post.id
    custom_id = (post.data['post'] && post.data['post']['id']) || post.id.gsub(/^\/(\w+)\//, '')
    # Extract the slug from the Jekyll post ID
    post_id_clean = post.id.gsub(/^\/(\w+)\//, '')
    slug = post_id_clean.gsub(/^\d{4}-\d{2}-\d{2}-/, '')
    "/assets/images/blog/post-#{custom_id}/#{slug}.jpg"
  when 'image-tag'
    # Generate image path
    # Use the custom post.post.id if available, otherwise fall back to extracting from post.id
    custom_id = (post.data['post'] && post.data['post']['id']) || post.id.gsub(/^\/(\w+)\//, '')
    # Extract the slug from the Jekyll post ID
    post_id_clean = post.id.gsub(/^\/(\w+)\//, '')
    slug = post_id_clean.gsub(/^\d{4}-\d{2}-\d{2}-/, '')
    image_path = "/assets/images/blog/post-#{custom_id}/#{slug}.jpg"

    # Parse additional options for the image tag
    image_options = parse_image_options(args[2..-1], context)

    # Set default alt text if not provided
    if !image_options['alt']
      # Try to get the title from post.post.title first, then fall back to post.title
      default_alt = (post.data['post'] && post.data['post']['title']) || post.data['title']
      image_options['alt'] = default_alt if default_alt
    end

    # Build the markup string for uj_image tag
    image_markup = build_image_markup(image_path, image_options)

    # Parse and render the uj_image tag using Liquid template
    template_content = "{% uj_image #{image_markup} %}"
    template = Liquid::Template.parse(template_content)
    template.render!(context)
  else
    # Try to access any other property dynamically
    (post.data['post'] && post.data['post'][property]) || post.data[property] || ''
  end
end