lib/twee2/story_file.rb in twee2-0.4.2 vs lib/twee2/story_file.rb in twee2-0.5.0

- old
+ new

@@ -7,10 +7,11 @@ module Twee2 class StoryFileNotFoundException < Exception; end class StoryFile attr_accessor :passages + attr_reader :child_story_files HAML_OPTIONS = { remove_whitespace: true } Tilt::CoffeeScriptTemplate.default_bare = true # bare mode for HAML :coffeescript blocks @@ -20,10 +21,12 @@ # Loads the StoryFile with the given name def initialize(filename) raise(StoryFileNotFoundException) if !File::exists?(filename) @passages, current_passage = {}, nil + @child_story_files = [] + # Load file into memory to begin with lines = File::read(filename).split(/\r?\n/) # First pass - go through and perform 'includes' i, in_story_includes_section = 0, false while i < lines.length @@ -31,13 +34,15 @@ if line =~ /^:: *StoryIncludes */ in_story_includes_section = true elsif line =~ /^::/ in_story_includes_section = false elsif in_story_includes_section && (line.strip != '') + child_file = line.strip # include a file here because we're in the StoryIncludes section - if File::exists?(line.strip) - lines.push(*File::read(line.strip).split(/\r?\n/)) # add it on to the end + if File::exists?(child_file) + lines.push(*File::read(child_file).split(/\r?\n/)) # add it on to the end + child_story_files.push(child_file) else puts "WARNING: tried to include file '#{line.strip}' via StoryIncludes but file was not found." end elsif line =~ /^( *)::@include (.*)$/ # include a file here because an @include directive was spotted @@ -61,11 +66,11 @@ end @passages.each_key{|k| @passages[k][:content].strip!} # Strip excessive trailing whitespace # Run each passage through a preprocessor, if required run_preprocessors # Extract 'special' passages and mark them as not being included in output - story_css, story_js, pid, @story_start_pid, @story_start_name = '', '', 0, nil, 'Start' + story_css, pid, @story_js, @story_start_pid, @story_start_name = '', 0, '', nil, 'Start' @passages.each_key do |k| if k == 'StoryTitle' Twee2::build_config.story_name = @passages[k][:content] @passages[k][:exclude_from_output] = true elsif k == 'StoryIncludes' @@ -75,11 +80,11 @@ @passages[k][:exclude_from_output] = true elsif @passages[k][:tags].include? 'stylesheet' story_css << "#{@passages[k][:content]}\n" @passages[k][:exclude_from_output] = true elsif @passages[k][:tags].include? 'script' - story_js << "#{@passages[k][:content]}\n" + @story_js << "#{@passages[k][:content]}\n" @passages[k][:exclude_from_output] = true elsif @passages[k][:tags].include? 'twee2' eval @passages[k][:content] @passages[k][:exclude_from_output] = true else @@ -98,21 +103,22 @@ ifid: Twee2::build_config.story_ifid, format: '{{STORY_FORMAT}}', options: '' }) do @story_data.style(story_css, role: 'stylesheet', id: 'twine-user-stylesheet', type: 'text/twine-css') - @story_data.script(story_js, role: 'script', id: 'twine-user-script', type: 'text/twine-javascript') + @story_data.script('{{STORY_JS}}', role: 'script', id: 'twine-user-script', type: 'text/twine-javascript') @passages.each do |k,v| unless v[:exclude_from_output] @story_data.tag!('tw-passagedata', { pid: v[:pid], name: k, tags: v[:tags].join(' '), position: v[:position] }, v[:content]) end end end end # Returns the rendered XML that represents this story def xmldata - @story_data.target! + data = @story_data.target! + data.gsub('{{STORY_JS}}', @story_js) end # Runs HAML, Coffeescript etc. preprocessors across each applicable passage def run_preprocessors @passages.each_key do |k| \ No newline at end of file