summaryrefslogtreecommitdiff
path: root/scripts/lib
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-08-18 00:11:02 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2022-08-18 00:11:02 +0000
commitc95fc172145f1bdbc8d959b6cee31555fc545784 (patch)
tree5aa4940ddefb9bea164905d61916593cc265a05c /scripts/lib
parenteda321fc0b96e44e296341f6288dd7f1a27ba93a (diff)
downloadgitlab-ce-c95fc172145f1bdbc8d959b6cee31555fc545784.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'scripts/lib')
-rw-r--r--scripts/lib/glfm/parse_examples.rb25
-rw-r--r--scripts/lib/glfm/update_example_snapshots.rb30
2 files changed, 41 insertions, 14 deletions
diff --git a/scripts/lib/glfm/parse_examples.rb b/scripts/lib/glfm/parse_examples.rb
index 14634bcfb3e..a15a6ecc47b 100644
--- a/scripts/lib/glfm/parse_examples.rb
+++ b/scripts/lib/glfm/parse_examples.rb
@@ -26,7 +26,7 @@ module Glfm
EXAMPLE_BACKTICKS_LENGTH = 32
EXAMPLE_BACKTICKS_STRING = '`' * EXAMPLE_BACKTICKS_LENGTH
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize
def parse_examples(spec_txt_lines)
line_number = 0
start_line = 0
@@ -41,6 +41,7 @@ module Glfm
h1_regex = /\A# / # new logic compared to original Python code
h2_regex = /\A## / # new logic compared to original Python code
+ h3_regex = /\A### / # new logic compared to original Python code
header_regex = /\A#+ / # Added beginning of line anchor to original Python code
spec_txt_lines.each do |line|
@@ -102,12 +103,24 @@ module Glfm
# reset the headers array if we found a new H1
headers = [] if line =~ h1_regex
- # headers should be size 2 or less [<H1_headertext>, <H2_headertext>]
- # pop the last entry from the headers array if we are in an H2 and found a new H2
- headers.pop if headers.length == 2 && line =~ h2_regex
+ # headers should be size 3 or less [<H1_headertext>, <H2_headertext>, <H3_headertext>]
+
+ if headers.length == 1 && line =~ h3_regex
+ errmsg = "Error: The H3 '#{headertext}' may not be nested directly within the H1 '#{headers[0]}'. " \
+ " Add an H2 header before the H3 header."
+ raise errmsg
+ end
+
+ if (headers.length == 2 || headers.length == 3) && line =~ h2_regex
+ # drop the everything but first entry from the headers array if we are in an H2 and found a new H2
+ headers = [headers[0]]
+ elsif headers.length == 3 && line =~ h3_regex
+ # pop the last entry from the headers array if we are in an H3 and found a new H3
+ headers.pop
+ end
# push the new header text to the headers array
- headers << headertext if line =~ h1_regex || line =~ h2_regex
+ headers << headertext if line =~ h1_regex || line =~ h2_regex || line =~ h3_regex
else
# Else if we are in regular text...
@@ -119,7 +132,7 @@ module Glfm
# no-op - skips any other non-header regular text lines
end
end
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize
tests
end
diff --git a/scripts/lib/glfm/update_example_snapshots.rb b/scripts/lib/glfm/update_example_snapshots.rb
index d8d6cf3cdbc..30a16f30c68 100644
--- a/scripts/lib/glfm/update_example_snapshots.rb
+++ b/scripts/lib/glfm/update_example_snapshots.rb
@@ -60,8 +60,8 @@ module Glfm
def add_example_names(all_examples)
# NOTE: This method and the parse_examples method assume:
# 1. Section 2 is the first section which contains examples
- # 2. Examples are always nested exactly 2 levels deep in an H2
- # 3. There may exist H3 headings with no examples (e.g. "Motivation" in the GLFM spec.txt)
+ # 2. Examples are always nested in an H2 or an H3, never directly in an H1
+ # 3. There may exist headings with no examples (e.g. "Motivation" in the GLFM spec.txt)
# 4. The Appendix doesn't ever contain any examples, so it doesn't show up
# in the H1 header count. So, even though due to the concatenation it appears before the
# GitLab examples sections, it doesn't result in their header counts being off by +1.
@@ -70,35 +70,49 @@ module Glfm
# GFM `spec_test.py` script (but it's NOT in the original CommonMark `spec_test.py`).
# 6. If a section contains ONLY disabled examples, the section numbering will still be
# incremented to match the rendered HTML specification section numbering.
- # 7. Every H2 must contain at least one example, but it is allowed that they are all disabled.
+ # 7. Every H2 or H3 must contain at least one example, but it is allowed that they are
+ # all disabled.
h1_count = 1 # examples start in H1 section 2; section 1 is the overview with no examples.
h2_count = 0
+ h3_count = 0
previous_h1 = ''
previous_h2 = ''
- index_within_h2 = 0
+ previous_h3 = ''
+ index_within_current_heading = 0
all_examples.each do |example|
headers = example[:headers]
if headers[0] != previous_h1
h1_count += 1
h2_count = 0
+ h3_count = 0
previous_h1 = headers[0]
end
if headers[1] != previous_h2
h2_count += 1
+ h3_count = 0
previous_h2 = headers[1]
- index_within_h2 = 0
+ index_within_current_heading = 0
end
- index_within_h2 += 1
+ if headers[2] && headers[2] != previous_h3
+ h3_count += 1
+ previous_h3 = headers[2]
+ index_within_current_heading = 0
+ end
+
+ index_within_current_heading += 1
# convert headers array to lowercase string with underscores, and double underscores between headers
formatted_headers_text = headers.join('__').tr('-', '_').tr(' ', '_').downcase
- hierarchy_level = "#{h1_count.to_s.rjust(2, '0')}_#{h2_count.to_s.rjust(2, '0')}"
- position_within_section = index_within_h2.to_s.rjust(3, '0')
+ hierarchy_level =
+ "#{h1_count.to_s.rjust(2, '0')}_" \
+ "#{h2_count.to_s.rjust(2, '0')}_" \
+ "#{h3_count.to_s.rjust(2, '0')}"
+ position_within_section = index_within_current_heading.to_s.rjust(3, '0')
name = "#{hierarchy_level}__#{formatted_headers_text}__#{position_within_section}"
converted_name = name.tr('(', '').tr(')', '') # remove any parens from the name
example[:name] = converted_name