Module: Rundoc

Extended by:
Rundoc
Included in:
Rundoc
Defined in:
lib/rundoc.rb,
lib/rundoc/cli.rb,
lib/rundoc/version.rb,
lib/rundoc/document.rb,
lib/rundoc/peg_parser.rb,
lib/rundoc/peg_parser.rb,
lib/rundoc/code_command.rb,
lib/rundoc/code_command/raw.rb,
lib/rundoc/code_command/pipe.rb,
lib/rundoc/context/execution.rb,
lib/rundoc/fenced_code_block.rb,
lib/rundoc/code_command/write.rb,
lib/rundoc/cli_argument_parser.rb,
lib/rundoc/context/after_build.rb,
lib/rundoc/code_command/deferred.rb,
lib/rundoc/code_command/rundoc_command.rb,
lib/rundoc/code_command/no_such_command.rb

Defined Under Namespace

Modules: CodeCommand, Context Classes: CLI, CLIArgumentParser, Document, FencedCodeBlock, PegParser, PegTransformer, UnknownCommand

Constant Summary collapse

VERSION =
"6.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#project_rootObject

Returns the value of attribute project_root.



151
152
153
# File 'lib/rundoc.rb', line 151

def project_root
  @project_root
end

Instance Method Details

#add_ensure_later(dir:, code:, binding:) ⇒ Object



104
105
106
# File 'lib/rundoc.rb', line 104

def add_ensure_later(dir:, code:, binding:)
  ensure_later_blocks << {dir: dir, code: code, binding: binding}
end

#after_build(&block) ⇒ Object



91
92
93
94
# File 'lib/rundoc.rb', line 91

def after_build(&block)
  @after_build_block ||= []
  @after_build_block << block
end

#always_hidden_commandsObject



78
79
80
# File 'lib/rundoc.rb', line 78

def always_hidden_commands
  @always_hidden_commands ||= {}
end

#capture_stdout_stderr(io) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/rundoc.rb', line 126

def capture_stdout_stderr(io)
  old_stdout = $stdout
  old_stderr = $stderr
  $stdout = io
  $stderr = io
  yield
ensure
  $stdout = old_stdout
  $stderr = old_stderr
end

#code_command(keyword) ⇒ Object



64
65
66
# File 'lib/rundoc.rb', line 64

def code_command(keyword)
  user_args[:"#{keyword}"]
end

#code_command_from_keyword(keyword, args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rundoc.rb', line 10

def code_command_from_keyword(keyword, args)
  args_klass = code_command(keyword.to_sym)
  original_args = args&.dup

  if args_klass
    runner_klass = user_args_runner[keyword]

    if args.is_a?(Array) && args.last.is_a?(Hash)
      kwargs = args.pop
      user_args = args_klass.new(*args, **kwargs)
    elsif args.is_a?(Hash)
      user_args = args_klass.new(**args)
    else
      user_args = args_klass.new(*args)
    end
  elsif keyword.start_with?("#")
    args_klass = Rundoc::CodeCommand::CommentArgs
    runner_klass = Rundoc::CodeCommand::CommentRunner
    remainder = keyword.to_s.delete_prefix("#")
    comment_text = [remainder, args].compact.join(" ").strip
    user_args = args_klass.new(comment_text.empty? ? nil : comment_text)
  else
    runner_klass = Rundoc::CodeCommand::NoSuchCommand
    user_args = nil
  end

  deferred = CodeCommand::Deferred.new(
    args_instance: user_args,
    runner_klass: runner_klass,
    always_hidden: always_hidden_commands[keyword] || keyword.start_with?("#")
  )
  deferred.original_args = original_args
  deferred.keyword = keyword
  deferred
rescue ArgumentError => e
  raise ArgumentError, "Wrong method signature for #{keyword} with arguments: #{original_args.inspect}, error:\n #{e.message}"
end

#config {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Rundoc)

    the object that the method was called on



96
97
98
# File 'lib/rundoc.rb', line 96

def config
  yield self
end

#configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Rundoc)

    the object that the method was called on



82
83
84
# File 'lib/rundoc.rb', line 82

def configure(&block)
  yield self
end

#ensure_later_blocksObject



100
101
102
# File 'lib/rundoc.rb', line 100

def ensure_later_blocks
  @ensure_later_blocks ||= []
end

#filter_sensitive(sensitive) ⇒ Object



137
138
139
140
141
# File 'lib/rundoc.rb', line 137

def filter_sensitive(sensitive)
  raise "Expecting #{sensitive} to be a hash" unless sensitive.is_a?(Hash)
  @sensitive ||= {}
  @sensitive.merge!(sensitive)
end

#known_commandsObject



68
69
70
# File 'lib/rundoc.rb', line 68

def known_commands
  user_args.keys
end

#parser_optionsObject



52
53
54
# File 'lib/rundoc.rb', line 52

def parser_options
  @parser_options ||= {}
end

#register_code_command(keyword:, args_klass:, runner_klass:, always_hidden: false) ⇒ Object



72
73
74
75
76
# File 'lib/rundoc.rb', line 72

def register_code_command(keyword:, args_klass:, runner_klass:, always_hidden: false)
  user_args[keyword] = args_klass
  user_args_runner[keyword] = runner_klass
  always_hidden_commands[keyword] = always_hidden
end

#run_after_build(context) ⇒ Object



86
87
88
89
# File 'lib/rundoc.rb', line 86

def run_after_build(context)
  @after_build_block ||= []
  @after_build_block.each { |block| block.call(context) }
end

#run_ensure_later(io:) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rundoc.rb', line 108

def run_ensure_later(io:)
  errors = []
  ensure_later_blocks.each do |block|
    io.puts "Running ensure_later block in #{block[:dir]}:\n#{block[:code]}"
    Dir.chdir(block[:dir]) do
      capture_stdout_stderr(io) do
        eval(block[:code], block[:binding]) # rubocop:disable Security/Eval
      end
    end
  rescue => e
    io.puts "ensure_later block failed in #{block[:dir]}: #{e.message}"
    io.puts e.backtrace.join("\n")
    errors << e
  end
  ensure_later_blocks.clear
  errors
end

#sanitize!(doc) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/rundoc.rb', line 143

def sanitize!(doc)
  return doc if @sensitive.nil?
  @sensitive.each do |sensitive, replace|
    doc.gsub!(sensitive.to_s, replace)
  end
  doc
end

#user_argsObject



60
61
62
# File 'lib/rundoc.rb', line 60

def user_args
  @user_args ||= {}
end

#user_args_runnerObject



56
57
58
# File 'lib/rundoc.rb', line 56

def user_args_runner
  @user_args_runner ||= {}
end

#user_code_runner_klassObject



48
49
50
# File 'lib/rundoc.rb', line 48

def user_code_runner_klass
  @user_code_runner_klass ||= {}
end