Class: Twee2::Decompiler

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

Class Method Summary collapse

Class Method Details

.decompile(url) ⇒ Object


10
11
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
# File 'lib/twee2/decompiler.rb', line 10

def self.decompile(url)
  result = ''
  # Load the compiled HTML and sanity-check it
  html = Nokogiri::HTML(open(url))
  raise(DecompilationFailedException, 'tw-storydata not found') unless storydata = html.at_css('tw-storydata')
  # Extract the tw-storydata#name (StoryTitle) and #startnode
  result << "::StoryTitle\n#{storydata[:name].strip}\n\n"
  startnode_pid, startnode_name = storydata[:startnode].strip, nil
  # Extract the custom CSS and Javascript, if applicable
  if (css = storydata.at_css('#twine-user-stylesheet')) && ((css_content = css.content.strip) != '')
    result << "::StoryCSS [stylesheet]\n#{css_content}\n\n"
  end
  if (js = storydata.at_css('#twine-user-script')) && ((js_content = js.content.strip) != '')
    result << "::StoryJS [script]\n#{js.content}\n\n"
  end
  # Extract each passage
  storydata.css('tw-passagedata').each do |passagedata|
    # Check if this is the start passage and record this accordingly
    startnode_name = passagedata[:name] if(startnode_pid == passagedata[:pid])
    # Write the passage out
    result << "::#{passagedata[:name].strip}"
    result << " [#{passagedata[:tags].strip}]" if passagedata[:tags].strip != ''
    result << " <#{passagedata[:position].strip}>" if passagedata[:position].strip != ''
    result << "\n#{tidyup_passagedata(passagedata.content.strip)}\n\n"
  end
  # Write the Twee2 settings out (compatability layer)
  result << "::Twee2Settings [twee2]\n"
  result << "@story_start_name = '#{startnode_name.gsub("'", "\\'")}'\n" if startnode_name
  result << "\n"
  # Return the result
  result
end