raindown 0.18.0 → 0.21.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 (67) hide show
  1. checksums.yaml +4 -4
  2. data/lib/raindown/area.rb +88 -0
  3. data/lib/raindown/chart/chart.rb +5 -2
  4. data/lib/raindown/chart/drawing.rb +0 -2
  5. data/lib/raindown/chart/line.rb +1 -1
  6. data/lib/raindown/chart/points.rb +2 -1
  7. data/lib/raindown/cli.rb +2 -2
  8. data/lib/raindown/code.rb +37 -5
  9. data/lib/raindown/config.rb +11 -0
  10. data/lib/raindown/document.rb +8 -6
  11. data/lib/raindown/editing.rb +8 -4
  12. data/lib/raindown/endpoint/curl.rb +19 -14
  13. data/lib/raindown/endpoint/curl_request.rb +90 -0
  14. data/lib/raindown/endpoint/curl_response.rb +6 -3
  15. data/lib/raindown/grid.rb +154 -0
  16. data/lib/raindown/grid_directives.rb +43 -0
  17. data/lib/raindown/grid_wrapping.rb +46 -0
  18. data/lib/raindown/http_json.rb +45 -0
  19. data/lib/raindown/image.rb +44 -0
  20. data/lib/raindown/js/js_nodes.rb +85 -33
  21. data/lib/raindown/js/quickjs_host.rb +63 -0
  22. data/lib/raindown/js/quickjs_runtime.js +61 -0
  23. data/lib/raindown/js/services.js +31 -0
  24. data/lib/raindown/markdown_nodes.rb +2 -0
  25. data/lib/raindown/node_blocks.rb +22 -9
  26. data/lib/raindown/node_injector.rb +56 -15
  27. data/lib/raindown/nodes.rb +52 -15
  28. data/lib/raindown/outdent.rb +100 -0
  29. data/lib/raindown/pages/listings.rb +71 -0
  30. data/lib/raindown/{pages.rb → pages/pages.rb} +21 -34
  31. data/lib/raindown/problem.rb +12 -3
  32. data/lib/raindown/project/card.rb +54 -0
  33. data/lib/raindown/project/endpoint.rb +14 -0
  34. data/lib/raindown/project/project.rb +168 -0
  35. data/lib/raindown/project_nodes.rb +47 -0
  36. data/lib/raindown/projects.rb +1 -1
  37. data/lib/raindown/rasters.rb +98 -0
  38. data/lib/raindown/scene.rb +10 -0
  39. data/lib/raindown/services/connections.rb +65 -0
  40. data/lib/raindown/services/local.rb +139 -0
  41. data/lib/raindown/services.rb +72 -0
  42. data/lib/raindown/skeleton.rb +1 -1
  43. data/lib/raindown/table/table.rb +2 -2
  44. data/lib/raindown/table/table_column.rb +0 -2
  45. data/lib/raindown/table/table_file.rb +2 -2
  46. data/lib/raindown/table/written_table.rb +0 -2
  47. data/lib/raindown/toc/markdown_headings.rb +98 -0
  48. data/lib/raindown/{toc.rb → toc/toc.rb} +8 -5
  49. data/lib/raindown/tui/endpoints.rb +1 -1
  50. data/lib/raindown/tui/feelings/feelings_log.rb +2 -1
  51. data/lib/raindown/tui/navigator/navigator.rb +1 -1
  52. data/lib/raindown/watcher/project.rb +2 -2
  53. data/lib/raindown/weather/branch_tree.rb +129 -0
  54. data/lib/raindown/weather/forecast.rb +193 -0
  55. data/lib/raindown/weather/forecast_cache.rb +58 -0
  56. data/lib/raindown/weather/forecast_cards.rb +115 -0
  57. data/lib/raindown/weather/sky.rb +82 -0
  58. data/lib/raindown/weather/weather.rb +140 -0
  59. data/lib/raindown/wiki_links.rb +2 -6
  60. data/lib/raindown.rb +39 -14
  61. data/lib/version.rb +1 -1
  62. metadata +33 -9
  63. data/lib/raindown/js/host.mjs +0 -51
  64. data/lib/raindown/tui/card.rb +0 -56
  65. data/lib/raindown/tui/endpoint.rb +0 -16
  66. data/lib/raindown/tui/project.rb +0 -147
  67. /data/lib/raindown/{page.rb → pages/page.rb} +0 -0
