Module: HtmlToMarkdown::CLIProxy

Defined in:
lib/html_to_markdown/cli_proxy.rb

Defined Under Namespace

Classes: CLIExecutionError, Error, MissingBinaryError

Class Method Summary collapse

Class Method Details

.call(argv) ⇒ Object

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/html_to_markdown/cli_proxy.rb', line 26

def call(argv)
  binary = find_cli_binary
  args = Array(argv).map(&:to_s)
  stdout, stderr, status = Open3.capture3(binary.to_s, *args)
  return stdout if status.success?

  raise CLIExecutionError.new(
    "html-to-markdown CLI exited with status #{status.exitstatus}",
    stderr: stderr,
    status: status.exitstatus
  )
end

.find_cli_binaryObject

Raises:



39
40
41
42
43
44
45
# File 'lib/html_to_markdown/cli_proxy.rb', line 39

def find_cli_binary
  binary_name = Gem.win_platform? ? 'html-to-markdown.exe' : 'html-to-markdown'
  found = search_paths(binary_name).find(&:file?)
  return found if found

  raise MissingBinaryError, missing_binary_message
end

.lib_pathObject



51
52
53
# File 'lib/html_to_markdown/cli_proxy.rb', line 51

def lib_path
  @lib_path ||= Pathname(__dir__.to_s).join('..').expand_path
end

.missing_binary_messageObject



67
68
69
70
71
72
# File 'lib/html_to_markdown/cli_proxy.rb', line 67

def missing_binary_message
  <<~MSG.strip
    html-to-markdown CLI binary not found. Build it with
    `cargo build --release --package html-to-markdown-cli`.
  MSG
end

.root_pathObject



47
48
49
# File 'lib/html_to_markdown/cli_proxy.rb', line 47

def root_path
  @root_path ||= Pathname(__dir__.to_s).join('../..').expand_path
end

.search_paths(binary_name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/html_to_markdown/cli_proxy.rb', line 55

def search_paths(binary_name)
  paths = [
    root_path.join('target', 'release', binary_name),
    lib_path.join('bin', binary_name),
    lib_path.join(binary_name)
  ]

  workspace_root = root_path.parent&.parent
  paths << workspace_root.join('target', 'release', binary_name) if workspace_root
  paths
end