raindown 0.13.0 → 0.15.0

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.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/lib/elements.rb +12 -25
  3. data/lib/lexemes/node_lexeme.rb +37 -0
  4. data/lib/nodes/list_node.rb +22 -25
  5. data/lib/nodes/write_in_node.rb +95 -0
  6. data/lib/raindown/bare_directives.rb +1 -1
  7. data/lib/raindown/chart/chart.rb +1 -1
  8. data/lib/raindown/cli.rb +2 -0
  9. data/lib/raindown/context.rb +19 -0
  10. data/lib/raindown/directives.rb +1 -1
  11. data/lib/raindown/fragments.rb +6 -1
  12. data/lib/raindown/js/host.mjs +2 -2
  13. data/lib/raindown/js/js_nodes.rb +4 -4
  14. data/lib/raindown/list.rb +31 -0
  15. data/lib/raindown/markdown_nodes.rb +3 -2
  16. data/lib/raindown/node.rb +10 -2
  17. data/lib/raindown/node_blocks.rb +27 -21
  18. data/lib/raindown/node_injector.rb +1 -1
  19. data/lib/raindown/nodes.rb +60 -22
  20. data/lib/raindown/pages.rb +8 -4
  21. data/lib/raindown/pre_renderer.rb +13 -10
  22. data/lib/raindown/projects.rb +24 -4
  23. data/lib/raindown/rewriter.rb +1 -1
  24. data/lib/raindown/stat.rb +1 -1
  25. data/lib/raindown/table/markdown_table.rb +11 -3
  26. data/lib/raindown/table/table.rb +92 -12
  27. data/lib/raindown/table/table_formulas.rb +50 -0
  28. data/lib/raindown/table/written_table.rb +68 -0
  29. data/lib/raindown/toc.rb +2 -2
  30. data/lib/raindown/tui/navigator/commands.rb +7 -1
  31. data/lib/raindown/watcher/changes.rb +5 -3
  32. data/lib/raindown/watcher/supervisor.rb +1 -1
  33. data/lib/raindown/wiki_links.rb +1 -1
  34. data/lib/raindown.rb +9 -14
  35. data/lib/version.rb +1 -1
  36. metadata +11 -8
  37. data/lib/lexemes/list_lexeme.rb +0 -32
  38. data/lib/nodes/component_nodes.rb +0 -38
  39. data/lib/raindown/files.rb +0 -22
  40. /data/lib/raindown/{table_formatter.rb → table/table_formatter.rb} +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f795c35070ab8ae4f6b40e5a32595ab9ba6099b99f42aada8d0faafe2f56121d
4
- data.tar.gz: a01426b3aab916e21b0e68f66fddd7f6b55d7e9c34ceb9665118a60b3ceae4b5
3
+ metadata.gz: 3d201e22c68a6326752cd504b5cca6674384031244b3b91cbe9e8da2e1034d8b
4
+ data.tar.gz: 6b3cf1bbc8e03c353c0eb0bade6a7c1f9546c99fc2dfabc260963f50e7ff3063
5
5
  SHA512:
6
- metadata.gz: 052231532f48cc3a575388ebe95fa62b70bc3eba2121742142071c2ab256ced437de1f4c80f2e439599d579c2b5eea2d793b7cb05439601523bc72ff9c252dd6
7
- data.tar.gz: 98ff7b646f8e380817bbf69efc06a0bf67b4945a233349c9702d3140458bb27e387ecd010498c80dbfa4a9d2e64872b4f7af75bcd12b28b992e8b82ac66f56c9
6
+ metadata.gz: cb8dba488c6c4ca2ddd49f8cd699a3d660f9a12fb00e756e6f5cd65e433b2ac04d4d4124e078a41b39fbe2707632f5cc1a78acf97e033dfdbe5715c01e50985b
7
+ data.tar.gz: 4d1d7f48804fcdc7ee6ce0343d2f34fa0f797566dc222a645dae1d3f4b79cc9f7841c6acbbfb83c93c0dde681bf110139f9ed51b4fd7ef7c37181db1052137fe
data/lib/elements.rb CHANGED
@@ -6,28 +6,27 @@ module Raindown
6
6
  class Elements
7
7
  include Plugs
8
8
 
