govuk_markdown 1.0.0 → 2.0.0b1

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
  SHA256:
3
- metadata.gz: 69f3f17dd960d5db6cb264a91912850a9e294f3a75c1c936044a58c6b36b7c04
4
- data.tar.gz: 7d30fe596d4262f8f445c845a7985e9fa1d18740494ca12e0d644a17371e28f2
3
+ metadata.gz: cf2eeb05a74d3e6dac921b756a58192707d5c4edbb0d706b8d79dbf735dd3279
4
+ data.tar.gz: 319c0f2a286dd65829c3b08c93f6a6492748445b82c223b60e8120f4285855f7
5
5
  SHA512:
6
- metadata.gz: 0a2e32563c6b61b9d51b998a9fa57ed6d5ad5318f080efc4bda6ab27534d7a39f86686407989b5542be205f4e9d60bfd7b169cbceec25b0dd25c627ceb5a6a9b
7
- data.tar.gz: abd0235bf969a13863c0dc415c1adefd606c70dda8f857ffd1d39c9126ea9d5334498194bebafa6a7b73b9e627f014cc64f31881989baca2bf973793c73f5207
6
+ metadata.gz: 4ba28fd33999e3201ce352baabd21c5a9f3f9959133401cacf39902b8c97259e3dd7ff4aac71db4de31154546f06b61599705e74cab88555f9c357d4cf760345
7
+ data.tar.gz: 90e4c6e7edea3656159d6532452d4bef11f292a0a9682a661ffcb0ea4e31d88c62efc87b22db3135b54852c02bdb0aff223ddec8119934a5f346a8278aa9ef63
@@ -1,28 +1,30 @@
1
+ name: Publish gem
1
2
  on:
2
- - push
3
-
3
+ workflow_dispatch:
4
+ inputs:
5
+ tag:
6
+ description: Tag
7
+ required: true
4
8
  jobs:
5
9
  build:
10
+ name: Publish to RubyGems.org
6
11
  runs-on: ubuntu-latest
7
- if: ${{ github.ref == 'refs/heads/main' }}
8
12
  steps:
9
13
  - uses: actions/checkout@v2
10
- - uses: ruby/setup-ruby@v1
11
- - name: Install dependencies
12
- run: gem update --system
14
+ with:
15
+ ref: ${{ github.event.inputs.tag }}
13
16
 
14
- - env:
15
- GEM_HOST_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }}
16
- name: Push a new gem version to Rubygems
17
+ - name: Set up Ruby
18
+ uses: ruby/setup-ruby@v1
19
+ with:
20
+ ruby-version: '2.7.6'
21
+
22
+ - name: Install dependencies
17
23
  run: |
18
- CURRENT_VERSION=$(rake gem_version)
19
- PUBLISHED_GEM_VERSION=$(gem list --exact --remote govuk_markdown)
24
+ gem install bundler
25
+ bundle install --jobs 4 --retry 3
20
26
 
21
- if [ "${PUBLISHED_GEM_VERSION}" != "govuk_markdown (${CURRENT_VERSION})" ]; then
22
- gem build govuk_markdown.gemspec
23
- gem push "govuk_markdown-${CURRENT_VERSION}.gem"
24
- fi
25
- if ! git ls-remote --tags --exit-code origin v${CURRENT_VERSION}; then
26
- git tag v${CURRENT_VERSION}
27
- git push --tags
28
- fi
27
+ - name: Publish gem
28
+ uses: dawidd6/action-publish-gem@v1
29
+ with:
30
+ api_key: ${{ secrets.RUBYGEMS_API_KEY }}
@@ -1,22 +1,26 @@
1
1
  name: Ruby
2
-
3
2
  on:
4
3
  - pull_request
5
-
6
4
  jobs:
7
- test:
8
-
5
+ rspec:
6
+ strategy:
7
+ fail-fast: false
8
+ matrix:
9
+ ruby: ['2.7.6', '3.0.4', '3.1.2']
9
10
  runs-on: ubuntu-latest
10
-
11
11
  steps:
12
12
  - uses: actions/checkout@v2
13
+
13
14
  - name: Set up Ruby
14
15
  uses: ruby/setup-ruby@v1
15
16
  with:
