jekyll-titles-from-headings 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 84b1cda340e7783f0500f07bf11623b19b416ffb
4
- data.tar.gz: f6dfc3e35d9f73b9e6c2f54305277382f082205b
3
+ metadata.gz: 367e754467d30e9b6ef62d13fb7c1f0030d7c165
4
+ data.tar.gz: 0e0f69aad2c526cc74cf5a32318879b05013e35e
5
5
  SHA512:
6
- metadata.gz: 8fcd40fa5b965ea1ff04572f493f47de8a67ba61b4096feb8ebecef6b0be01b7b968bbcf628b1c5806eeeec69aac508dfc19eca14651d7687cb7c13ba0ec2578
7
- data.tar.gz: 689a0d575e1cfe1ffa95fea8d15a35112c7b79c205b394ef0e5bdaefbf5ea5b75893af44ee83e5703a1d7b76fb879aba541134ec58967eeff5bafb2b6ccc3175
6
+ metadata.gz: d44d0aa1fdb4dc000ba159948723e4314014d67917be7a131066ce99d079452f8e40963d0cd33d95295fee28345333f6b8f099b92c39375ccb139e6285935c9f
7
+ data.tar.gz: 8f03486d52cb21770c8cb0314f090ab4096f1da54c8904fffe51933d5e406a3eda80cc91180d451c3f73adfcc27203348ec0f229a891157cd2a9ce9495bbf304
@@ -2,4 +2,6 @@ require "jekyll"
2
2
  require "jekyll-titles-from-headings/generator"
3
3
 
4
4
  module JekyllTitlesFromHeadings
5
+ autoload :Context, "jekyll-titles-from-headings/context"
6
+ autoload :Filters, "jekyll-titles-from-headings/filters"
5
7
  end
@@ -0,0 +1,13 @@
1
+ module JekyllTitlesFromHeadings
2
+ class Context
3
+ attr_reader :site
4
+
5
+ def initialize(site)
6
+ @site = site
7
+ end
8
+
9
+ def registers
10
+ { :site => site }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module JekyllTitlesFromHeadings
2
+ class Filters
3
+ include Jekyll::Filters
4
+ include Liquid::StandardFilters
5
+
6
+ def initialize(site)
7
+ @site = site
8
+ @context = JekyllTitlesFromHeadings::Context.new(site)
9
+ end
10
+ end
11
+ end
@@ -2,8 +2,23 @@ module JekyllTitlesFromHeadings
2
2
  class Generator < Jekyll::Generator
3
3
  attr_accessor :site
4
4
 
5
- TITLE_REGEX = %r!\A\s*\#{1,3} (.*)\n$!
5
+ TITLE_REGEX =
6
+ %r!
7
+ \A\s* # Beginning and whitespace
8
+ (?: # either
9
+ \#{1,3}\s+(.*) # atx-style header
10
+ | # or
11
+ (.*)\r?\n[-=]+\s* # Setex-style header
12
+ )$ # end of line
13
+ !x
6
14
  CONVERTER_CLASS = Jekyll::Converters::Markdown
15
+ STRIP_MARKUP_FILTERS = %i[
16
+ markdownify strip_html normalize_whitespace
17
+ ].freeze
18
+
19
+ # Regex to strip extra markup still present after markdownify
20
+ # (footnotes at the moment).
21
+ EXTRA_MARKUP_REGEX = %r!\[\^[^\]]*\]!
7
22
 
8
23
  safe true
9
24
  priority :lowest
@@ -40,9 +55,21 @@ module JekyllTitlesFromHeadings
40
55
  def title_for(document)
41
56
  return document.data["title"] if title?(document)
42
57
  matches = document.content.match(TITLE_REGEX)
43
- matches[1] if matches
58
+ strip_markup(matches[1] || matches[2]) if matches
44
59
  rescue ArgumentError => e
45
60
  raise e unless e.to_s.start_with?("invalid byte sequence in UTF-8")
46
61
  end
62
+
63
+ private
64
+
65
+ def strip_markup(string)
66
+ STRIP_MARKUP_FILTERS.reduce(string) do |memo, method|
67
+ filters.public_send(method, memo)
68
+ end.gsub(EXTRA_MARKUP_REGEX, "")
69
+ end
70
+
71
+ def filters
72
+ @filters ||= JekyllTitlesFromHeadings::Filters.new(site)
73
+ end
47
74
  end
48
75
  end
@@ -1,3 +1,3 @@
1
1
  module JekyllTitlesFromHeadings
2
- VERSION = "0.1.5".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-titles-from-headings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Balter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-28 00:00:00.000000000 Z
11
+ date: 2017-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -62,6 +62,8 @@ files:
62
62
  - LICENSE.md
63
63
  - README.md
64
64
  - lib/jekyll-titles-from-headings.rb
65
+ - lib/jekyll-titles-from-headings/context.rb
66
+ - lib/jekyll-titles-from-headings/filters.rb
65
67
  - lib/jekyll-titles-from-headings/generator.rb
66
68
  - lib/jekyll-titles-from-headings/version.rb
67
69
  homepage: https://github.com/benbalter/jekyll-titles-from-headings