9
- # Antlers' prop and slot directives, rendered as components in the template pass.
10
- plug(:prop_component) do
9
+ # Write-in nodes, in <{ }>, like <{ Card }>, <{ :toc }> or <{ Card: }>...<{ :Card }>, rendered in the template pass.
10
+ plug(:write_in) do
11
11
  plug(:lexeme) do
12
- require 'lexemes/prop_lexeme'
13
- Antlers::PropLexeme
12
+ require_relative 'lexemes/node_lexeme'
13
+ NodeLexeme
14
14
  end
15
15
 
16
+ # A list: block loops over its files, so it comes before other nodes' blocks.
16
17
  plug(:node) do
17
- require_relative 'nodes/component_nodes'
18
- PropComponentNode
18
+ require_relative 'nodes/list_node'
19
+ ListNode
19
20
  end
20
- end
21
21
 
22
- plug(:slot_component) do
23
- plug(:lexeme) do
24
- require 'lexemes/slot_lexeme'
25
- Antlers::SlotLexeme
22
+ plug(:node) do
23
+ require_relative 'nodes/write_in_node'
24
+ WriteInNode
26
25
  end
27
26
 
28
27
  plug(:node) do
29
- require_relative 'nodes/component_nodes'
30
- SlotComponentNode
28
+ require_relative 'nodes/write_in_node'
29
+ SoloWriteInNode
31
30
  end
32
31
  end
33
32
 
@@ -42,17 +41,5 @@ module Raindown
42
41
  EndpointNode
43
42
  end
44
43
  end
45
-
46
- plug(:list) do
47
- plug(:lexeme) do
48
- require_relative 'lexemes/list_lexeme'
49
- ListLexeme
50
- end
51
-
52
- plug(:node) do
53
- require_relative 'nodes/list_node'
54
- ListNode
55
- end
56
- end
57
44
  end
58
45
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'antlers/api'
4
+ require_relative '../raindown/node_blocks'
5
+
6
+ module Raindown
7
+ # A write-in node, in Antlers' delimiters, read with its props as Raindown reads them, like
8
+ # <{ :list folder: 'notes' }>. It opens a block with <{ Name: }>, and <{ :Name }> ends one, or else it's self-closing,
9
+ # as is <{ Card title='Hi' }>. Template directives, like if: and for:, aren't nodes, so they're left to their lexemes.
10
+ module NodeLexeme
11
+ include Antlers::Lexeme
12
+
13
+ module_function
14
+
15
+ DIRECTIVE = /\A(:)?([A-Za-z]\w*(?:::[A-Z]\w*)*)(:)?(?:\s+(.*))?\z/m
16
+
17
+ # A name with a colon is a node, like <{ :toc }> or <{ :app_name }>, while {:app_name} is a translation. One without
18
+ # is a node when it's capitalized, so <{ title }> is still a variable. The lexer gives no name to a directive that
19
+ # starts with another lexeme's keyword, like <{ if: draft }> or <{ :list }>, which it leaves to that lexeme.
20
+ def match?(segment:, name: nil, **)
21
+ _, close, node_name, open = segment.to_s.match(DIRECTIVE).to_a
22
+ return false if name.nil? || node_name.nil? || (close && open)
23
+ return true if close || open
24
+
25
+ node_name.match?(/\A[A-Z]/)
26
+ end
27
+
28
+ # <{ :Name }> ends a block of the name, or else is self-closing, while <{ Name }> is always self-closing.
29
+ def lexeme(segment:, **)
30
+ _, close, name, open, args = segment.match(DIRECTIVE).to_a
31
+ return { node_def: name, args: args.to_s } if open
32
+ return { node_end: name, args: args.to_s } if close
33
+
34
+ { node_solo: name, args: args.to_s }
35
+ end
36
+ end
37
+ end
@@ -1,46 +1,43 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'antlers/api'
4
+ require_relative '../raindown/context'
5
+ require_relative '../raindown/node_blocks'
6
+ require_relative '../raindown/nodes'
7
+ require_relative 'write_in_node'
4
8
 
5
9
  module Raindown
10
+ # <{ list: page folder: 'notes' }> ... <{ :list }>, which renders its template for each file the list: node lists,
11
+ # with the file as page. Without a block, <{ :list folder: 'notes' }> renders the node's links, as a WriteInNode.
6
12
  class ListNode < Antlers::BranchNode
7
- include Antlers::Props
8
- include Antlers::Variables
13
+ DEF_KEY = :node_def
14
+ END_KEY = :node_end
9
15
 
