raindown 0.19.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 (53) hide show
  1. checksums.yaml +4 -4
  2. data/lib/raindown/area.rb +88 -0
  3. data/lib/raindown/chart/drawing.rb +0 -2
  4. data/lib/raindown/chart/line.rb +1 -1
  5. data/lib/raindown/cli.rb +2 -2
  6. data/lib/raindown/config.rb +11 -0
  7. data/lib/raindown/document.rb +7 -3
  8. data/lib/raindown/editing.rb +4 -2
  9. data/lib/raindown/endpoint/curl.rb +0 -1
  10. data/lib/raindown/grid.rb +154 -0
  11. data/lib/raindown/grid_directives.rb +43 -0
  12. data/lib/raindown/grid_wrapping.rb +46 -0
  13. data/lib/raindown/http_json.rb +45 -0
  14. data/lib/raindown/image.rb +44 -0
  15. data/lib/raindown/js/js_nodes.rb +83 -36
  16. data/lib/raindown/js/quickjs_host.rb +63 -0
  17. data/lib/raindown/js/quickjs_runtime.js +61 -0
  18. data/lib/raindown/js/services.js +31 -0
  19. data/lib/raindown/markdown_nodes.rb +2 -0
  20. data/lib/raindown/node_blocks.rb +22 -9
  21. data/lib/raindown/node_injector.rb +56 -17
  22. data/lib/raindown/nodes.rb +50 -15
  23. data/lib/raindown/outdent.rb +100 -0
  24. data/lib/raindown/problem.rb +12 -3
  25. data/lib/raindown/project/card.rb +54 -0
  26. data/lib/raindown/project/endpoint.rb +14 -0
  27. data/lib/raindown/project/project.rb +168 -0
  28. data/lib/raindown/project_nodes.rb +47 -0
  29. data/lib/raindown/rasters.rb +98 -0
  30. data/lib/raindown/scene.rb +10 -0
  31. data/lib/raindown/services/connections.rb +65 -0
  32. data/lib/raindown/services/local.rb +139 -0
  33. data/lib/raindown/services.rb +72 -0
  34. data/lib/raindown/table/table.rb +2 -2
  35. data/lib/raindown/table/table_column.rb +0 -2
  36. data/lib/raindown/table/table_file.rb +0 -1
  37. data/lib/raindown/table/written_table.rb +0 -2
  38. data/lib/raindown/tui/endpoints.rb +1 -1
  39. data/lib/raindown/tui/navigator/navigator.rb +1 -1
  40. data/lib/raindown/watcher/project.rb +2 -2
  41. data/lib/raindown/weather/branch_tree.rb +129 -0
  42. data/lib/raindown/weather/forecast.rb +193 -0
  43. data/lib/raindown/weather/forecast_cache.rb +58 -0
  44. data/lib/raindown/weather/forecast_cards.rb +115 -0
  45. data/lib/raindown/weather/sky.rb +82 -0
  46. data/lib/raindown/weather/weather.rb +140 -0
  47. data/lib/raindown.rb +10 -1
  48. data/lib/version.rb +1 -1
  49. metadata +27 -6
  50. data/lib/raindown/js/host.mjs +0 -51
  51. data/lib/raindown/tui/card.rb +0 -56
  52. data/lib/raindown/tui/endpoint.rb +0 -16
  53. data/lib/raindown/tui/project.rb +0 -147
