Class: Koziolekweb::Tags::Book
- Inherits:
-
Liquid::Tag
- Object
- Liquid::Tag
- Koziolekweb::Tags::Book
- Defined in:
- lib/jekyll/koziolekweb/tags.rb
Overview
A tag that displays book information with cover image in a structured format.
Usage:
{% book "Title" "Author" "2024" "1234567890" "https://example.com/cover.jpg" %}
Optional language parameter:
{% book "Title" "Author" "2024" "1234567890" "https://example.com/cover.jpg" lang:pl %}
The tag requires:
-
Title (in quotes)
-
Author (in quotes)
-
Publication year (4 digits)
-
ISBN (can be empty string)
-
Cover image URL
Language support:
-
Default language is ‘en’
-
Language files should be in _data/lang/.yml
-
Language codes must be 2 letters (e.g. en, pl, de)
Constant Summary collapse
- MIN_ARGS =
Minimum number of required arguments (title, author, year, isbn, cover_url)
5
Instance Method Summary collapse
-
#initialize(tag_name, markup, tokens) ⇒ Book
constructor
A new instance of Book.
- #render(_context) ⇒ Object
Constructor Details
#initialize(tag_name, markup, tokens) ⇒ Book
Returns a new instance of Book.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/jekyll/koziolekweb/tags.rb', line 135 def initialize(tag_name, markup, tokens) super args = Shellwords.split(markup) raise Liquid::SyntaxError, "Invalid usage" if args.size < MIN_ARGS @title, , @year, @isbn, @cover_url = args[0..4].map { _1.delete('"') } unless @year.match?(/\A\d{4}\z/) raise Liquid::SyntaxError, "Invalid publication year: #{@year.inspect}" end lang_arg = args.find { |s| s.start_with?("lang:") } @lang = lang_arg&.split(":", 2)&.last || "en" unless @lang.match?(/\A[a-z]{2}\z/i) raise Liquid::SyntaxError, "Invalid language code: #{@lang.inspect}" end # Inicjalizacja LanguageManager lang_data_path = File.join(Dir.pwd, '_data', 'lang') # Katalog z plikami językowymi config_path = File.join(Dir.pwd, '_config.yml') # Ścieżka do pliku `_config.yml` @language_manager = Koziolekweb::LanguageSupport::LanguageManager.new(config_path, lang_data_path) end |
Instance Method Details
#render(_context) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/jekyll/koziolekweb/tags.rb', line 159 def render(_context) title_html = CGI.escapeHTML(@title) = CGI.escapeHTML() year_html = CGI.escapeHTML(@year) isbn_html = CGI.escapeHTML(@isbn.to_s) label_title = @language_manager.translate('title', @lang, 'Tytuł') = @language_manager.translate('author', @lang, 'Autor') label_year = @language_manager.translate('year', @lang, 'Rok') label_isbn = @language_manager.translate('isbn', @lang, 'ISBN') cover_alt = @language_manager.translate('cover_alt', @lang, 'Okładka książki %{title} autorstwa %{author}') .gsub('%{title}', @title) .gsub('%{author}', ) " <div class=\"book\">\n <img src=\"\#{@cover_url}\" alt=\"\#{cover_alt}\" title=\"\#{@title}\" class=\"cover\" />\n <div class=\"book_desc\">\n <ul>\n <li><span>\#{label_title}: </span>\#{title_html}</li>\n <li><span>\#{label_author}: </span>\#{author_html}</li>\n <li><span>\#{label_year}: </span>\#{year_html}</li>\n \#{@isbn.nil? ? \"\" : \"<li><span>\#{label_isbn}: </span>\#{isbn_html}</li>\"}\n </ul>\n </div>\n </div>\n HTML\nend\n" |