@@ -4,6 +4,7 @@ require_relative '../raindown'
4
4
  require_relative 'bare_directives'
5
5
  require_relative 'front_matter'
6
6
  require_relative 'node_blocks'
7
+ require_relative 'outdent'
7
8
  require_relative 'written'
8
9
 
9
10
  module Raindown
@@ -30,47 +31,87 @@ module Raindown
30
31
  @problems = []
31
32
  @bare = BareDirectives.enabled?(path:, content:, bare:)
32
33
  @metadata = metadata
34
+ @ahead = {}
33
35
  end
34
36
 
37
+ # Outdented blocks' directives are written first, then the rest of the file's.
35
38
  def call
36
- lines = @content.lines
37
- blocks = NodeBlocks.find(lines, front_matter: FrontMatter.parse(@content).first, bare: @bare)
38
- .select { |block| block[:writes] }
39
- return @content if blocks.empty?
39
+ front_matter = FrontMatter.parse(@content).first
40
+ outdented = Outdent.rewrite(@content) { |content, range| write(content, front_matter:, within: range) }
41
+ write(outdented, front_matter:)
42
+ end
43
+
44
+ private
45
+
46
+ # Writes the blocks whose directives are within the range of lines, or all of them.
47
+ def write(content, front_matter:, within: nil)
48
+ lines = content.lines
49
+ blocks = NodeBlocks.find(lines, front_matter:, bare: @bare).select do |block|
50
+ block[:writes] && (within.nil? || within.cover?(block[:open]))
51
+ end
52
+ return content if blocks.empty?
40
53
 
54
+ @ahead = written_ahead(lines, blocks)
41
55
  # From the bottom up, so the lines above each block stay where they are, so its problems are put back in order.
56
+ problems = @problems
57
+ @problems = []
42
58
  blocks.reverse_each { |block| lines[block[:open]..block[:last]] = block_lines(lines, block) }
43
- @problems.reverse!
59
+ @problems = problems + @problems.reverse
44
60
  lines.join
45
61
  end
46
62
 
47
- private
48
-
49
63
  def block_lines(lines, block)
50
- comment, written = comment_and_written(written_lines(lines, block))
51
- context = { document: @content, metadata: @metadata, bare: @bare, comment:, written: }
52
- written = written_lines_of(node_written(block, context))
53
- return [line(lines[block[:open]]), *bare(written, block)] unless block[:close]
64
+ written = written_lines_of(node_written(block, context_of(lines, block)))
65
+ return [*directive(lines, block), *bare(written, block)] unless block[:close]
54
66
 
55
67
  delimited(lines, block, written)
56
68
  end
57
69
 
70
+ # The document as it's written so far, from the bottom up, so a node sees what the nodes below it just wrote, like
71
+ # the TOC the headings they write.
72
+ def context_of(lines, block)
73
+ comment, written = comment_and_written(Problem.without_alert(written_lines(lines, block)))
74
+ { document: lines.join, metadata: @metadata, bare: @bare, comment:, written: }
75
+ end
76
+
77
+ # What the JavaScript nodes among the blocks write, by each block's line, asked of their host together, as a request
78
+ # costs about as much as a node. So they see the document as it was before the blocks wrote, not what the nodes
79
+ # below them just wrote. A host that can't be asked leaves each to be asked alone, to say why in its place.
80
+ def written_ahead(lines, blocks)
81
+ ahead = blocks.select { it[:node].is_a?(JSNode) }
82
+ calls = ahead.map { it[:node].call_for(:rewrite, context_of(lines, it)) }
83
+ ahead.map { it[:open] }.zip(JSNodes.call(calls)).to_h
84
+ rescue JSNodes::Error
85
+ {}
86
+ end
87
+
58
88
  # A node that raises writes why in its place, like a Problem, so the rest of the file still writes back.
59
89
  def node_written(block, context)
60
90
  @problems << block[:node].warning if block[:node].is_a?(Problem)
