Class: Playground::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/playground/generator.rb

Instance Method Summary collapse

Instance Method Details

#generate(input, output) ⇒ Object



9
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/playground/generator.rb', line 9

def generate(input, output)
  @input = input
  @output = output

  # Remove anything already there
  FileUtils.rm_rf(@output)

  # Make folders
  @doc_dir = @output + '/Documentation'
  FileUtils.mkdir_p(@doc_dir)

  # Copy stylesheet
  @data_dir = File.expand_path('../data', __FILE__)
  FileUtils.cp(@data_dir + '/style.css', @doc_dir)

  # Setup
  section = 1
  @contents_sections = []
  html = ''

  # Parse markdown
  chunks = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new).render(File.read(input)).split("\n\n")

  # Loop through chunks
  chunks.each do |chunk|
    # Code
    if chunk.start_with?('<pre><code>')
      if html.length > 0
        write_html(html, section)
        section += 1
      end

      swift = CGI.unescapeHTML(chunk[11, chunk.length - 11 - 13])
      write_swift(swift, section)
      section += 1
      html = ''
    # HTML
    else
      html << chunk + "\n\n"
    end
  end

  # Write any remaining HTML
  if html.length > 0
    write_html(html, section)
  end

  # Create contents
  contents = erb(@data_dir + '/contents.xcplayground.erb', { sdk: 'macosx', sections: @contents_sections.join("\n    ") })
  File.write(output + '/contents.xcplayground', contents)
end

#generate_empty(path, platform) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/playground/generator.rb', line 80

def generate_empty(path, platform)
  if path.nil?
    # Create playground using current date as name
    time = Time.now.strftime "%Y%m%d-%H%M%S"
    path = "/tmp/playgrounds/#{time}.playground"
  end
  # Add extension if not provided
  path += ".playground" unless /.playground$/.match(path)
  FileUtils.mkdir_p(path)
  # Minimum xml so that Xcode recognizes it as a valid playground
  contents_xcplayground = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><playground version='1.0' sdk='#{platform}'><sections><code source-file-name='section-1.swift'/></sections></playground>"
  File.open(path + "/contents.xcplayground", 'w') {|f| f.write(contents_xcplayground) }
  
  # Create the swift file
  section1_swift = "// Playground - noun: a place where people can play\n\nimport #{platform=='macosx' ? 'Cocoa' : 'UIKit'}\n\nvar str = \"Hello, playground\""
  File.open(path + "/section-1.swift", 'w') {|f| f.write(section1_swift) }
  
  # Open with Xcode
  if !system("open \"#{path}\" 2>/dev/null")
    puts "There was an error opening #{path}"
  end
end

#write_html(html, section) ⇒ Object

Write an HTML file to the output package and add its metadata to the contents



62
63
64
65
66
67
68
69
# File 'lib/playground/generator.rb', line 62

def write_html(html, section)
  filename = "section-#{section.to_s.rjust(3, '0')}.html"
  @contents_sections << %Q{<documentation relative-path="#{filename}"/>}

  html = html[0, html.length - 2]
  html = erb(@data_dir + '/section.html.erb', { title: filename, content: html })
  File.write(@doc_dir + "/#{filename}", html)
end

#write_swift(swift, section) ⇒ Object

Write a Swift file to the output package and add its metadata to the contents



72
73
74
75
76
77
# File 'lib/playground/generator.rb', line 72

def write_swift(swift, section)
  filename = "section-#{section.to_s.rjust(3, '0')}.swift"
  @contents_sections << %Q{<code source-file-name="#{filename}"/>}

  File.write(@output + "/#{filename}", swift)
end