@@ -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/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)
@@ -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
@@ -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
@@ -0,0 +1,168 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../actions'
4
+ require_relative '../bare_directives'
5
+ require_relative '../chart/chart'
6
+ require_relative '../document'
7
+ require_relative '../editing'
8
+ require_relative '../front_matter'
9
+ require_relative '../node_blocks'
10
+ require_relative '../nodes'
11
+ require_relative '../pages/pages'
12
+ require_relative '../projects'
13
+ require_relative '../project_nodes'
14
+ require_relative '../rasters'
15
+ require_relative '../workers/workers'
16
+ require_relative '../stat'
17
+ require_relative 'card'
18
+ require_relative 'endpoint'
19
+ require_relative '../code_blocks'
20
+ require_relative '../endpoint/curl'
21
+ require_relative '../endpoint/endpoint_variables'
22
+
23
+ module Raindown
24
+ # What a UI, like the terminal UI or the app, asks of a project, answered in the project's Worker with its own nodes loaded,
25
+ # so two projects' nodes of the same name stay apart. Answers are plain data, which a Worker sends back through a pipe.
26
+ class Project
27
+ Outline = Data.define(:lines, :headings, :directives)
28
+
29
+ attr_reader :root
30
+
31
+ # Workers for the projects in the folder, each answering as a Project, which allows bare directives in any file,
32
+ # and runs endpoints with no response as it writes back, when asked, like with --bare and -x.
33
+ def self.workers(root:, bare: false, execute: false, **)
34
+ Workers.new(root:, **) { new(root: it, bare:, execute:) }
35
+ end
36
+
37
+ # The projects inside this one have workers of their own, so their files are left out.
38
+ def initialize(root:, bare: false, execute: false)
39
+ @root = root
40
+ @bare = bare
41
+ @execute = execute
42
+ @file_errors = []
43
+ @load_errors = Nodes.load(File.join(root, Nodes::FOLDER))
44
+ rescue StandardError, ScriptError => e
45
+ @load_errors = ["#{File.basename(root)}/#{Nodes::FOLDER}: #{e.message}"]
46
+ end
47
+
48
+ # A file's lines, headings and directives.
49
+ def outline(path)
50
+ document = document(path)
51
+ Outline.new(lines: File.readlines(path, chomp: true), headings: document.headings, directives: document.directives)
52
+ end
53
+
54
+ # Every chart and stat in the project's files, or any node that draws a card, drawn from its data, and the files
55
+ # that couldn't be read.
56
+ # The charts are found again when asked, and otherwise drawn again, so they follow changes to their data.
57
+ def charts(find: false)
58
+ if find || @charts.nil?
59
+ @file_errors = []
60
+ @charts = Projects.files(root).flat_map { |path| charts_in(path) }
61
+ end
62
+
63
+ { cards: drawn, errors: @load_errors + @file_errors }
64
+ end
65
+
66
+ # Every endpoint in the project's files that writes its response back, so it can run, by its request, leaving out
67
+ # the ones whose snippet can't be read.
68
+ def endpoints
69
+ Projects.files(root).flat_map do |path|
70
+ document = document(path)
71
+ # Most files have no endpoint, so they aren't parsed for one.
72
+ next [] unless document.markdown.include?('endpoint')
73
+
74
+ runnable = document.directives.select { it.endpoint? && it.writes }
75
+ runnable.filter_map { |directive| endpoint(path, document.markdown.lines, directive) }
76
+ rescue StandardError
77
+ []
78
+ end
79
+ end
80
+
81
+ # Markdown in one of the project's files, as Raindown would write it back, with its nodes, as { output, warnings }.
82
+ def rewrite_markdown(markdown, path)
83
+ Editing.rewrite_markdown(markdown, path:, bare: @bare)
84
+ end
85
+
86
+ # The names of the nodes that rasterize, Raindown's own and the project's, for a file in it.
87
+ def rasterizing(path)
88
+ Rasters.names(path)
89
+ end
90
+
91
+ # The project's own nodes, from its .raindown folder, as an app lists them, and why any couldn't be loaded.
92
+ def nodes
93
+ { nodes: ProjectNodes.of(root), errors: @load_errors }
94
+ end
95
+
96
+ # What the node on a line of a file that rasterizes, like weather:, shows behind its text, and how it formats its
97
+ # lines, as { painting:, formatting: }, with its options, like width: and height:.
98
+ def repaint(path, line, **options)
99
+ Rasters.repaint(path:, line:, **options)
100
+ end
101
+
102
+ def write_back(path)
103
+ Actions.write_back(path, bare: @bare, execute: @execute)
104
+ end
105
+
106
+ # Runs the endpoint on the line, replacing its response.
107
+ def execute(path, line)
108
+ directive = Document.read(path, bare: @bare).directives.find { it.line == line && it.endpoint? }
109
+ raise Actions::NotAnEndpoint, "There's no endpoint on line #{line}" unless directive
110
+
111
+ Actions.execute(path, directive)
112
+ end
113
+
114
+ private
115
+
116
+ def endpoint(path, lines, directive)
117
+ snippet = CodeBlocks.endpoint(lines, directive.line).first&.dig(:body)
118
+ variables = EndpointVariables.new(path:, content: lines.join)
119
+ request = Curl.request(snippet.to_s, variables)
120
+ Endpoint.new(project: root, path:, line: directive.line, method: request.method, url: variables.redact(request.url)) if request
121
+ end
122
+
123
+ def charts_in(path)
124
+ document = document(path)
125
+ lines = document.markdown.lines
126
+ # Its headings are only parsed when it has a chart, as most files don't.
127
+ NodeBlocks.find(lines, front_matter: FrontMatter.parse(document.markdown).first, bare: document.bare)
128
+ .select { it[:node].respond_to?(:card_lines) }
129
+ .map { |block| chart(block, path:, lines:, headings: document.headings) }
130
+ rescue StandardError => e
131
+ @file_errors << "#{path.delete_prefix(File.join(root, ''))}: #{e.message}"
132
+ []
133
+ end
134
+
135
+ # The file's Document, read and parsed again only once the file's changed, so its endpoints, charts and outline
136
+ # share it, and finding them again only parses the files that changed.
137
+ def document(path)
138
+ stat = File.stat(path)
139
+ changed = [stat.mtime, stat.size]
140
+ cached_at, document = (@documents ||= {})[path]
141
+ return document if cached_at == changed
142
+
143
+ Document.read(path, bare: @bare).tap { @documents[path] = [changed, it] }
144
+ end
145
+
146
+ def drawn
147
+ pages = {}
148
+ @charts.map do |chart|
149
+ lines = lines(chart, pages[File.dirname(chart[:path])] ||= Pages.for(chart[:path]))
150
+ Card.new(project: root, chart: lines, **chart.except(:node))
151
+ end
152
+ end
153
+
154
+ def chart(block, path:, lines:, headings:)
155
+ line = block[:open] + 1
156
+ directive = Directives.text(lines[block[:open]])
157
+
158
+ { path:, line:, heading: headings.select { it.line < line }.last&.text, directive:, node: block[:node] }
159
+ end
160
+
161
+ # Draws the chart or stat as its Markdown would be written back, with the files its project lists.
162
+ def lines(chart, metadata)
163
+ chart[:node].card_lines(metadata:)
164
+ rescue StandardError => e
165
+ ["Can't chart: #{e.message}"]
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'nodes'
4
+
5
+ module Raindown
6
+ # A project's own nodes, from its .raindown folder, as plain data for an app to list: each one's name, whether it's
7
+ # Ruby, JavaScript or Markdown, its file, from the project's root, and whether it rasterizes. They're the nodes loaded
8
+ # in the process, so a project's Worker lists its own.
9
+ module ProjectNodes
10
+ module_function
11
+
12
+ def of(root)
13
+ folder = File.join(root, Nodes::FOLDER)
14
+ return [] unless File.directory?(folder)
15
+
16
+ Nodes.load(folder)
17
+ (ruby(folder) + javascript(folder) + markdown(folder)).map { listed(root, **it) }.sort_by { it[:name] }
18
+ end
19
+
20
+ # By where they're defined, so nothing else is loaded to find them.
21
+ def ruby(folder)
22
+ Object.constants.filter_map do |name|
23
+ file, = Object.const_source_location(name)
24
+ next unless file&.start_with?("#{folder}/")
25
+
26
+ klass = Object.const_get(name)
27
+ { name: name.to_s, kind: 'ruby', file:, node: klass } if Nodes.node?(klass)
28
+ end
29
+ end
30
+
31
+ def javascript(folder)
32
+ JSNodes.node_classes.filter_map do |name, node|
33
+ { name:, kind: 'javascript', file: node.file, node: } if node.file && File.dirname(node.file) == folder
34
+ end
35
+ end
36
+
37
+ def markdown(folder)
38
+ MarkdownNodes.node_classes.filter_map do |name, node|
39
+ { name:, kind: 'markdown', file: node.path, node: } if File.dirname(node.path) == folder
40
+ end
41
+ end
42
+
43
+ def listed(root, name:, kind:, file:, node:)
44
+ { name:, kind:, file: file.delete_prefix("#{root}/"), rasterizes: node.method_defined?(:rasterize) }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'bare_directives'
4
+ require_relative 'front_matter'
5
+ require_relative 'grid_directives'
6
+ require_relative 'node_blocks'
7
+ require_relative 'nodes'
8
+ require_relative 'outdent'
9
+ require_relative 'pages/pages'
10
+ require_relative 'area'
11
+ require_relative 'scene'
12
+ require_relative 'utils'
13
+
14
+ module Raindown
15
+ # The nodes that rasterize, painting pixels behind their text in an app that draws pixels, like weather:'s sky. An
16
+ # app finds their blocks by name, then asks for each one's pixels by its directive's line.
17
+ module Rasters
18
+ module_function
19
+
20
+ # The names of Raindown's own nodes that rasterize, like weather, and, for a file, its project's Ruby and JavaScript
21
+ # Components that do.
22
+ def names(path = nil)
23
+ built_in = Nodes::BUILT_INS.select { |_, name| Raindown.const_get(name).method_defined?(:rasterize) }.keys
24
+ return built_in unless path
25
+
26
+ Nodes.load(path)
27
+ folder = Nodes.find_folder(File.dirname(File.expand_path(path)))
28
+ return built_in unless folder
29
+
30
+ built_in + ruby_rasterizing(folder) + JSNodes.rasterizing(folder)
31
+ end
32
+
33
+ # The names of the Ruby nodes in a project's folder that rasterize, by where they're defined, so nothing else is
34
+ # loaded to find them.
35
+ def ruby_rasterizing(folder)
36
+ Object.constants.filter_map do |name|
37
+ file, = Object.const_source_location(name)
38
+ next unless file&.start_with?("#{folder}/")
39
+
40
+ klass = Object.const_get(name)
41
+ name.to_s if Nodes.node?(klass) && klass.method_defined?(:rasterize)
42
+ end.sort
43
+ end
44
+
45
+ # What the node whose directive's on the line, counted from 0, in the file shows behind its text, for an app, and
46
+ # how it formats the characters of its lines, as { painting:, formatting: }, in an Area of the width and height,
47
+ # in pixels, with where the text ends on each of its rows, and a character's width and height, as cell. A node in a
48
+ # grid's cell is by the column its cell starts at, after its |.
49
+ def repaint(path:, line:, column: nil, **space)
50
+ Nodes.load(path)
51
+ node = node_on(path, line, column)
52
+ raise ArgumentError, "Nothing on line #{line + 1} of #{path} rasterizes" unless node
53
+
54
+ metadata = Pages.for(path)
55
+ area = Area.new(**space)
56
+ { painting: painting_of(node, area, metadata), formatting: formatting(node, metadata) }
57
+ end
58
+
59
+ # What the node shows: a Scene, for one of an app's own painters to paint, like weather:'s valley, or else the area
60
+ # it painted, painted again in the milliseconds it gave, if it did.
61
+ def painting_of(node, area, metadata)
62
+ painted = Nodes.call(node, :rasterize, area:, metadata:)
63
+ return painted.to_h if painted.is_a?(Scene)
64
+
65
+ again = painted if painted.is_a?(Numeric)
66
+ area.to_h(again)
67
+ end
68
+
69
+ # The characters of a node's lines to dim, to embolden and to draw as large as a line is tall, as { quiet:, bold:,
70
+ # large: }, each [line, from, to], its lines counted from the first it writes, from its formatting, or none.
71
+ def formatting(node, metadata)
72
+ return {} unless node.respond_to?(:formatting)
73
+
74
+ node.formatting(metadata:)
75
+ end
76
+
77
+ # The node on the line, read with outdent blocks' lines without their <, as anywhere else, or in the grid cell at
78
+ # the column.
79
+ def node_on(path, line, column)
80
+ content = Utils.read_markdown(path)
81
+ lines = Outdent.without_markers(content).lines
82
+ return cell_node(lines, line, column) if column
83
+
84
+ bare = BareDirectives.enabled?(path:, content:)
85
+ blocks = NodeBlocks.find(lines, front_matter: FrontMatter.parse(content).first, bare:)
86
+ blocks.find { it[:open] == line && it[:node].respond_to?(:rasterize) }&.dig(:node)
87
+ end
88
+
89
+ # The node whose directive starts the grid cell at the column of the line, up to the cell's next |.
90
+ def cell_node(lines, line, column)
91
+ text = lines[line].to_s[column..].to_s.split('|').first.to_s.strip
92
+ return unless GridDirectives.directive?(text)
93
+
94
+ blocks = NodeBlocks.find(["#{text}\n"], bare: true)
95
+ blocks.find { it[:node].respond_to?(:rasterize) }&.dig(:node)
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raindown
4
+ # What a node shows behind its text in an app that draws pixels, from its rasterize, for the app to paint itself,
5
+ # like weather:'s valley: the name of a painter the app has, like valley, and the data it paints, like branches.
6
+ # A node that paints its own pixels paints its area instead.
7
+ Scene = Data.define(:name, :data) do
8
+ def to_h = { painter: name, data: }
9
+ end
10
+ end