61
- written = Nodes.call(block[:node], :rewrite, **context)
91
+ written = written_by(block, context)
62
92
  return written if written.is_a?(Written)
63
93
 
64
94
  Written.new(written.to_s)
65
95
  rescue StandardError => e
66
96
  problem = Problem.new("Couldn't write: #{e.message}", block[:open] + 1)
67
97
  @problems << problem.warning
68
- Written.new(problem.rewrite)
98
+ Written.new(problem.rewrite(written: context[:written]))
99
+ end
100
+
101
+ # What the block's node writes, as it was asked ahead, or asked now.
102
+ def written_by(block, context)
103
+ output = @ahead.fetch(block[:open]) { return Nodes.call(block[:node], :rewrite, **context) }
104
+ raise output if output.is_a?(JSNodes::Error)
105
+
106
+ output
69
107
  end
70
108
 
71
109
  # The directive, what the node wrote and the end tag, straight after each other, as a comment on its own line ends
72
110
  # at its -->, so what's written below it, like a list, is its own block.
73
- def delimited(lines, block, written) = [line(lines[block[:open]]), *written, line(lines[block[:close]])]
111
+ def delimited(lines, block, written) = [*directive(lines, block), *written, line(lines[block[:close]])]
112
+
113
+ # Its directive's line, or none for a bare grid, which has none.
114
+ def directive(lines, block) = lines[block[:open]...block[:first]].map { line(it) }
74
115
 
75
116
  # A bare block ends at the next blank line, so what it writes can't have one.
76
117
  def bare(written, block)
@@ -98,7 +139,7 @@ module Raindown
98
139
 
99
140
  # What the block's node wrote last time, between its directive and its end tag, or the next blank line.
100
141
  def written_lines(lines, block)
101
- lines[(block[:open] + 1)...(block[:close] || (block[:last] + 1))]
142
+ lines[block[:first]...(block[:close] || (block[:last] + 1))]
102
143
  end
103
144
 
104
145
  def line(line)
@@ -1,17 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'chart/chart'
4
3
  require_relative 'fragments'
5
4
  require_relative 'js/js_nodes'
6
- require_relative 'list'
7
5
  require_relative 'markdown_nodes'
8
6
  require_relative 'node'
9
7
  require_relative 'problem'
10
8
  require_relative 'stat'
11
- require_relative 'table/table'
12
- require_relative 'toc'
13
9
 
14
10
  module Raindown
11
+ # Raindown's own nodes, each loaded the first time it's used, like when a note names it, so starting Raindown doesn't
12
+ # load what a session may never use, like the weather's, or what they use, like unicode-display_width's tables.
13
+ { Chart: 'chart/chart', Grid: 'grid', Image: 'image', List: 'list', Table: 'table/table', TOC: 'toc/toc',
14
+ Weather: 'weather/weather' }.each { |name, path| autoload(name, File.join(__dir__, path)) }
15
+
15
16
  # Nodes are LowNodes that a node block writes into the Markdown and renders into the HTML.
16
17
  # A project keeps them in a .raindown folder, which is loaded with Dirload, beside its JavaScript and Markdown
17
18
  # Components, if any.
@@ -24,25 +25,57 @@ module Raindown
24
25
  class NoRender < StandardError; end
25
26
 
26
27
  FOLDER = '.raindown'
27
- # Lowercase directives for Raindown's own nodes. A project's own TOC, List, Chart or Table node replaces Raindown's.
28
- BUILT_INS = { 'toc' => 'TOC', 'list' => 'List', 'chart' => 'Chart', 'table' => 'Table' }.freeze
28
+ # Lowercase directives for Raindown's own nodes. A project's own TOC, List, Chart, Table, Image, Grid or Weather node
29
+ # replaces Raindown's.
30
+ BUILT_INS = { 'toc' => 'TOC', 'list' => 'List', 'chart' => 'Chart', 'table' => 'Table', 'image' => 'Image',
31
+ 'grid' => 'Grid', 'weather' => 'Weather' }.freeze
29
32
  # A placeholder for a node's HTML, which stays as it is through Commonmarker and Antlers, with its slot's HTML, as a
30
33
  # fragment, when it has one.
31
34
  PLACEHOLDER = /<!-- raindown:node (\d+)(?: slot (\d+))? -->/