10
- DEF_KEY = :list_def
11
- END_KEY = :list_end
16
+ def initialize(name:, args:)
17
+ super(name:)
12
18
 
13
- def initialize(value:, folder:, props: [], children: [])
14
- super(name: :toc, props:, children:)
15
-
16
- @value = value.delete("'").delete('"')
17
- @folder = folder.delete("'").delete('"')
19
+ @variable, @tags = args.split(/\s+/, 2)
18
20
  end
19
21
 
20
- def render(current_binding: nil, parent_binding: nil, slot_node: nil, metadata: {})
21
- output = ''
22
-
23
- list = metadata.list(folder: @folder)
24
-
25
- list.each do |value|
26
- # TODO: Parallelize by creating new bindings and ensuring children have any args they need via RenderEvent.
27
- current_binding.local_variable_set(@value, value)
28
-
29
- @children.each do |child|
30
- output += child.render(current_binding:, parent_binding:, slot_node:, metadata:) || ''
31
- end
32
- end
22
+ def render(current_binding: nil, context: Context.new, **)
23
+ tags = NodeBlocks.props(@tags.to_s, BoundVariables.new(current_binding))
24
+ Nodes.instance(Nodes.node_class('list'), tags).files(metadata: context.metadata).map do |file|
25
+ current_binding.local_variable_set(@variable, file)
26
+ super
27
+ end.join
28
+ end
33
29
 
34
- output
30
+ def end_name
31
+ 'list'
35
32
  end
36
33
 
37
34
  class << self
38
35
  def match?(segment:)
39
- segment[DEF_KEY]
36
+ segment[DEF_KEY] == 'list'
40
37
  end
41
38
 
42
39
  def build(segment:, **)
43
- new(value: segment[:list_def], folder: segment[:folder])
40
+ new(name: :list, args: segment[:args])
44
41
  end
45
42
  end
46
43
  end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'antlers/api'
4
+ require 'nodes/html_node'
5
+ require_relative '../raindown/context'
6
+ require_relative '../raindown/fragments'
7
+ require_relative '../raindown/node_blocks'
8
+ require_relative '../raindown/nodes'
9
+
10
+ module Raindown
11
+ # A write-in node, in <{ }>, which Antlers renders where it is in the template, so its props see loop variables, like
12
+ # <{ Card name=p.name }> in a for: loop. It only renders, with its render, never its rewrite, and the Markdown
13
+ # between its tags, <{ Card: }> to <{ :Card }>, is its slot, rendered to HTML in its scope first.
14
+ # One with a class template, like Raindeer's .rbx components, renders its template, with that HTML as its slot.
15
+ class WriteInNode < Antlers::BranchNode
16
+ DEF_KEY = :node_def
17
+ END_KEY = :node_end
18
+
19
+ def initialize(name:, args: '', **)
20
+ super(name:)
21
+
22
+ @args = args
23
+ end
24
+
25
+ def render(current_binding: nil, context: Context.new, **)
26
+ slot = Raindown.commonmark(super) unless @children.empty?
27
+ props, args = NodeBlocks.args(@args, BoundVariables.new(current_binding))
28
+ node = Nodes.instance(node_class, props, args || [])
29
+ return Fragments.add(template_html(node, props, slot.to_s, current_binding)) if Nodes.template?(node.class)
30
+
31
+ Nodes.rendered(node, context, slot: slot.to_s)
32
+ end
33
+
34
+ def end_name
35
+ @name
36
+ end
37
+
38
+ private
39
+
40
+ # A render-only component, like Card, isn't a node Nodes finds, as it has no rewrite, so it's any class of the name.
41
+ def node_class
42
+ klass = Nodes.node_class(@name)
43
+ return klass if klass
44
+ raise NameError, "No node is named #{@name}, for <{ :#{@name} }>" unless @name.match?(/\A[A-Z]/)
45
+
46
+ Object.const_get(@name)
47
+ end
48
+
49
+ # The node's class template, whose slot is the HTML in place of its children.
50
+ def template_html(node, props, slot, parent_binding)
51
+ children = @children
52
+ @children = [Antlers::HTMLNode.new(html: slot)]
53
+ node.render_template(parent_binding:, slot_node: self, props:)
54
+ ensure
55
+ @children = children
56
+ end
57
+
58
+ class << self
59
+ def match?(segment:)
60
+ segment[DEF_KEY]
61
+ end
62
+
63
+ def build(segment:, **)
64
+ new(name: segment[DEF_KEY], args: segment[:args])
65
+ end
66
+ end
67
+ end
68
+
69
+ # <{ Card }>, or <{ :Card }> when it doesn't end a block, which a lowercase name needs, like <{ :toc }>.
70
+ class SoloWriteInNode < WriteInNode
71
+ END_KEY = nil
72
+
73
+ def self.match?(segment:)
74
+ segment[:node_solo] || segment[:node_end]
75
+ end
76
+
77
+ def self.build(segment:, **)
78
+ new(name: segment[:node_solo] || segment[:node_end], args: segment[:args])
79
+ end
80
+ end
81
+
82
+ # The variables where Antlers renders a node, like a loop's, and then the front matter's, as its props read them.
83
+ class BoundVariables < BasicObject
84
+ def initialize(binding)
85
+ @binding = binding
86
+ end
87
+
88
+ # A name that can't be a local, like a-b, is only looked for in the front matter.
89
+ def send(name)
90
+ return @binding.local_variable_get(name) if name.match?(/\A[a-z_]\w*\z/) && @binding.local_variable_defined?(name)
91
+
92
+ @binding.receiver.send(name)
93
+ end
94
+ end
95
+ end
@@ -8,7 +8,7 @@ require_relative 'front_matter'
8
8
  module Raindown
