Skip to content

Commit

Permalink
Screenshot path is now relative.
Browse files Browse the repository at this point in the history
  • Loading branch information
schneems committed Jul 10, 2024
1 parent 9655bb5 commit 8107148
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
27 changes: 15 additions & 12 deletions lib/rundoc/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ def build(path:)

raise "#{@path} does not exist" unless File.exist?(@path)
raise "Expecting #{@path} to be a rundoc markdown file" unless File.file?(@path)
@working_dir = Pathname.new(File.expand_path("../", @path))
tmp_dir = @working_dir.join("tmp")

root_rundoc_dir = Pathname.new(File.expand_path("../", @path))

tmp_output_dir = root_rundoc_dir.join("tmp")
screenshots_path = if Rundoc.project_root
tmp_dir.join(Rundoc.project_root, "screenshots")
tmp_output_dir.join(Rundoc.project_root, "screenshots")
else
tmp_dir.join("screenshots")
tmp_output_dir.join("screenshots")
end

tmp_dir.join("screenshots")
tmp_output_dir.join("screenshots")

dot_env_path = File.expand_path("../.env", @path)
if File.exist?(dot_env_path)
Expand All @@ -27,8 +29,8 @@ def build(path:)

source_contents = File.read(@path)

FileUtils.remove_entry_secure(tmp_dir) if tmp_dir.exist?
tmp_dir.mkdir
FileUtils.remove_entry_secure(tmp_output_dir) if tmp_output_dir.exist?
tmp_output_dir.mkdir
banner = <<~HEREDOC
<!-- STOP
This file was generated by a rundoc script, do not modify it.
Expand All @@ -40,9 +42,10 @@ def build(path:)
HEREDOC

puts "== Running your docs"
Dir.chdir(tmp_dir) do
Dir.chdir(tmp_output_dir) do
@output = Rundoc::Parser.new(
source_contents,
output_dir: tmp_output_dir,
document_path: @path,
screenshots_path: screenshots_path
).to_md
Expand All @@ -57,19 +60,19 @@ def build(path:)
"project"
end

project_dir = @working_dir.join(project_name)
project_dir = root_rundoc_dir.join(project_name)

FileUtils.remove_entry_secure(project_dir) if project_dir.exist?

cp_root = if Rundoc.project_root
tmp_dir.join(Rundoc.project_root, ".")
tmp_output_dir.join(Rundoc.project_root, ".")
else
tmp_dir.join(".")
tmp_output_dir.join(".")
end

FileUtils.cp_r(cp_root, project_dir)

FileUtils.remove_entry_secure(tmp_dir) if tmp_dir.exist?
FileUtils.remove_entry_secure(tmp_output_dir) if tmp_output_dir.exist?

source_path = project_dir.join("README.md")
puts "== Done, writing original source to #{source_path}"
Expand Down
7 changes: 6 additions & 1 deletion lib/rundoc/code_command/rundoc/require.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ def call(env = {})
document_path = @path.expand_path(current_path)

puts "rundoc.require: Start executing #{@path.to_s.inspect}"
output = Rundoc::Parser.new(document_path.read, document_path: document_path.to_s, screenshots_path: env[:screenshots_path]).to_md
output = Rundoc::Parser.new(
document_path.read,
output_dir: env[:output_dir],
document_path: document_path.to_s,
screenshots_path: env[:screenshots_path]
).to_md
puts "rundoc.require: Done executing #{@path.to_s.inspect}, putting contents into document"

env[:replace] << output
Expand Down
4 changes: 3 additions & 1 deletion lib/rundoc/code_command/website/screenshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def to_md(env = {})
def call(env = {})
puts "Taking screenshot: #{@driver.current_url}"
filename = @driver.screenshot(upload: @upload, screenshots_path: env[:screenshots_path])
env[:replace] = "![Screenshot of #{@driver.current_url}](#{filename})"

relative_filename = filename.relative_path_from(env[:output_dir])
env[:replace] = "![Screenshot of #{@driver.current_url}](#{relative_filename})"
""
end

Expand Down
2 changes: 2 additions & 0 deletions lib/rundoc/code_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def initialize(match, options = {})
@commands = []
@stack = []
@keyword = options[:keyword] or raise "keyword is required"
@output_dir = options[:output_dir]
@screenshots_path = options[:screenshots_path]
@document_path = options[:document_path]
@fence = match[:fence]
Expand All @@ -49,6 +50,7 @@ def render
env[:fence_end] = "#{fence}#{AUTOGEN_WARNING}"
env[:before] = []
env[:after] = []
env[:output_dir] = @output_dir
env[:document_path] = @document_path
env[:screenshots_path] = @screenshots_path

Expand Down
11 changes: 9 additions & 2 deletions lib/rundoc/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class Parser

attr_reader :contents, :keyword, :stack

def initialize(contents, screenshots_path:, keyword: DEFAULT_KEYWORD, document_path: nil)
def initialize(contents, output_dir:, screenshots_path:, keyword: DEFAULT_KEYWORD, document_path: nil)
@output_dir = output_dir
@document_path = document_path
@screenshots_path = screenshots_path
@contents = contents
Expand Down Expand Up @@ -42,7 +43,13 @@ def partition
@stack << head unless head.empty?
unless code.empty?
match = code.match(CODEBLOCK_REGEX)
@stack << CodeSection.new(match, keyword: keyword, document_path: @document_path, screenshots_path: @screenshots_path)
@stack << CodeSection.new(
match,
keyword: keyword,
output_dir: @output_dir,
document_path: @document_path,
screenshots_path: @screenshots_path
)
end
@contents = tail
end
Expand Down

0 comments on commit 8107148

Please sign in to comment.