32
35
 
33
36
  module_function
34
37
 
35
- # Loads the .raindown folder beside the file or in the nearest directory above it, once per process.
38
+ # Loads the .raindown folder beside the file or in the nearest directory above it, once per process, and gives why
39
+ # any of its files couldn't be loaded, like ".raindown/stars.rb:3: syntax errors found". Each file loads on its own,
40
+ # so one that can't be leaves the others loaded.
36
41
  def load(path)
37
42
  folder = find_folder(File.dirname(File.expand_path(path)))
38
- return if folder.nil? || loaded.include?(folder)
43
+ return [] if folder.nil?
44
+ return load_errors[folder] if loaded.include?(folder)
45
+
46
+ loaded << folder
47
+ load_errors[folder] = load_ruby(folder) + JSNodes.load(folder) + MarkdownNodes.load(folder)
48
+ end
39
49
 
50
+ # Dirload's steps, for the folder's Ruby files, as dirload(folder) takes them, a file that fails one being left out
51
+ # of the next, and why each failed.
52
+ def load_ruby(folder)
40
53
  require 'lownode'
41
54
  require 'dirload'
42
- loaded << folder
43
- dirload(folder)
44
- JSNodes.load(folder)
45
- MarkdownNodes.load(folder)
55
+ files = Dirload.file_paths(absolute_path: folder).map { [it, Dirload.find_adapter(file_path: it)] }
56
+ errors = {}
57
+ %i[mapload preload evaluate].each do |step|
58
+ files.each do |file, adapter|
59
+ next if adapter.nil? || errors.key?(file)
60
+
61
+ adapter.public_send(step, file_path: file)
62
+ rescue StandardError, ScriptError => e
63
+ errors[file] = e.message
64
+ end
65
+ end
66
+ errors.map { |file, message| load_error(folder, file, message) }
67
+ end
68
+
69
+ # Why a file couldn't be loaded, by its path from the project, with its line, when the error has it.
70
+ def load_error(folder, file, message)
71
+ listed = file.delete_prefix("#{File.dirname(folder)}/")
72
+ return "#{listed}#{message.delete_prefix(file)}" if message.start_with?("#{file}:")
73
+
74
+ "#{listed}: #{message}"
75
+ end
76
+
77
+ def load_errors
78
+ @load_errors ||= {}
46
79
  end
47
80
 
48
81
  def find_folder(directory)
@@ -84,8 +117,10 @@ module Raindown
84
117
  klass < Node || (defined?(::LowNode) && klass < ::LowNode) || false
85
118
  end
86
119
 
87
- # Calls a node's rewrite or render with the context keywords it takes.
120
+ # Calls a node's rewrite or render with the context keywords it takes, raindown: being Raindown's services for its
121
+ # project.
88
122
  def call(node, name, **context)
123
+ context = { raindown: Services.for(context[:metadata]), **context }
89
124
  parameters = node.method(name).parameters
90
125
  return node.public_send(name, **context) if parameters.any? { |type, _| type == :keyrest }
91
126
 
@@ -108,11 +143,11 @@ module Raindown
108
143
 
109
144
  # Renders the node where it is, as a fragment, so its headings are part of the page, or through a placeholder, once
110
145
  # the page is HTML, when its render takes html:, the page's HTML, like TOC's.
111
- def rendered(node, context, slot: '')
146
+ def rendered(node, context, slot: '', **keywords)
112
147
  raise NoRender, "#{node.class.name} has no render, so it can't be write-in: write it bare or in comments" unless renders?(node)
113
148
  return placeholder(node, slot:) if !template?(node.class) && takes?(node, :render, :html)
114
149
 
115
- Fragments.add(html(node, context, slot:))
150
+ Fragments.add(html(node, context, slot:, **keywords))
116
151
  end
117
152
 
118
153
  def takes?(node, method_name, keyword)
@@ -140,8 +175,10 @@ module Raindown
140
175
  end
141
176
  end
142
177
 
178
+ # Rendering Markdown, a node's written as it would be written back.
143
179
  def html(node, context, **keywords)
144
180
  return node.render_template(props: {}).to_s if template?(node.class)