9
9
  # Raindown files (.rd/.raindown) can write a content directive, which writes its output back, bare on its own line,
10
10
  # like endpoint: or toc:, which shows in bold in the HTML. Markdown files opt in with "raindown: true" in their front
11
- # matter, or with the --bare flag. Nodes like toc: and files: are handled by NodeBlocks, before the rest.
11
+ # matter, or with the --bare flag. Nodes like toc: and list: are handled by NodeBlocks, before the rest.
12
12
  # Template directives, like if: and for:, are always in Antlers' delimiters, so they're never bare.
13
13
  #
14
14
  # A bare schema: shows in bold, with its fields in a code block below it.
@@ -32,7 +32,7 @@ module Raindown
32
32
  end
33
33
 
34
34
  # The data is read every time, so the chart follows changes to its file.
35
- def replace(metadata: NoMetadata)
35
+ def rewrite(metadata: NoMetadata)
36
36
  points = points(metadata:)
37
37
  return '' if points.empty?
38
38
 
data/lib/raindown/cli.rb CHANGED
@@ -76,6 +76,8 @@ module Raindown
76
76
  end
77
77
 
78
78
  def self.run(argv)
79
+ # Markdown is UTF-8 whatever the locale, as an app opened from the Dock, like Obsidian, starts Raindown without one.
80
+ Encoding.default_external = Encoding::UTF_8
79
81
  return if command(argv)
80
82
 
81
83
  # Without a path, like "raindown", "raindown -x" or "raindown watch", it's the current directory.
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'front_matter'
4
+ require_relative 'variables'
5
+
6
+ module Raindown
7
+ # The document being rendered, its metadata, like a site's pages, and whether its directives can be bare: the keywords
8
+ # a node's rewrite and render can take. Antlers passes it down to Raindown's own Antlers nodes.
9
+ Context = Data.define(:document, :metadata, :bare) do
10
+ def initialize(document: '', metadata: NoMetadata, bare: false)
11
+ super
12
+ end
13
+
14
+ # The document's front matter, as variables, like {title}.
15
+ def variables
16
+ Variables.new(FrontMatter.parse(document).first)
17
+ end
18
+ end
19
+ end
@@ -19,7 +19,7 @@ module Raindown
19
19
 
20
20
  module_function
21
21
 
22
- # A directive's line without its delimiters, like "files: folder: 'notes'" for <!-- files: folder: 'notes' -->.
22
+ # A directive's line without its delimiters, like "list: folder: 'notes'" for <!-- list: folder: 'notes' -->.
23
23
  def text(line)
24
24
  line = line.strip
25
25
  [COMMENT, DELIMITERS].each do |open, close|
@@ -18,8 +18,13 @@ module Raindown
18
18
  end
19
19
 
20
20
  def add(html)
21
+ "<!-- raindown:fragment #{store(html)} -->"
22
+ end
23
+
24
+ # Keeps the HTML for this render, returning its index in the list.
25
+ def store(html)
21
26
  list << html
22
- "<!-- raindown:fragment #{list.length - 1} -->"
27
+ list.length - 1
23
28
  end