16
- ruby-version: 2.7
17
+ ruby-version: ${{ matrix.ruby }}
18
+
17
19
  - name: Install dependencies
18
20
  run: bundle install --jobs 4 --retry 3
21
+
19
22
  - name: Run linter
20
23
  run: bundle exec rubocop .
24
+
21
25
  - name: Run tests
22
26
  run: bundle exec rake
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- govuk_markdown (1.0.0)
4
+ govuk_markdown (2.0.0b1)
5
5
  activesupport
6
6
  redcarpet
7
7
 
@@ -15,15 +15,20 @@ GEM
15
15
  tzinfo (~> 2.0)
16
16
  zeitwerk (~> 2.3)
17
17
  ast (2.4.2)
18
+ coderay (1.1.3)
18
19
  concurrent-ruby (1.1.9)
19
20
  diff-lcs (1.4.4)
20
21
  i18n (1.8.10)
21
22
  concurrent-ruby (~> 1.0)
23
+ method_source (1.0.0)
22
24
  minitest (5.14.4)
23
25
  parallel (1.20.1)
24
26
  parser (3.0.2.0)
25
27
  ast (~> 2.4.1)
26
- rack (2.2.3)
28
+ pry (0.14.1)
29
+ coderay (~> 1.1)
30
+ method_source (~> 1.0)
31
+ rack (3.0.0)
27
32
  rainbow (3.0.0)
28
33
  rake (13.0.6)
29
34
  redcarpet (3.5.1)
@@ -79,6 +84,7 @@ PLATFORMS
79
84
 
80
85
  DEPENDENCIES
81
86
  govuk_markdown!
87
+ pry
82
88
  rake (~> 13.0)
83
89
  rspec
84
90
  rubocop-govuk
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.add_dependency "activesupport"
28
28
  spec.add_dependency "redcarpet"
29
29
 
30
+ spec.add_development_dependency "pry"
30
31
  spec.add_development_dependency "rake", "~> 13.0"
31
32
  spec.add_development_dependency "rspec"
32
33
  spec.add_development_dependency "rubocop-govuk"
@@ -0,0 +1,64 @@
1
+ module GovukMarkdown
2
+ class Preprocessor
3
+ attr_reader :output
4
+
5
+ def initialize(document)
6
+ @output = document
7
+ end
8
+
9
+ def inject_inset_text
10
+ output.gsub!(build_regexp("inset-text")) do
11
+ <<~HTML
12
+ <div class="govuk-inset-text">
13
+ #{Regexp.last_match(1)}
14
+ </div>
15
+ HTML
16
+ end
17
+ self
18
+ end
19
+
20
+ def inject_details
21
+ output.gsub!(build_regexp("details")) do
22
+ summary, details = *construct_details_from(Regexp.last_match(1))
23
+
24
+ <<~HTML
25
+ <details class="govuk-details" data-module="govuk-details">
26
+ <summary class="govuk-details__summary">
27
+ <span class="govuk-details__summary-text">
28
+ #{summary}
29
+ </span>
30
+ </summary>
31
+ <div class="govuk-details__text">
32
+ #{details}
33
+ </div>
34
+ </details>
35
+ HTML
36
+ end
37
+ self
38
+ end
39
+
40
+ private
41
+
42
+ def build_regexp(tag_name, pre_tag: "{", post_tag: "}", closing: "/")
43
+ start_tag = pre_tag + tag_name + post_tag
44
+ end_tag = pre_tag + closing + tag_name + post_tag
45
+ pattern = [Regexp.quote(start_tag), "(.*?)", Regexp.quote(end_tag)].join
46
+
47
+ Regexp.compile(pattern, Regexp::EXTENDED | Regexp::MULTILINE)
48
+ end
49
+
50
+ def construct_details_from(match_string, partition_characters: %w[? .])
51
+ summary_text, match, details = match_string.partition(Regexp.union(*partition_characters))
52
+
53
+ summary = [summary_text, format_punctuation(match)].compact.join
54
+
55
+ [summary, details].compact.map(&:strip)
56
+ end
57
+
58
+ def format_punctuation(match)
59
+ return if match.include?(".")
60
+
61
+ match
62
+ end
63
+ end
64
+ end
@@ -1,5 +1,10 @@
1
1
  module GovukMarkdown
