Module: Twee2

Defined in:
lib/twee2.rb,
lib/twee2/version.rb,
lib/twee2/decompiler.rb,
lib/twee2/story_file.rb,
lib/twee2/build_config.rb,
lib/twee2/story_format.rb

Defined Under Namespace

Classes: BuildConfig, DecompilationFailedException, Decompiler, StoryFile, StoryFileNotFoundException, StoryFormat, StoryFormatNotFoundException

Constant Summary collapse

DEFAULT_FORMAT =

Constants

'Harlowe'
VERSION =
"0.5.0"

Class Method Summary collapse

Class Method Details

.build(input, output, options = {}) ⇒ Object



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
47
48
49
# File 'lib/twee2.rb', line 12

def self.build(input, output, options = {})
  # Read and parse input file
  begin
    build_config.story_file = StoryFile::new(input)
  rescue StoryFileNotFoundException
    puts "ERROR: story file '#{input}' not found."
    exit
  end
  # Read and parse format file, unless already set (by a Twee2::build_config.story_format call in the story file, for example)
  if !build_config.story_format
    begin
      build_config.story_format = StoryFormat::new(options[:format])
    rescue StoryFormatNotFoundException
      puts "ERROR: story format '#{options[:format]}' not found."
      exit
    end
  end
  # Load story format, if for some reason Twee2::build_config.story_format is set to a string rather than an instance
  if build_config.story_format.is_a?(String)
    new_format = build_config.story_format
    begin
      build_config.story_format = StoryFormat::new(new_format)
    rescue StoryFormatNotFoundException
      puts "ERROR: story format '#{new_format}' not found."
      exit
    end
  end
  # Warn if IFID not specified
  if !build_config.story_ifid_specified
    puts "NOTICE: You haven't specified your IFID. Consider adding to your code -"
    puts "::StoryIFID[twee2]\nTwee2::build_config.story_ifid = '#{build_config.story_ifid}'"
  end
  # Produce output file
  File::open(output, 'w') do |out|
    out.print build_config.story_format.compile
  end
  puts "Done"
end

.build_configObject



25
26
27
# File 'lib/twee2/build_config.rb', line 25

def self.build_config
  BuildConfig::instance
end

.buildpath(path) ⇒ Object



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

def self.buildpath(path)
  File.join(File.dirname(File.expand_path(__FILE__)), "../#{path}")
end

.decompile(url, output) ⇒ Object

Reverse-engineers a Twee2/Twine 2 output HTML file into a Twee2 source file



79
80
81
82
83
84
# File 'lib/twee2.rb', line 79

def self.decompile(url, output)
  File::open(output, 'w') do |out|
    out.print Decompiler::decompile(url)
  end
  puts "Done"
end

.formatsObject



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

def self.formats
  puts "I understand the following output formats:"
  puts StoryFormat.known_names.join("\n")
end

.helpObject



87
88
89
90
# File 'lib/twee2.rb', line 87

def self.help
  puts "Twee2 #{Twee2::VERSION}"
  puts File.read(buildpath('doc/usage.txt'))
end

.version_checkObject



68
69
70
71
72
73
74
75
# File 'lib/twee2.rb', line 68

def self.version_check
  `gem list -r -q twee2` =~ /\((.*)\)/
  puts "   Your version: #{Twee2::VERSION}"
  puts " Latest version: #{$1}"
  if Twee2::VERSION.to_s != $1
    puts "To upgrade, run: gem install twee2"
  end
end

.watch(input, output, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/twee2.rb', line 51

def self.watch(input, output, options = {})
  puts "Compiling #{output}"
  build(input, output, options)
  puts "Watching #{input} and included children"
  watch_files = build_config.story_file.child_story_files
  watch_files.unshift(input)
  FileWatcher.new(watch_files).watch do |filename|
    puts "#{filename} changed. Recompiling #{output}"
    build(input, output, options)
  end
end