181
+ return call(node, :rewrite, **context.to_h, **keywords).to_s if !Raindown.commonmark? && node.respond_to?(:rewrite)
145
182
 
146
183
  call(node, :render, **context.to_h, **keywords).to_s
147
184
  end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'code_blocks'
4
+
5
+ module Raindown
6
+ # A block that reaches the page's top and left edges, framed by a rule of ^ above it and a < before each of its lines:
7
+ # ^^^^^^^^^^^^^^^^^^^^
8
+ # < weather:
9
+ # < main -> fix
10
+ # Its lines are written back and rendered without their <, so directives work in it as anywhere else, then it's put
11
+ # back. In HTML it's a <div class="outdent">, which a page's styles take to its edges.
12
+ module Outdent
13
+ RULE = /\A\^{3,}[ \t]*\r?\n?\z/
14
+ LINE = /\A</
15
+ HTML = "<div class=\"outdent\">\n"
16
+
17
+ module_function
18
+
19
+ # Each block outside code, as { rule:, first:, last: }, its rule's line and its first and last lines.
20
+ def blocks(lines)
21
+ found = []
22
+ index = 0
23
+ while index < lines.length
24
+ if (code = CodeBlocks.at(lines, index))
25
+ index = code[:close] + 1
26
+ elsif lines[index].match?(RULE) && lines[index + 1]&.match?(LINE)
27
+ found << block_at(lines, index)
28
+ index = found.last[:last] + 1
29
+ else
30
+ index += 1
31
+ end
32
+ end
33
+ found
34
+ end
35
+
36
+ def block_at(lines, rule)
37
+ last = rule + 1
38
+ last += 1 while lines[last + 1]&.match?(LINE)
39
+ { rule:, first: rule + 1, last: }
40
+ end
41
+
42
+ # The content with each block's lines written by the block, which is given the file with that block's lines
43
+ # without their <, so they're at their lines in the file, and the range of them, which only it may change.
44
+ def rewrite(content, &)
45
+ lines = content.lines
46
+ blocks(lines).reverse_each { |block| lines[block[:first]..block[:last]] = written_block(lines, block, &) }
47
+ lines.join
48
+ end
49
+
50
+ # The block's lines as the block given writes them, each with its < again.
51
+ def written_block(lines, block)
52
+ given = with_block_unwrapped(lines, block)
53
+ written = yield(given.join, block[:first]..block[:last]).lines
54
+ last = block[:last] + written.length - given.length
55
+ outer_lines(written[block[:first]..last], ends_file: !lines[block[:last]].end_with?("\n"))
56
+ end
57
+
58
+ # A blank line after the block's lines ends a bare block in it with it, as the line after it is outside it.
59
+ def with_block_unwrapped(lines, block)
60
+ [*lines[0...block[:first]], *inner_lines(lines[block[:first]..block[:last]]), "\n", *lines[(block[:last] + 1)..]]
61
+ end
62
+
63
+ # The content with every block's lines without their <, at the same lines, so what's in them is read as anywhere
64
+ # else, like by Document.
65
+ def without_markers(content)
66
+ lines = content.lines
67
+ blocks(lines).each { |block| lines[block[:first]..block[:last]] = inner_lines(lines[block[:first]..block[:last]]) }
68
+ lines.join
69
+ end
70
+
71
+ # The last line of a bare block that opens at a line, which ends with the outdent block it's in, if it's in one.
72
+ def bare_block_last(blocks, open, last)
73
+ block = blocks.find { (it[:first]..it[:last]).cover?(open) }
74
+ return last unless block
75
+
76
+ [last, block[:last]].min
77
+ end
78
+
79
+ # The Markdown with each block as a <div class="outdent"> of its lines without their <, so they render as Markdown.
80
+ def unwrapped(markdown)
81
+ lines = markdown.lines
82
+ blocks(lines).reverse_each do |block|
83
+ inner = inner_lines(lines[block[:first]..block[:last]]).map { it.end_with?("\n") ? it : "#{it}\n" }
84
+ lines[block[:rule]..block[:last]] = [HTML, "\n", *inner, "\n", "</div>\n"]
85
+ end
86
+ lines.join
87
+ end
88
+
89
+ def inner_lines(lines)
90
+ lines.map { it.sub(/\A< ?/, '') }
91
+ end
92
+
93
+ # Each line with its <, and the last without a line break when the block ends the file without one.
94
+ def outer_lines(lines, ends_file:)
95
+ outer = lines.map { it.strip.empty? ? "<#{it}" : "< #{it}" }
96
+ outer[-1] = outer[-1].chomp if ends_file && outer.any?
97
+ outer
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raindown
4
+ # What Pages keeps for as long as the process runs, like "raindown server", until it changes: each root's files, and
5
+ # each file's front matter. An editor that's told of every change to the files, like the Obsidian plugin, which knows
6
+ # its vault's, watches them, so their modified times aren't checked each time they're listed, which in WebAssembly takes
7
+ # as long as listing thousands of them.
8
+ module Listings
9
+ # Each file's front matter, by its path, with the time it was modified when it was read.
10
+ def front_matters
11
+ @front_matters ||= {}
12
+ end
13
+
14
+ # Each file's metadata from its path, by its root and path, like order: 1 for docs/_1-setup.md, which only changes
15
+ # with its path.
16
+ def implicit_metadatas
17
+ @implicit_metadatas ||= {}
18
+ end
19
+
20
+ # The front matter the editor's already read, by path, is kept rather than read again.
21
+ def watch(front_matters = {})
22
+ @watched = true
23
+ front_matters.each { |path, front_matter| self.front_matters[path] = [:given, front_matter] }
24
+ end
25
+
26
+ def watched?
27
+ @watched == true
28
+ end
29
+
30
+ # Files or folders added, changed or removed, by path, so their front matter's read again, and each root's listing
31
+ # has the files added and not those removed, rather than listing every file again.
32
+ def changed(paths)
33
+ return if paths.empty?
34
+
35
+ pages.clear
36
+ paths.each do |path|
37
+ front_matters.delete(path)
38
+ listings.each do |root, listing|
39
+ next unless path.start_with?("#{root}/")
40
+
41
+ listing[:file_paths].reject! { it == path || it.start_with?("#{path}/") }
42
+ listing[:file_paths] << path if File.file?(path) && !path.delete_prefix(root).include?('/.')
43
+ end
44
+ end
45
+ end
46
+
47
+ # Each root's Pages, while an editor watching the files says none have changed, so their metadata's worked out once.
48
+ def pages
49
+ @pages ||= {}
50
+ end
51
+
52
+ # The files under the root, outside hidden folders, like Dirload lists them. They're listed again once a folder among
53
+ # them has changed, which adding, removing or renaming a file or folder in it does, or the editor watching says so.
54
+ def file_paths(root)
55
+ listing = listings[root]
56
+ return listing[:file_paths] if listing && (watched? || listing[:folders].all? { |folder, at| modified_at(folder) == at })
57
+
58
+ folders, file_paths = Dir["#{root}/**/*"].partition { File.directory?(it) }
59
+ listings[root] = { file_paths:, folders: [root, *folders].to_h { [it, modified_at(it)] } }
60
+ file_paths
61
+ end
62
+
63
+ def listings
64
+ @listings ||= {}
65
+ end
66
+
67
+ def modified_at(path)
68
+ File.mtime(path) if File.exist?(path)
69
+ end
70
+ end
71
+ end
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'front_matter'
3
+ require_relative '../front_matter'
4
+ require_relative 'listings'
4
5
  require_relative 'page'