2
2
  class Renderer < ::Redcarpet::Render::HTML
3
+ def initialize(govuk_options, options = {})
4
+ @headings_start_with = govuk_options[:headings_start_with]
5
+ super options
6
+ end
7
+
3
8
  def table(header, body)
4
9
  <<~HTML
5
10
  <table class='govuk-table'>
@@ -30,16 +35,17 @@ module GovukMarkdown
30
35
  end
31
36
 
32
37
  def header(text, header_level)
33
- heading_size = case header_level
34
- when 1 then "xl"
35
- when 2 then "l"
36
- when 3 then "m"
37
- else "s" end
38
+ valid_header_sizes = %w[xl l m s].freeze
38
39
 
39
- id_attribute = @options[:with_toc_data] ? " id=\"#{text.parameterize}\"" : ""
40
+ start_size = valid_header_sizes.include?(@headings_start_with) ? @headings_start_with : "xl"
41
+
42
+ start_size_index = valid_header_sizes.find_index(start_size)
40
43
 
44
+ header_size = valid_header_sizes[start_size_index + header_level - 1] || "s"
45
+
46
+ id_attribute = @options[:with_toc_data] ? " id=\"#{text.parameterize}\"" : ""
41
47
  <<~HTML
42
- <h#{header_level}#{id_attribute} class="govuk-heading-#{heading_size}">#{text}</h#{header_level}>
48
+ <h#{header_level}#{id_attribute} class="govuk-heading-#{header_size}">#{text}</h#{header_level}>
43
49
  HTML
44
50
  end
45
51
 
@@ -73,5 +79,13 @@ module GovukMarkdown
73
79
  <hr class="govuk-section-break govuk-section-break--xl govuk-section-break--visible">
74
80
  HTML
75
81
  end
82
+
83
+ def preprocess(document)
84
+ Preprocessor
85
+ .new(document)
86
+ .inject_inset_text
87
+ .inject_details
88
+ .output
89
+ end
76
90
  end
77
91
  end
@@ -1,3 +1,3 @@
1
1
  module GovukMarkdown
2
- VERSION = "1.0.0".freeze
2
+ VERSION = "2.0.0b1".freeze
3
3
  end
@@ -2,11 +2,12 @@ require "active_support/all"
2
2
  require "redcarpet"
3
3
 
4
4
  require_relative "./govuk_markdown/version"
5
+ require_relative "./govuk_markdown/preprocessor"
5
6
  require_relative "./govuk_markdown/renderer"
6
7
 
7
8
  module GovukMarkdown
8
- def self.render(markdown)
9
- renderer = GovukMarkdown::Renderer.new(with_toc_data: true, link_attributes: { class: "govuk-link" })
9
+ def self.render(markdown, govuk_options = {})
10
+ renderer = GovukMarkdown::Renderer.new(govuk_options, { with_toc_data: true, link_attributes: { class: "govuk-link" } })
10
11
  Redcarpet::Markdown.new(renderer, tables: true, no_intra_emphasis: true).render(markdown).strip
11
12
  end
12
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_markdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0b1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tijmen Brommet
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-20 00:00:00.000000000 Z
11
+ date: 2023-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -107,6 +121,7 @@ files:
107
121
  - example/govuk-frontend-3.9.1.min.css
108
122
  - govuk_markdown.gemspec
109
123
  - lib/govuk_markdown.rb
124
+ - lib/govuk_markdown/preprocessor.rb
110
125
  - lib/govuk_markdown/renderer.rb
111
126
  - lib/govuk_markdown/version.rb
112
127
  homepage: https://github.com/DFE-Digital/govuk_markdown
@@ -126,11 +141,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
126
141
  version: 2.7.0
127
142
  required_rubygems_version: !ruby/object:Gem::Requirement
128
143
  requirements:
129
- - - ">="
144
+ - - ">"
130
145
  - !ruby/object:Gem::Version
131
- version: '0'
146
+ version: 1.3.1
132
147
  requirements: []
133
- rubygems_version: 3.2.24
148
+ rubygems_version: 3.1.4
134
149
  signing_key:
135
150
  specification_version: 4
136
151
  summary: Convert Markdown into GOV.UK styled HTML