Class: XRT::Command::Extract

Inherits:
Object
  • Object
show all
Defined in:
lib/xrt/command/extract.rb

Instance Method Summary collapse

Instance Method Details

#as_transaction(from_file, target_block, templates_directory, to_file_name) ⇒ Object



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
47
48
# File 'lib/xrt/command/extract.rb', line 16

def as_transaction(from_file, target_block, templates_directory, to_file_name)
  transaction = XRT::Transaction.new
  from_source = open(from_file).read
  parser = XRT::Parser.new(from_source)
  from_doc = parser.document

  found_blocks = from_doc.find_block_texts.select{|block|
    block.content.index(target_block) == 0
  }

  if found_blocks.length == 0
    raise "target_block not found"
  end

  if found_blocks.length > 1
    raise "ambiguous target_block"
  end

  found_block = found_blocks.first

  replace_to_node = XRT::Parser.new(%Q{[% INCLUDE "#{to_file_name}" %]}).document

  from_doc.replace_child(replace_to_node, found_block)

  content_to_overwrite = from_doc.content
  content_for_new_file = found_block.auto_indent + "\n"

  transaction.edit from_file, content_to_overwrite

  transaction.new_file transaction.full_path(templates_directory, to_file_name), content_for_new_file

  transaction
end

#execute(from_file, target_block, templates_directory, to_file_name) ⇒ Object

xrt extract templates/blogs/index.html ‘[% IF pager’ ‘templates/’ ‘blogs/_pager.tt’



10
11
12
13
14
# File 'lib/xrt/command/extract.rb', line 10

def execute(from_file, target_block, templates_directory, to_file_name)
  transaction = as_transaction(from_file, target_block, templates_directory, to_file_name)
  transaction.commit!
  true
end