5
- require_relative 'utils'
6
+ require_relative '../utils'
6
7
 
7
8
  module Raindown
8
9
  # The metadata of a project's Markdown files, which list: lists by tag.
@@ -13,49 +14,31 @@ module Raindown
13
14
  # folder: basics, order: 1, path: docs/_basics/_1-getting-started.md
14
15
  # Folder tags can share a folder name with "&", like docs/_1&_basics/getting-started.md.
15
16
  # An app can add its own metadata by subclassing and overriding implicit_metadata, like Raindeer's URL paths.
16
- # Each file's front matter is only read once something lists the files. The listing and the front matter are kept for
17
- # as long as the process runs, like "raindown server", until they change.
17
+ # Each file's front matter is only read once something lists the files, and kept, with the listing, by Listings.
18
18
  class Pages
19
19
  EXTENSIONS = %w[md rd markdown raindown].freeze
20
20
  SEGMENTS = Regexp.union(['/', '&', '-'])
21
21
 
22
- class << self
23
- # Each file's front matter, by its path, with the time it was modified when it was read.
24
- def front_matters
25
- @front_matters ||= {}
26
- end
22
+ extend Listings
27
23
 
24
+ class << self
28
25
  # The Markdown files under a project's root, the folder with its .raindown folder, or a vault, or else the file's
