Sha256: 7a8647b28bb4ab30954553d7a81433a2a72999cf4c252d2c668def0da0aa821a

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

module Twee2
  class StoryFormatNotFoundException < Exception; end

  class StoryFormat
    # Loads the StoryFormat with the specified name
    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

    # Given a story file, injects it into the StoryFormat and returns the HTML results
    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

    # Returns an array containing the known StoryFormat names
    def self.known_names
      Dir.open(Twee2::buildpath('storyFormats')).to_a.sort.reject{|d|d=~/^\./}.map{|f|" * #{f}"}
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
twee2-0.3.1 lib/twee2/story_format.rb
twee2-0.3.0 lib/twee2/story_format.rb