24
29
 
25
30
  # Fills each placeholder, again and again, since a fragment can hold others, like a component in a slot.
@@ -16,13 +16,13 @@ const classes = {}
16
16
  const nodes = {}
17
17
 
18
18
  const requests = {
19
- // Every exported class with replace is a node, named by its export, or by its class for a default export.
19
+ // Every exported class with rewrite is a node, named by its export, or by its class for a default export.
20
20
  async load({ folder }) {
21
21
  for (const file of readdirSync(folder).filter((file) => /\.m?js$/.test(file))) {
22
22
  const exports = await import(pathToFileURL(join(folder, file)).href)
23
23
 
24
24
  for (const [name, value] of Object.entries(exports)) {
25
- if (typeof value?.prototype?.replace === 'function') classes[name === 'default' ? value.name : name] = value
25
+ if (typeof value?.prototype?.rewrite === 'function') classes[name === 'default' ? value.name : name] = value
26
26
  }
27
27
  }
28
28
 
@@ -6,7 +6,7 @@ require 'open3'
6
6
  require_relative '../node'
7
7
 
8
8
  module Raindown
9
- # JavaScript Components, the .js and .mjs files in a project's .raindown folder, which export classes with replace and
9
+ # JavaScript Components, the .js and .mjs files in a project's .raindown folder, which export classes with rewrite and
10
10
  # optionally render, like Ruby nodes. They run in a NodeJS process of Raindown's own, started when a folder has them,
11
11
  # so only projects with JavaScript Components need NodeJS.
12
12
  module JSNodes
@@ -57,7 +57,7 @@ module Raindown
57
57
  end
58
58
  end
59
59
 
60
- # A JavaScript Component, made in the NodeJS process with its props, where its replace and render are sent.
60
+ # A JavaScript Component, made in the NodeJS process with its props, where its rewrite and render are sent.
61
61
  class JSNode < Node
62
62
  # A class for each JavaScript Component, which renders its own HTML when the component has render.
63
63
  def self.for(name:, render:)
@@ -72,8 +72,8 @@ module Raindown
72
72
  JSNodes.host.request(op: 'new', id: object_id, name: self.class.name, args:, props:)
73
73
  end
74
74
 
75
- def replace(**context)
76
- js_output(:replace, context)
75
+ def rewrite(**context)
76
+ js_output(:rewrite, context)
77
77
  end
78
78
 
79
79
  private
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'node'
4
+
5
+ module Raindown
6
+ # The node behind list:, the files the metadata has for its props, like list: folder: 'notes'. Bare or in comments it
7
+ # writes a link to each, and in <{ list: page folder: 'notes' }> ... <{ :list }> it renders its template for each,
8
+ # with the file as page, the name before its props, which is its argument. A project can change how files are listed
9
+ # with its own List node in .raindown, like "class List < Raindown::List".
10
+ class List < Node
11
+ # Order is the metadata to sort by, like order: :created_at, rather than a tag to list by.
12
+ def initialize(_name = nil, order: nil, **tags)
13
+ @order = order
14
+ @tags = tags
15
+ end
16
+
17
+ # Each file has its front matter, its path and its content. Sorted by an order, files without it come last, and ones
18
+ # with the same value stay in the order they're listed.
19
+ def files(metadata: NoMetadata)
20
+ files = metadata.list(**@tags)
21
+ return files unless @order
22
+
23
+ sorted, unsorted = files.partition { it[@order] }
24
+ sorted.sort_by.with_index { |file, index| [file[@order], index] } + unsorted
25
+ end
26
+
27
+ def rewrite(metadata: NoMetadata)
28
+ files(metadata:).map { |file| "- [#{file.title || file.path}](#{file.path})" }.join("\n")
29
+ end
30
+ end
31
+ end
@@ -57,10 +57,11 @@ module Raindown
57
57
 
58
58
  # Braces that the template pass hid, in its code or its props' values, are written back as braces.
59
59
  # The template pass loads here, as it loads nodes itself.
60
- def replace(metadata: NoMetadata, bare: false)
60
+ def rewrite(document: '', metadata: NoMetadata, bare: false)
61
61
  require_relative 'pre_renderer'
62
62
  defaults, template = FrontMatter.parse(File.read(self.class.path))
63
- markdown = PreRenderer.render(template, variables: Variables.new(defaults.merge(@props)), metadata:, bare:)
63
+ context = Context.new(document:, metadata:, bare:)
64
+ markdown = PreRenderer.render(template, variables: Variables.new(defaults.merge(@props)), context:)
64
65
  Variables.unwrap(markdown.sub(/\A(?:[ \t]*\n)+/, '').rstrip)
65
66
  end
66
67
  end
data/lib/raindown/node.rb CHANGED
@@ -1,7 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Raindown
4
- # The base class of Raindown's own nodes, like TOC and Files.
4
+ # The base class of Raindown's own nodes, like TOC and List.
5
5
  # A project's nodes are LowNodes, or subclasses of these.
6
- class Node; end
6
+ class Node
7
+ # Write-in, in <{ }>, a node renders its Markdown as HTML. One whose rewrite takes written:, what it wrote last
8
+ # time, can write edits back to its source, like Table to its file, so it needs its own render, which never does.
9
+ def render(**context)
10
+ raise Nodes::NoRender, "#{self.class.name} writes edits back, so it needs a render" if Nodes.takes?(self, :rewrite, :written)
11
+
12
+ Raindown.commonmark(Nodes.call(self, :rewrite, **context).to_s)
13
+ end
14
+ end
7
15
  end
@@ -16,9 +16,10 @@ module Raindown
16
16
  # - **1.2.0** Bare directives
17
17
  #
18
18
  # <!-- :Changelog -->
19
- # In Antlers' delimiters, <{ Changelog: }> to <{ :Changelog }>, or a self-closing <{ :Changelog }> or bare
20
- # :Changelog, it renders the node without writing it. toc: and files: are Raindown's own nodes. A name only starts
21
- # a block when it's a loaded node, so other text like "Note:" is left alone.
19
+ # A bare :Changelog is write-in: it only renders, with its render, never its rewrite. In Antlers' delimiters, like
20
+ # <{ Changelog }>, it's write-in too, and the template pass renders it, as a WriteInNode, so it's only found here
21
+ # for a document's outline. toc: and list: are Raindown's own nodes. A name only starts a block when it's a loaded
22
+ # node, so other text like "Note:" is left alone.
22
23
  module NodeBlocks
23
24
  class MissingEndTag < StandardError; end
24
25
  class BrokenText < StandardError; end
@@ -27,7 +28,8 @@ module Raindown
27
28
  OPEN = /\A( {0,3})<\{\s*(#{NAME}):(?:\s+(.*?))?\s*\}>[ \t]*$/
28
29
  COMMENT_OPEN = /\A( {0,3})<!--\s*(#{NAME}):(?:\s+(.*?))?\s*-->[ \t]*$/
29
30
  BARE_OPEN = /\A( {0,3})(#{NAME}):(?:[ \t]+(.*?))?[ \t]*$/
30
- SELF_CLOSING = /\A( {0,3})<\{\s*:(#{NAME})(?:\s+(.*?))?\s*\}>[ \t]*$/
31
+ # <{ Card }>, or <{ :Card }>, which a lowercase name like toc needs, as <{ toc }> is a variable.
32
+ SELF_CLOSING = /\A( {0,3})<\{\s*(?::|(?=[A-Z]))(#{NAME})(?:\s+(.*?))?\s*\}>[ \t]*$/
31
33
  BARE_SELF_CLOSING = /\A( {0,3}):(#{NAME})(?:[ \t]+(.*?))?[ \t]*$/
32
34
  # Unquoted text after name:, like YAML's, runs up to the next prop, so title: Hello world limit: 3 has two.
33
35
  UNQUOTED = /.+?(?=[ \t]+\w+(?:=|:[ \t])|[ \t]*\z)/
@@ -40,12 +42,15 @@ module Raindown
40
42
  module_function
41
43
 
42
44
  # Each block outside code and front matter as { open:, close:, last:, writes:, node: }, where close is nil for a
43
- # bare block, and writes is whether its output's written into the file. A self-closing node is a block of one line
44
- # with self_closing: true.
45
+ # bare block, and writes is whether its output's written into the file. A self-closing node is a block of one line.
45
46
  def find(lines, front_matter: {}, bare: false)
47
+ with_nodes(spans(lines, bare:), front_matter)
48
+ end
49
+
50
+ def with_nodes(spans, front_matter)
46
51
  variables = Variables.new(front_matter)
47
52
 
48
- spans(lines, bare:).map do |span|
53
+ spans.map do |span|
49
54
  node = Nodes.instance(span[:class], *args(span[:args], variables, antlers: span[:antlers]))
50
55
  span.except(:name, :class, :args, :antlers).merge(node:)
51
56
  end
@@ -71,28 +76,29 @@ module Raindown
71
76
  spans
72
77
  end
73
78
 
74
- # Swaps each block for its node's HTML, through a placeholder, or for its Markdown when it has no HTML of its own.
75
- # The directive and end tag become Directives markers, which also keep the node's output from joining
76
- # the Markdown around it, like two lists becoming one.
77
- def to_rendered(markdown, metadata: NoMetadata, bare: false)
78
- lines = markdown.lines
79
- find(lines, front_matter: FrontMatter.parse(markdown).first, bare:).reverse_each do |block|
80
- lines[block[:open]..block[:last]] = rendered_lines(lines, block, markdown:, metadata:, bare:)
79
+ # Swaps each bare block or block in comments for its node's Markdown, or its HTML when it has its own, or only renders,
80
+ # as a fragment or a placeholder. The directive and end tag become Directives markers, which also keep the node's
81
+ # output from joining the Markdown around it, like two lists becoming one. Ones in <{ }> are left to Antlers.
82
+ def to_rendered(context)
83
+ lines = context.document.dup.force_encoding('UTF-8').lines
84
+ spans = spans(lines, bare: context.bare).reject { it[:antlers] }
85
+ with_nodes(spans, FrontMatter.parse(context.document).first).reverse_each do |block|
86
+ lines[block[:open]..block[:last]] = rendered_lines(lines, block, context)
81
87
  end
82
88
  lines.join
83
89
  end
84
90
 
85
- def rendered_lines(lines, block, markdown:, metadata:, bare:)
86
- output = node_markdown(block[:node], markdown:, metadata:, bare:).chomp
91
+ def rendered_lines(lines, block, context)
92
+ output = node_markdown(block, context).chomp
87
93
  close = marker(lines[block[:close]]) if block[:close]
88
94
 
89
95
  [marker(lines[block[:open]]), *("#{output}\n".lines unless output.empty?), *close]
90
96
  end
91
97
 
92
- def node_markdown(node, markdown:, metadata:, bare:)
93
- return Nodes.placeholder(node) if Nodes.html?(node)
98
+ def node_markdown(block, context)
99
+ return Nodes.rendered(block[:node], context) if !block[:writes] || Nodes.html?(block[:node])
94
100
 
95
- Nodes.call(node, :replace, document: markdown, metadata:, bare:).to_s
101
+ Nodes.call(block[:node], :rewrite, **context.to_h).to_s
96
102
  end
97
103
 
98
104
  def marker(line)
@@ -109,7 +115,7 @@ module Raindown
109
115
  end_tag(lines, index, found[2], comment: pattern == :comment_open)
110
116
  .then { |close| { open: index, close:, last: close, writes: pattern == :comment_open, **span } }
111
117
  when :bare_open then { **bare_block(lines, index), writes: true, **span }
112
- else { open: index, close: nil, last: index, writes: false, self_closing: true, **span }
118
+ else { open: index, close: nil, last: index, writes: false, **span }
113
119
  end
114
120
  end
115
121
 
@@ -156,7 +162,7 @@ module Raindown
156
162
  end
157
163
 
158
164
  # A value, like 'text', 3, true, nil or :draft, or else a variable. In Antlers' delimiters a word is a variable, like
159
- # author.name, while bare or in a comment, which read like Markdown, it's text, like notes in files: folder: notes,
165
+ # author.name, while bare or in a comment, which read like Markdown, it's text, like notes in list: folder: notes,
160
166
  # and only one in braces is a variable, like {author.name}.
161
167
  def value(value, variables, antlers: true)
162
168
  Lowkey::ValueParser.parse(value) do
@@ -36,7 +36,7 @@ module Raindown
36
36
 
37
37
  def block_lines(lines, block)
38
38
  context = { document: @content, metadata: @metadata, bare: @bare, written: written_lines(lines, block) }
39
- markdown = Nodes.call(block[:node], :replace, **context).to_s.chomp
39
+ markdown = Nodes.call(block[:node], :rewrite, **context).to_s.chomp
40
40
  written = markdown.empty? ? [] : "#{markdown}\n".lines
41
41
  return [line(lines[block[:open]]), *bare(written, block)] unless block[:close]
42
42