Class: Playgroundbook::MarkdownWrapper
- Inherits:
-
Object
- Object
- Playgroundbook::MarkdownWrapper
- Defined in:
- lib/wrapper/markdown_wrapper.rb
Overview
Converts a Markdown file into a Swift Playground Needs to:
- Switch out code from being in a block to being a root element
- Comment out non-code comments
- Embed images inside the Resources Dir
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#playground_contents ⇒ Object
Returns the value of attribute playground_contents.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
- #create_a_playground_wrapper(file_content, images) ⇒ Object
- #generate ⇒ Object
- #get_list_of_images(file_content) ⇒ Object
-
#initialize(source, name) ⇒ MarkdownWrapper
constructor
A new instance of MarkdownWrapper.
- #swap_code_context(file_content) ⇒ Object
Constructor Details
#initialize(source, name) ⇒ MarkdownWrapper
Returns a new instance of MarkdownWrapper.
14 15 16 17 18 |
# File 'lib/wrapper/markdown_wrapper.rb', line 14 def initialize(source, name) self.source = source self.name = name self.playground_contents = File.read(source) end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/wrapper/markdown_wrapper.rb', line 12 def name @name end |
#playground_contents ⇒ Object
Returns the value of attribute playground_contents.
12 13 14 |
# File 'lib/wrapper/markdown_wrapper.rb', line 12 def playground_contents @playground_contents end |
#source ⇒ Object
Returns the value of attribute source.
12 13 14 |
# File 'lib/wrapper/markdown_wrapper.rb', line 12 def source @source end |
Instance Method Details
#create_a_playground_wrapper(file_content, images) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/wrapper/markdown_wrapper.rb', line 37 def create_a_playground_wrapper(file_content, images) folder = File.dirname(source) playground = File.join(folder, name + ".playground") FileUtils.rm_r(playground) if Dir.exist?(playground) Dir.mkdir(playground) xcplayground = <<-XML <?xml version='1.0' encoding='UTF-8' standalone='yes'?> <playground version='5.0' target-platform='osx' display-mode='rendered'> <timeline fileName='timeline.xctimeline'/> </playground> XML timeline = <<-XML <?xml version="1.0" encoding="UTF-8"?> <Timeline version = "3.0"> <TimelineItems> </TimelineItems> </Timeline> XML File.write(File.join(playground, "contents.xcplayground"), xcplayground) File.write(File.join(playground, "timeline.xctimeline"), timeline) resources = File.join(playground, "Resources") Dir.mkdir(resources) images.each do |image| outer_image_path = File.join(folder, image) local = File.exist? outer_image_path remote = image.include? "http" if local basedir = File.dirname(image) inner_basedir = File.join(resources, basedir) Dir.mkdir(inner_basedir) unless Dir.exist?(inner_basedir) FileUtils.cp(outer_image_path, inner_basedir) elsif remote file = File.open(File.join(resources, File.basename(image)), "wb") file.write(open(image, "rb").read) file_content.gsub!(image, File.basename(image)) end end File.write(File.join(playground, "Contents.swift"), file_content) end |
#generate ⇒ Object
20 21 22 23 24 |
# File 'lib/wrapper/markdown_wrapper.rb', line 20 def generate contents = swap_code_context(playground_contents) images = get_list_of_images(contents) create_a_playground_wrapper(contents, images) end |
#get_list_of_images(file_content) ⇒ Object
33 34 35 |
# File 'lib/wrapper/markdown_wrapper.rb', line 33 def get_list_of_images(file_content) file_content.scan(/\!\[.*\]\((.*?)\)/).flatten end |
#swap_code_context(file_content) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/wrapper/markdown_wrapper.rb', line 26 def swap_code_context(file_content) prefix = "import Foundation\n/*:\n" suffix = "*/" content = file_content.gsub("```swift", "*/\n").gsub("```", "/*:") prefix + content + suffix end |