Class: Twee2::StoryFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/twee2/story_format.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ StoryFormat

Loads the StoryFormat with the specified name


8
9
10
11
12
13
14
15
# File 'lib/twee2/story_format.rb', line 8

def initialize(name)
  raise(StoryFormatNotFoundException) if !File::exists?(format_file_path = Twee2::buildpath("storyFormats/#{name}/format.js"))
  @name = name
  format_file = File::read(format_file_path)
  format_data = format_file.match(/(["'])source\1 *: *(["']).*?[^\\]\2/)[0]
  format_data_for_json = "\{#{format_data}\}"
  @source = JSON.parse(format_data_for_json)['source']
end

Class Method Details

.known_namesObject

Returns an array containing the known StoryFormat names


23
24
25
# File 'lib/twee2/story_format.rb', line 23

def self.known_names
  Dir.open(Twee2::buildpath('storyFormats')).to_a.sort.reject{|d|d=~/^\./}.map{|f|" * #{f}"}
end

Instance Method Details

#compileObject

Given a story file, injects it into the StoryFormat and returns the HTML results


18
19
20
# File 'lib/twee2/story_format.rb', line 18

def compile
  @source.gsub('{{STORY_NAME}}', Twee2::build_config.story_name).gsub('{{STORY_DATA}}', Twee2::build_config.story_file.xmldata).gsub('{{STORY_FORMAT}}', @name)
end