Class: Playgroundbook::Renderer
- Inherits:
-
AbstractLinter
- Object
- AbstractLinter
- Playgroundbook::Renderer
- Defined in:
- lib/playgroundbook_renderer/playgroundbook_renderer.rb
Overview
A renderer for playground books.
Instance Attribute Summary collapse
-
#chapter_collator ⇒ Object
Returns the value of attribute chapter_collator.
-
#contents_manifest_generator ⇒ Object
Returns the value of attribute contents_manifest_generator.
-
#glossary_generator ⇒ Object
Returns the value of attribute glossary_generator.
-
#page_parser ⇒ Object
Returns the value of attribute page_parser.
-
#ui ⇒ Object
Returns the value of attribute ui.
-
#yaml_file_name ⇒ Object
Returns the value of attribute yaml_file_name.
Instance Method Summary collapse
-
#initialize(yaml_file_name, contents_manifest_generator = ContentsManifestGenerator.new, page_parser = PageParser.new, chapter_collator = ChapterCollator.new, glossary_generator = GlossaryGenerator.new, ui = Cork::Board.new) ⇒ Renderer
constructor
A new instance of Renderer.
- #render! ⇒ Object
- #yaml_contents ⇒ Object
Methods inherited from AbstractLinter
Constructor Details
#initialize(yaml_file_name, contents_manifest_generator = ContentsManifestGenerator.new, page_parser = PageParser.new, chapter_collator = ChapterCollator.new, glossary_generator = GlossaryGenerator.new, ui = Cork::Board.new) ⇒ Renderer
Returns a new instance of Renderer.
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/playgroundbook_renderer/playgroundbook_renderer.rb', line 23 def initialize(yaml_file_name, contents_manifest_generator = ContentsManifestGenerator.new, page_parser = PageParser.new, chapter_collator = ChapterCollator.new, glossary_generator = GlossaryGenerator.new, ui = Cork::Board.new) @yaml_file_name = yaml_file_name @contents_manifest_generator = contents_manifest_generator @page_parser = page_parser @chapter_collator = chapter_collator @glossary_generator = glossary_generator @ui = ui end |
Instance Attribute Details
#chapter_collator ⇒ Object
Returns the value of attribute chapter_collator.
19 20 21 |
# File 'lib/playgroundbook_renderer/playgroundbook_renderer.rb', line 19 def chapter_collator @chapter_collator end |
#contents_manifest_generator ⇒ Object
Returns the value of attribute contents_manifest_generator.
17 18 19 |
# File 'lib/playgroundbook_renderer/playgroundbook_renderer.rb', line 17 def contents_manifest_generator @contents_manifest_generator end |
#glossary_generator ⇒ Object
Returns the value of attribute glossary_generator.
20 21 22 |
# File 'lib/playgroundbook_renderer/playgroundbook_renderer.rb', line 20 def glossary_generator @glossary_generator end |
#page_parser ⇒ Object
Returns the value of attribute page_parser.
18 19 20 |
# File 'lib/playgroundbook_renderer/playgroundbook_renderer.rb', line 18 def page_parser @page_parser end |
#ui ⇒ Object
Returns the value of attribute ui.
21 22 23 |
# File 'lib/playgroundbook_renderer/playgroundbook_renderer.rb', line 21 def ui @ui end |
#yaml_file_name ⇒ Object
Returns the value of attribute yaml_file_name.
16 17 18 |
# File 'lib/playgroundbook_renderer/playgroundbook_renderer.rb', line 16 def yaml_file_name @yaml_file_name end |
Instance Method Details
#render! ⇒ 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 |
# File 'lib/playgroundbook_renderer/playgroundbook_renderer.rb', line 37 def render! ui.puts "Rendering #{yaml_file_name.green}..." book = yaml_contents book_dir_name = "#{book['name']}.playgroundbook" book_chapter_contents = [] # TODO: Validate YAML contents? begin book_chapter_contents = book['chapters'].map do |chapter| File.read("#{chapter}.playground/Contents.swift") end rescue => e ui.puts 'Failed to open playground Contents.swift file.' raise e end parsed_chapters = book_chapter_contents.map { |c| page_parser.parse_chapter_pages(c) } Dir.mkdir(book_dir_name) unless Dir.exist?(book_dir_name) Dir.chdir(book_dir_name) do Dir.mkdir(ContentsDirectoryName) unless Dir.exist?(ContentsDirectoryName) Dir.chdir(ContentsDirectoryName) do Dir.mkdir(ResourcesDirectoryName) unless Dir.exist?(ResourcesDirectoryName) # Always create a Resources dir, even if empty. resources_dir = book['resources'] if !(resources_dir.nil? || resources_dir.empty?) @ui.puts "Copying resource directory (#{resources_dir.green}) contents." Dir.glob("../../#{resources_dir}/*").each do |file| FileUtils.cp(file, ResourcesDirectoryName) end end @contents_manifest_generator.generate!(book) Dir.mkdir(ChaptersDirName) unless Dir.exist?(ChaptersDirName) Dir.chdir(ChaptersDirName) do # Chapter file name becomes chapter name in playground book. book['chapters'].each_with_index do |chapter_file_name, index| parsed_chapter = parsed_chapters[index] @chapter_collator.collate!(chapter_file_name, parsed_chapter, book['imports'] || ['UIKit']) end end end unless book['glossary'].nil? @ui.puts 'Generating glossary.' @glossary_generator.generate!(parsed_chapters, book['chapters'], book['glossary']) end end end |
#yaml_contents ⇒ Object
85 86 87 |
# File 'lib/playgroundbook_renderer/playgroundbook_renderer.rb', line 85 def yaml_contents YAML.load(File.open(@yaml_file_name)) end |