29
26
  # folder.
30
27
  def for(path)
31
- require_relative 'projects'
28
+ require_relative '../projects'
32
29
  load(root: Projects.content_root(path) || File.dirname(File.expand_path(path)))
33
30
  end
34
31
 
35
- # Lists the files under the root, without loading them, for Dirload.
32
+ # Lists the files under the root, without loading them, for Dirload, once while an editor watching them says none
33
+ # have changed.
36
34
  def load(root:)
35
+ return pages[root] if watched? && pages[root]
36
+
37
37
  require 'dirload'
38
38
  file_types = file_paths(root).group_by { Dirload.extension(file_path: it) }
39
- new(metadata: Dirload::Metadata.new(loaded_paths: {}, missed_paths: [], file_types:), root:)
40
- end
41
-
42
- # The files under the root, outside hidden folders, like Dirload lists them. They're listed again once a folder
43
- # among them has changed, which adding, removing or renaming a file or folder in it does.
44
- def file_paths(root)
45
- listing = listings[root]
46
- return listing[:file_paths] if listing&.dig(:folders)&.all? { |folder, modified| modified_at(folder) == modified }
47
-
48
- folders, file_paths = Dir["#{root}/**/*"].partition { File.directory?(it) }
49
- listings[root] = { file_paths:, folders: [root, *folders].to_h { [it, modified_at(it)] } }
50
- file_paths
51
- end
52
-
53
- def listings
54
- @listings ||= {}
55
- end
56
-
57
- def modified_at(path)
58
- File.mtime(path) if File.exist?(path)
39
+ loaded = new(metadata: Dirload::Metadata.new(loaded_paths: {}, missed_paths: [], file_types:), root:)
40
+ pages[root] = loaded if watched?
41
+ loaded
59
42
  end
60
43
  end
61
44
 
@@ -109,14 +92,18 @@ module Raindown
109
92
  end
110
93
 
111
94
  def process(file_path:)
112
- metadata = @metadata[file_path] = implicit_metadata(file_path:).merge(front_matter(file_path))
95
+ implicit = self.class.implicit_metadatas[[@root, file_path]] ||= implicit_metadata(file_path:)
96
+ metadata = @metadata[file_path] = implicit.merge(front_matter(file_path))
113
97
  metadata.each { |type, tag| Array(tag).each { |item| tag(type:, tag: item, file_path:) } }
114
98
  end
115
99
 
116
- # The file's front matter, read again only when the file's changed since it was last read.
100
+ # The file's front matter, read again only when the file's changed since it was last read, or an editor watching the
101
+ # files says so.
117
102
  def front_matter(file_path)
118
- modified = File.mtime(file_path)
119
103
  modified_then, front_matter = self.class.front_matters[file_path]
104
+ return front_matter if modified_then && self.class.watched?
105
+
106
+ modified = File.mtime(file_path)
120
107
  return front_matter if modified_then == modified
121
108
 
122
109
  FrontMatter.read(file_path).tap { self.class.front_matters[file_path] = [modified, it] }
@@ -6,7 +6,9 @@ module Raindown
6
6
  # What a directive writes in place of its output when it can't run, like toc: extra, or a node that raised, so the
7
7
  # rest of the file still writes back. In the file it's under its directive, so only its warning says its line and
8
8
  # name. It never starts with the name, like toc:, which would read as a directive once the one above it's deleted.
9
+ # It keeps what the directive wrote below it, so a typo in a directive, like grid: 80, doesn't lose its block.
9
10
  class Problem < Node
11
+ ALERT = "> [!WARNING]\n"
10
12
  attr_reader :message, :line
11
13
 
12
14
  def initialize(message, line, name = nil)
