Class: XRT::Command::Extract
- Inherits:
-
Object
- Object
- XRT::Command::Extract
- Defined in:
- lib/xrt/commands.rb
Instance Method Summary collapse
-
#execute(from_file, target_block, templates_directory, to_file_name) ⇒ Object
xrt extract templates/blogs/index.html ‘[% IF pager’ ‘templates/’ ‘blogs/_pager.tt’.
Instance Method Details
#execute(from_file, target_block, templates_directory, to_file_name) ⇒ Object
xrt extract templates/blogs/index.html ‘[% IF pager’ ‘templates/’ ‘blogs/_pager.tt’
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/xrt/commands.rb', line 26 def execute(from_file, target_block, templates_directory, to_file_name) from_source = open(from_file).read parser = XRT::Parser.new(from_source) from_doc = parser.document found_blocks = from_doc.find_blocks.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 open(from_file, 'w'){|f| f.write content_to_overwrite } new_file = Pathname(templates_directory).join(to_file_name) if new_file.exist? raise 'TO_FILE_NAME exists.' end open(new_file, 'w'){|f| f.puts content_for_new_file } true end |