coradoc-markdown 1.0.13 → 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/coradoc/markdown/behaviors/admonition.rb +21 -0
- data/lib/coradoc/markdown/behaviors/attribute_list.rb +81 -0
- data/lib/coradoc/markdown/behaviors/document.rb +34 -0
- data/lib/coradoc/markdown/behaviors/extension.rb +69 -0
- data/lib/coradoc/markdown/behaviors/heading.rb +33 -0
- data/lib/coradoc/markdown/behaviors/math.rb +32 -0
- data/lib/coradoc/markdown/behaviors/text.rb +14 -0
- data/lib/coradoc/markdown/behaviors.rb +18 -0
- data/lib/coradoc/markdown/lint/duplicate_headings.rb +30 -0
- data/lib/coradoc/markdown/lint/heading_levels.rb +27 -0
- data/lib/coradoc/markdown/lint.rb +17 -0
- data/lib/coradoc/markdown/model/admonition.rb +1 -8
- data/lib/coradoc/markdown/model/attribute_list.rb +1 -63
- data/lib/coradoc/markdown/model/document.rb +1 -16
- data/lib/coradoc/markdown/model/extension.rb +1 -61
- data/lib/coradoc/markdown/model/hard_line_break.rb +0 -3
- data/lib/coradoc/markdown/model/heading.rb +1 -22
- data/lib/coradoc/markdown/model/math.rb +1 -16
- data/lib/coradoc/markdown/model/text.rb +1 -3
- data/lib/coradoc/markdown/parser/ast_processor.rb +8 -10
- data/lib/coradoc/markdown/parser/attribute_lists.rb +48 -0
- data/lib/coradoc/markdown/parser/block_parser.rb +12 -45
- data/lib/coradoc/markdown/parser/inline_parser.rb +41 -19
- data/lib/coradoc/markdown/parser/parsanol_atoms.rb +15 -114
- data/lib/coradoc/markdown/schema/definition.rb +81 -0
- data/lib/coradoc/markdown/schema/loader.rb +68 -0
- data/lib/coradoc/markdown/schema/parity.rb +101 -0
- data/lib/coradoc/markdown/schema.rb +44 -0
- data/lib/coradoc/markdown/serializer/registrations.rb +2 -0
- data/lib/coradoc/markdown/serializer/serializers/text.rb +19 -0
- data/lib/coradoc/markdown/transform/from_core_model.rb +18 -0
- data/lib/coradoc/markdown/transform/to_core_model.rb +14 -5
- data/lib/coradoc/markdown/transformer.rb +73 -2
- data/lib/coradoc/markdown/version.rb +1 -1
- data/lib/coradoc/markdown.rb +12 -0
- data/schemas/blocks.yml +73 -0
- data/schemas/document.yml +55 -0
- data/schemas/inline.yml +57 -0
- data/schemas/lists.yml +35 -0
- data/schemas/tables.yml +11 -0
- metadata +27 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d0a7fd1a87349725905a318a44d33ec43156872195415b01183974ea70862e04
|
|
4
|
+
data.tar.gz: bb2f9298ab83f661e321676cb44d628d708bd4ef75d5a0219921a1020af93260
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b26bb83310278e00937a39ad8a1c7bcf6926a186a378e2bf1e379ff5f411fe3f5aaf10c0fc032a1e81502d5bd134b7e2995bd358241887dfb808507519b41ebb
|
|
7
|
+
data.tar.gz: 8f2b5fbfb9bef49eda15e7f3ac8dd7c09f4f992c556c0bed9a08f4902303fdfbd69261d41ea97df6cb760c55100d4c8c58ebee0526160e1d949e47d69bd29de5
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coradoc
|
|
4
|
+
module Markdown
|
|
5
|
+
module Behaviors
|
|
6
|
+
# Hand-written behavior for Admonition. Prepended (not included) so
|
|
7
|
+
# that the setter can call `super` into the attribute setter that
|
|
8
|
+
# lutaml-model defines on the class itself.
|
|
9
|
+
module Admonition
|
|
10
|
+
ALLOWED_TYPES = %w[note tip warning important caution].freeze
|
|
11
|
+
|
|
12
|
+
# Normalize type on assignment. Callers can pass "NOTE", "Note",
|
|
13
|
+
# or "note" — storage is always lowercase so downstream comparison
|
|
14
|
+
# and CSS class generation don't need to repeat the normalization.
|
|
15
|
+
def admonition_type=(value)
|
|
16
|
+
super(value.to_s.downcase)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coradoc
|
|
4
|
+
module Markdown
|
|
5
|
+
module Behaviors
|
|
6
|
+
# Hand-written behavior for AttributeList (IAL/ALD parsing, merging).
|
|
7
|
+
module AttributeList
|
|
8
|
+
def self.prepended(base)
|
|
9
|
+
base.extend(ClassMethods)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Parsing entry points.
|
|
13
|
+
module ClassMethods
|
|
14
|
+
# Parse an IAL string into an AttributeList
|
|
15
|
+
# @param str [String] The IAL string (e.g., '{:.class #id key="val"}')
|
|
16
|
+
# @return [AttributeList] Parsed attribute list
|
|
17
|
+
def parse(str)
|
|
18
|
+
return nil if str.nil? || str.empty?
|
|
19
|
+
|
|
20
|
+
# Remove surrounding braces
|
|
21
|
+
content = str.strip.gsub(/\A\{?:?|\}?\z/, '')
|
|
22
|
+
|
|
23
|
+
attr_list = new
|
|
24
|
+
|
|
25
|
+
# Check for ALD (has a name before colon)
|
|
26
|
+
if content =~ /\A(\w+):\s*/
|
|
27
|
+
attr_list.name = ::Regexp.last_match(1)
|
|
28
|
+
content = ::Regexp.last_match.post_match
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Parse the content
|
|
32
|
+
parse_attributes(content, attr_list)
|
|
33
|
+
|
|
34
|
+
attr_list
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def parse_attributes(content, attr_list)
|
|
38
|
+
# Use shared IalParser for consistent parsing
|
|
39
|
+
ParserUtil::IalParser.tokenize(content).each do |token|
|
|
40
|
+
case token[:type]
|
|
41
|
+
when :class
|
|
42
|
+
attr_list.classes << token[:value]
|
|
43
|
+
when :id
|
|
44
|
+
attr_list.id = token[:value]
|
|
45
|
+
when :attribute
|
|
46
|
+
attr_list.attributes << NamedValue.new(name: token[:key], value: token[:value])
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Merge another AttributeList into this one
|
|
53
|
+
# @param other [AttributeList] The other attribute list to merge
|
|
54
|
+
# @return [AttributeList] self for chaining
|
|
55
|
+
def merge!(other)
|
|
56
|
+
return self unless other
|
|
57
|
+
|
|
58
|
+
self.id = other.id if other.id
|
|
59
|
+
self.classes = (classes + other.classes).uniq
|
|
60
|
+
merged = {}
|
|
61
|
+
(attributes + other.attributes).each { |nv| merged[nv.name] = nv }
|
|
62
|
+
self.attributes = merged.values
|
|
63
|
+
self
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Create a merged copy
|
|
67
|
+
# @param other [AttributeList] The other attribute list to merge
|
|
68
|
+
# @return [AttributeList] New merged attribute list
|
|
69
|
+
def merge(other)
|
|
70
|
+
dup.merge!(other)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Check if this has any attributes
|
|
74
|
+
# @return [Boolean]
|
|
75
|
+
def empty?
|
|
76
|
+
id.nil? && classes.empty? && attributes.empty?
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coradoc
|
|
4
|
+
module Markdown
|
|
5
|
+
module Behaviors
|
|
6
|
+
# Hand-written behavior for Document: index access and construction.
|
|
7
|
+
module Document
|
|
8
|
+
def self.prepended(base)
|
|
9
|
+
base.extend(ClassMethods)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Constructors for Document.
|
|
13
|
+
module ClassMethods
|
|
14
|
+
# Create a document from an array of blocks
|
|
15
|
+
def from_ast(elements)
|
|
16
|
+
new(blocks: elements)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# @param [Integer] index The index of the block to retrieve
|
|
21
|
+
# @return [Coradoc::Markdown::Base] The block at the specified index
|
|
22
|
+
def [](index)
|
|
23
|
+
blocks[index]
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# @param [Integer] index The index of the block to set
|
|
27
|
+
# @param [Coradoc::Markdown::Base] value The block to set
|
|
28
|
+
def []=(index, value)
|
|
29
|
+
blocks[index] = value
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coradoc
|
|
4
|
+
module Markdown
|
|
5
|
+
module Behaviors
|
|
6
|
+
# Hand-written behavior for the kramdown Extension model.
|
|
7
|
+
module Extension
|
|
8
|
+
# Known extension types
|
|
9
|
+
TYPES = {
|
|
10
|
+
toc: :toc,
|
|
11
|
+
options: :options,
|
|
12
|
+
comment: :comment,
|
|
13
|
+
nomarkdown: :nomarkdown,
|
|
14
|
+
ignore: :ignore,
|
|
15
|
+
if: :conditional,
|
|
16
|
+
endif: :conditional
|
|
17
|
+
}.freeze
|
|
18
|
+
|
|
19
|
+
def self.prepended(base)
|
|
20
|
+
base.extend(ClassMethods)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Factory helpers for common extensions.
|
|
24
|
+
module ClassMethods
|
|
25
|
+
# @param options [Hash] TOC options (levels, etc.)
|
|
26
|
+
def toc(options = {})
|
|
27
|
+
new(name: :toc, options: hash_to_named(options))
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# @param options [Hash] Parser options
|
|
31
|
+
def options(options = {})
|
|
32
|
+
new(name: :options, options: hash_to_named(options))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @param content [String] Comment content
|
|
36
|
+
def comment(content = '')
|
|
37
|
+
new(name: :comment, content: content)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# @param content [String] Raw content to pass through
|
|
41
|
+
def nomarkdown(content)
|
|
42
|
+
new(name: :nomarkdown, content: content)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def hash_to_named(hash)
|
|
48
|
+
return [] if hash.nil? || hash.empty?
|
|
49
|
+
|
|
50
|
+
hash.map { |k, v| NamedValue.new(name: k.to_s, value: v.to_s) }
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Check if this is a specific extension type
|
|
55
|
+
# @param type [Symbol] The extension type
|
|
56
|
+
# @return [Boolean]
|
|
57
|
+
def type?(type)
|
|
58
|
+
name.to_sym == type
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Check if this is a self-closing extension
|
|
62
|
+
# @return [Boolean]
|
|
63
|
+
def self_closing?
|
|
64
|
+
content.nil? || content.empty?
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coradoc
|
|
4
|
+
module Markdown
|
|
5
|
+
module Behaviors
|
|
6
|
+
# Hand-written behavior for Heading, shared by the hand-written and
|
|
7
|
+
# the schema-generated class.
|
|
8
|
+
module Heading
|
|
9
|
+
# Generate an auto ID from the heading text
|
|
10
|
+
#
|
|
11
|
+
# @return [String] A slugified version of the text suitable for use as an ID
|
|
12
|
+
# @example
|
|
13
|
+
# Heading.new(text: "Hello World!").auto_id #=> "hello-world"
|
|
14
|
+
def auto_id
|
|
15
|
+
return '' if text.nil? || text.empty?
|
|
16
|
+
|
|
17
|
+
slug = text.to_s
|
|
18
|
+
.downcase
|
|
19
|
+
.gsub(/[^a-z0-9]+/, '-')
|
|
20
|
+
.gsub(/^-+|-+$/, '')
|
|
21
|
+
slug.empty? ? 'section' : slug
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Get the ID for this heading (uses explicit id if set, otherwise auto_id)
|
|
25
|
+
#
|
|
26
|
+
# @return [String] The ID to use for this heading
|
|
27
|
+
def heading_id
|
|
28
|
+
id || auto_id
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coradoc
|
|
4
|
+
module Markdown
|
|
5
|
+
module Behaviors
|
|
6
|
+
# Hand-written behavior for Math (block or inline).
|
|
7
|
+
module Math
|
|
8
|
+
def self.prepended(base)
|
|
9
|
+
base.extend(ClassMethods)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Constructors for the two math flavors.
|
|
13
|
+
module ClassMethods
|
|
14
|
+
# @param content [String] The math content
|
|
15
|
+
# @return [Coradoc::Markdown::Math]
|
|
16
|
+
def inline(content)
|
|
17
|
+
new(content: content, inline: true)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def block(content)
|
|
21
|
+
new(content: content, inline: false)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @return [Boolean]
|
|
26
|
+
def inline?
|
|
27
|
+
inline == true
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coradoc
|
|
4
|
+
module Markdown
|
|
5
|
+
# Hand-written behavior modules, one per model that needs methods beyond
|
|
6
|
+
# its attributes. Both the hand-written models and the schema-generated
|
|
7
|
+
# classes prepend the same module, so behavior has a single source.
|
|
8
|
+
module Behaviors
|
|
9
|
+
autoload :AttributeList, 'coradoc/markdown/behaviors/attribute_list'
|
|
10
|
+
autoload :Admonition, 'coradoc/markdown/behaviors/admonition'
|
|
11
|
+
autoload :Document, 'coradoc/markdown/behaviors/document'
|
|
12
|
+
autoload :Extension, 'coradoc/markdown/behaviors/extension'
|
|
13
|
+
autoload :Heading, 'coradoc/markdown/behaviors/heading'
|
|
14
|
+
autoload :Math, 'coradoc/markdown/behaviors/math'
|
|
15
|
+
autoload :Text, 'coradoc/markdown/behaviors/text'
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coradoc
|
|
4
|
+
module Markdown
|
|
5
|
+
module Lint
|
|
6
|
+
# MD024 — headings at the same level must have unique text.
|
|
7
|
+
# Duplicate headings produce ambiguous outlines.
|
|
8
|
+
class DuplicateHeadings < Coradoc::Lint::Rule
|
|
9
|
+
rule_id 'MD024'
|
|
10
|
+
applies_to :markdown
|
|
11
|
+
|
|
12
|
+
def check(document, path)
|
|
13
|
+
seen = Hash.new(0)
|
|
14
|
+
Array(document.blocks).each do |block|
|
|
15
|
+
next unless block.is_a?(Coradoc::Markdown::Heading)
|
|
16
|
+
|
|
17
|
+
seen[[block.level, block.text]] += 1
|
|
18
|
+
end
|
|
19
|
+
seen.select { |_key, count| count > 1 }
|
|
20
|
+
.map do |(level, text), count|
|
|
21
|
+
build_violation(
|
|
22
|
+
path: path,
|
|
23
|
+
message: "duplicate heading (level #{level}): #{text.inspect} used #{count} times"
|
|
24
|
+
)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coradoc
|
|
4
|
+
module Markdown
|
|
5
|
+
module Lint
|
|
6
|
+
# MD001 — heading levels must not skip: a heading may be at most
|
|
7
|
+
# one level deeper than the previous heading (markdownlint MD001).
|
|
8
|
+
# Headings are top-level blocks in the Markdown model.
|
|
9
|
+
class HeadingLevels < Coradoc::Lint::Rule
|
|
10
|
+
rule_id 'MD001'
|
|
11
|
+
applies_to :markdown
|
|
12
|
+
|
|
13
|
+
def check(document, path)
|
|
14
|
+
headings = Array(document.blocks).filter_map do |block|
|
|
15
|
+
[block.level, block.text] if block.is_a?(Coradoc::Markdown::Heading)
|
|
16
|
+
end
|
|
17
|
+
headings.each_cons(2).filter_map do |(prev, _), (level, text)|
|
|
18
|
+
next unless level > prev + 1
|
|
19
|
+
|
|
20
|
+
build_violation(path: path,
|
|
21
|
+
message: "heading level skips from #{prev} to #{level}: #{text}")
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coradoc
|
|
4
|
+
module Markdown
|
|
5
|
+
# Markdown lint rules (#165 follow-up): model-driven checks over
|
|
6
|
+
# the Markdown model tree. Rules self-register into
|
|
7
|
+
# Coradoc::Lint.registry when this file is required.
|
|
8
|
+
module Lint
|
|
9
|
+
require_relative 'lint/heading_levels'
|
|
10
|
+
require_relative 'lint/duplicate_headings'
|
|
11
|
+
|
|
12
|
+
RULES = [HeadingLevels, DuplicateHeadings].freeze
|
|
13
|
+
|
|
14
|
+
RULES.each { |rule| Coradoc::Lint.registry.register(rule) }
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -18,8 +18,6 @@ module Coradoc
|
|
|
18
18
|
# Type is stored lowercase. Content is raw Markdown text (already
|
|
19
19
|
# serialized). Title is optional.
|
|
20
20
|
class Admonition < Base
|
|
21
|
-
ALLOWED_TYPES = %w[note tip warning important caution].freeze
|
|
22
|
-
|
|
23
21
|
attribute :admonition_type, :string
|
|
24
22
|
attribute :content, :string
|
|
25
23
|
attribute :title, :string
|
|
@@ -29,12 +27,7 @@ module Coradoc
|
|
|
29
27
|
# references, code spans, etc. When empty, fall back to `content`.
|
|
30
28
|
attribute :children, Coradoc::Markdown::Base, collection: true, default: []
|
|
31
29
|
|
|
32
|
-
|
|
33
|
-
# or "note" — storage is always lowercase so downstream comparison
|
|
34
|
-
# and CSS class generation don't need to repeat the normalization.
|
|
35
|
-
def admonition_type=(value)
|
|
36
|
-
super(value.to_s.downcase)
|
|
37
|
-
end
|
|
30
|
+
prepend Behaviors::Admonition
|
|
38
31
|
end
|
|
39
32
|
end
|
|
40
33
|
end
|
|
@@ -19,69 +19,7 @@ module Coradoc
|
|
|
19
19
|
attribute :attributes, NamedValue, collection: true, default: []
|
|
20
20
|
attribute :name, :string # For ALD - the reference name
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
# @param str [String] The IAL string (e.g., '{:.class #id key="val"}')
|
|
24
|
-
# @return [AttributeList] Parsed attribute list
|
|
25
|
-
def self.parse(str)
|
|
26
|
-
return nil if str.nil? || str.empty?
|
|
27
|
-
|
|
28
|
-
# Remove surrounding braces
|
|
29
|
-
content = str.strip.gsub(/\A\{?:?|\}?\z/, '')
|
|
30
|
-
|
|
31
|
-
attr_list = new
|
|
32
|
-
|
|
33
|
-
# Check for ALD (has a name before colon)
|
|
34
|
-
if content =~ /\A(\w+):\s*/
|
|
35
|
-
attr_list.name = ::Regexp.last_match(1)
|
|
36
|
-
content = ::Regexp.last_match.post_match
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
# Parse the content
|
|
40
|
-
parse_attributes(content, attr_list)
|
|
41
|
-
|
|
42
|
-
attr_list
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
# Merge another AttributeList into this one
|
|
46
|
-
# @param other [AttributeList] The other attribute list to merge
|
|
47
|
-
# @return [AttributeList] self for chaining
|
|
48
|
-
def merge!(other)
|
|
49
|
-
return self unless other
|
|
50
|
-
|
|
51
|
-
self.id = other.id if other.id
|
|
52
|
-
self.classes = (classes + other.classes).uniq
|
|
53
|
-
merged = {}
|
|
54
|
-
(attributes + other.attributes).each { |nv| merged[nv.name] = nv }
|
|
55
|
-
self.attributes = merged.values
|
|
56
|
-
self
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
# Create a merged copy
|
|
60
|
-
# @param other [AttributeList] The other attribute list to merge
|
|
61
|
-
# @return [AttributeList] New merged attribute list
|
|
62
|
-
def merge(other)
|
|
63
|
-
dup.merge!(other)
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
# Check if this has any attributes
|
|
67
|
-
# @return [Boolean]
|
|
68
|
-
def empty?
|
|
69
|
-
id.nil? && classes.empty? && attributes.empty?
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
def self.parse_attributes(content, attr_list)
|
|
73
|
-
# Use shared IalParser for consistent parsing
|
|
74
|
-
ParserUtil::IalParser.tokenize(content).each do |token|
|
|
75
|
-
case token[:type]
|
|
76
|
-
when :class
|
|
77
|
-
attr_list.classes << token[:value]
|
|
78
|
-
when :id
|
|
79
|
-
attr_list.id = token[:value]
|
|
80
|
-
when :attribute
|
|
81
|
-
attr_list.attributes << NamedValue.new(name: token[:key], value: token[:value])
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
end
|
|
22
|
+
prepend Behaviors::AttributeList
|
|
85
23
|
end
|
|
86
24
|
end
|
|
87
25
|
end
|
|
@@ -27,22 +27,7 @@ module Coradoc
|
|
|
27
27
|
# without dragging YAML semantics into the Markdown model (MECE).
|
|
28
28
|
attribute :frontmatter, :string
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
# @return [Coradoc::Markdown::Base] The block at the specified index
|
|
32
|
-
def [](index)
|
|
33
|
-
blocks[index]
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
# @param [Integer] index The index of the block to set
|
|
37
|
-
# @param [Coradoc::Markdown::Base] value The block to set
|
|
38
|
-
def []=(index, value)
|
|
39
|
-
blocks[index] = value
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
# Create a document from an array of blocks
|
|
43
|
-
def self.from_ast(elements)
|
|
44
|
-
new(blocks: elements)
|
|
45
|
-
end
|
|
30
|
+
prepend Behaviors::Document
|
|
46
31
|
end
|
|
47
32
|
end
|
|
48
33
|
end
|
|
@@ -17,67 +17,7 @@ module Coradoc
|
|
|
17
17
|
attribute :content, :string # For block extensions with content
|
|
18
18
|
attribute :body, :string # Alias for content
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
TYPES = {
|
|
22
|
-
toc: :toc,
|
|
23
|
-
options: :options,
|
|
24
|
-
comment: :comment,
|
|
25
|
-
nomarkdown: :nomarkdown,
|
|
26
|
-
ignore: :ignore,
|
|
27
|
-
if: :conditional,
|
|
28
|
-
endif: :conditional
|
|
29
|
-
}.freeze
|
|
30
|
-
|
|
31
|
-
# Create a TOC extension
|
|
32
|
-
# @param options [Hash] TOC options (levels, etc.)
|
|
33
|
-
# @return [Extension]
|
|
34
|
-
def self.toc(options = {})
|
|
35
|
-
new(name: :toc, options: hash_to_named(options))
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
# Create an options extension
|
|
39
|
-
# @param options [Hash] Parser options
|
|
40
|
-
# @return [Extension]
|
|
41
|
-
def self.options(options = {})
|
|
42
|
-
new(name: :options, options: hash_to_named(options))
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
# Create a comment extension
|
|
46
|
-
# @param content [String] Comment content
|
|
47
|
-
# @return [Extension]
|
|
48
|
-
def self.comment(content = '')
|
|
49
|
-
new(name: :comment, content: content)
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
# Create a nomarkdown extension (passthrough)
|
|
53
|
-
# @param content [String] Raw content to pass through
|
|
54
|
-
# @return [Extension]
|
|
55
|
-
def self.nomarkdown(content)
|
|
56
|
-
new(name: :nomarkdown, content: content)
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
# Check if this is a specific extension type
|
|
60
|
-
# @param type [Symbol] The extension type
|
|
61
|
-
# @return [Boolean]
|
|
62
|
-
def type?(type)
|
|
63
|
-
name.to_sym == type
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
# Check if this is a self-closing extension
|
|
67
|
-
# @return [Boolean]
|
|
68
|
-
def self_closing?
|
|
69
|
-
content.nil? || content.empty?
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
class << self
|
|
73
|
-
private
|
|
74
|
-
|
|
75
|
-
def hash_to_named(hash)
|
|
76
|
-
return [] if hash.nil? || hash.empty?
|
|
77
|
-
|
|
78
|
-
hash.map { |k, v| NamedValue.new(name: k.to_s, value: v.to_s) }
|
|
79
|
-
end
|
|
80
|
-
end
|
|
20
|
+
prepend Behaviors::Extension
|
|
81
21
|
end
|
|
82
22
|
end
|
|
83
23
|
end
|
|
@@ -11,28 +11,7 @@ module Coradoc
|
|
|
11
11
|
attribute :level, :integer, default: 1
|
|
12
12
|
attribute :text, :string
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
#
|
|
16
|
-
# @return [String] A slugified version of the text suitable for use as an ID
|
|
17
|
-
# @example
|
|
18
|
-
# Heading.new(text: "Hello World!").auto_id #=> "hello-world"
|
|
19
|
-
def auto_id
|
|
20
|
-
return '' if text.nil? || text.empty?
|
|
21
|
-
|
|
22
|
-
# Downcase, replace non-alphanumeric with hyphens, collapse multiple hyphens
|
|
23
|
-
slug = text.to_s
|
|
24
|
-
.downcase
|
|
25
|
-
.gsub(/[^a-z0-9]+/, '-')
|
|
26
|
-
.gsub(/^-+|-+$/, '')
|
|
27
|
-
slug.empty? ? 'section' : slug
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
# Get the ID for this heading (uses explicit id if set, otherwise auto_id)
|
|
31
|
-
#
|
|
32
|
-
# @return [String] The ID to use for this heading
|
|
33
|
-
def heading_id
|
|
34
|
-
id || auto_id
|
|
35
|
-
end
|
|
14
|
+
prepend Behaviors::Heading
|
|
36
15
|
end
|
|
37
16
|
end
|
|
38
17
|
end
|
|
@@ -15,22 +15,7 @@ module Coradoc
|
|
|
15
15
|
attribute :content, :string
|
|
16
16
|
attribute :inline, :boolean, default: false
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
# @return [Boolean]
|
|
20
|
-
def inline?
|
|
21
|
-
inline == true
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
# Create an inline math element
|
|
25
|
-
# @param content [String] The math content
|
|
26
|
-
# @return [Math]
|
|
27
|
-
def self.inline(content)
|
|
28
|
-
new(content: content, inline: true)
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def self.block(content)
|
|
32
|
-
new(content: content, inline: false)
|
|
33
|
-
end
|
|
18
|
+
prepend Behaviors::Math
|
|
34
19
|
end
|
|
35
20
|
end
|
|
36
21
|
end
|