@@ -19,11 +21,18 @@ module Raindown
19
21
  # A message as a Markdown alert, which Obsidian, GitHub and Raindown show as a warning, and which can't read as a
20
22
  # directive, as it starts with >.
21
23
  def self.alert(message)
22
- "> [!WARNING]\n#{message.lines.map { "> #{it}" }.join}"
24
+ "#{ALERT}#{message.lines.map { "> #{it}" }.join}"
23
25
  end
24
26
 
25
- def rewrite
26
- self.class.alert(@message)
27
+ # The lines a node wrote, without the alert a Problem wrote above them, so the node doesn't read it as its own.
28
+ def self.without_alert(lines)
29
+ return lines unless lines.first == ALERT
30
+
31
+ lines.drop(1).drop_while { it.start_with?('> ') }
32
+ end
33
+
34
+ def rewrite(written: [])
35
+ [self.class.alert(@message), *written.map(&:chomp)].join("\n")
27
36
  end
28
37
 
29
38
  # For a terminal, or an editor to underline the line, like "line 6: toc: takes no value".
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'unicode/display_width'
4
+
5
+ module Raindown
6
+ # A chart on a dashboard, drawn from its data by its project's Worker: a title of its file, from its project,
7
+ # and the heading above it, then its directive, then the chart, each line with its Layout style,
8
+ # in a border with rounded corners. It's as wide as the chart, so the title and directive are cut to fit it:
9
+ # ╭───────────────────────────────────────╮
10
+ # │ markdown.md › Charts │
11
+ # │ chart: 'data/downloads.csv' width: 30 │
12
+ # │ Jun ██████▊ 640 │
13
+ # ╰───────────────────────────────────────╯
14
+ class Card < Data.define(:project, :path, :line, :heading, :directive, :chart)
15
+ # The border's columns either side of a card's lines, "│ " and " │", and its lines above and below them.
16
+ BORDER_WIDTH = 4
17
+ BORDER_HEIGHT = 2
18
+
19
+ def title = [path.delete_prefix(File.join(project, '')), heading].compact.join(' › ')
20
+
21
+ def lines = parts.map(&:first)
22
+
23
+ # Each line, with its style.
24
+ def parts = [[title, :heading], [directive, :directive], *chart.map { [it, :chart] }]
25
+
26
+ # As wide as its chart.
27
+ def width = chart.map { Unicode::DisplayWidth.of(it) }.max || 0
28
+
29
+ def height = lines.length
30
+
31
+ # With its border, within a range of widths, like a pane's.
32
+ def box_width(within: nil) = within ? (width + BORDER_WIDTH).clamp(within) : width + BORDER_WIDTH
33
+ def box_height = height + BORDER_HEIGHT
34
+
35
+ # A line of the card in its border, as [text, style, width] parts, for a card the box width wide.
36
+ # The border's top is line 0, and below its bottom, the lines are blank, for a card shorter than its row.
37
+ # The title's style is title_style, like when the card's selected.
38
+ def format_line(line, box_width:, title_style: :heading)
39
+ width = [box_width - BORDER_WIDTH, 0].max
40
+ return [border(width, '╭', '╮')] if line.zero?
41
+ return [border(width, '╰', '╯')] if line == height + 1
42
+ return [['', :blank, width + BORDER_WIDTH]] if line > height + 1
43
+
44
+ text, style = parts[line - 1]
45
+ [['│ ', :border, 2], [text, style == :heading ? title_style : style, width], [' │', :border, 2]]
46
+ end
47
+
48
+ private
49
+
50
+ def border(width, left, right)
51
+ ["#{left}#{'─' * (width + 2)}#{right}", :border, width + BORDER_WIDTH]
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raindown
4
+ # An endpoint in a project's files, listed in the nav pane by its request, like "GET httpbin.org/uuid",
5
+ # with its method in its style, like GET in :method_get.
6
+ Endpoint = Data.define(:project, :path, :line, :method, :url) do
7
+ def text = "#{method} #{address}"
8
+
9
+ # The URL without its scheme.
10
+ def address = url.sub(%r{\Ahttps?://}i, '')
11
+
12
+ def style = :"method_#{method.downcase}"
13
+ end
14
+ end