Skip to content

Commit

Permalink
Merge pull request #22 from ashfurrow/flight
Browse files Browse the repository at this point in the history
Removes extraneous newlines.
  • Loading branch information
rhysforyou committed Sep 5, 2016
2 parents f1b230c + f1ce684 commit d9a3d8f
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Current Master

- Nothing yet.
- Removes newlines surrounding markdown blocks. See [#20](https://github.com/ashfurrow/playgroundbook/issues/20).

# 0.2.1

Expand Down
15 changes: 15 additions & 0 deletions lib/playgroundbook_renderer/page_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Playgroundbook
class PageProcessor
def strip_extraneous_newlines(page_contents)
# Three cases we need to look for:
# - Extraneous newlines before /*:
# - Extraneous newlines after */
# - Extraneous newlines either before or after //:
page_contents
.gsub(/\n+\/\*:/, "\n/*:")
.gsub(/\*\/\n+/, "*/\n")
.split(/(\/\/:.*$)\n*/).join("\n") # Important to do this before the next line, because it adds newlines before //: comments.
.gsub(/\n+\/\/:/, "\n//:")
end
end
end
7 changes: 4 additions & 3 deletions lib/playgroundbook_renderer/page_writer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require 'plist'
require 'playgroundbook_renderer/page_processor'

module Playgroundbook
class PageWriter

def initialize(ui = Cork::Board.new)
def initialize(page_processor = PageProcessor.new, ui = Cork::Board.new)
@page_processor = page_processor
@ui = ui
end

Expand All @@ -13,7 +14,7 @@ def write_page!(page_name, page_dir_name, imports, page_contents)
contents_with_import = "//#-hidden-code\n"
contents_with_import += imports.map { |i| "import #{i}" }.join("\n") + "\n"
contents_with_import += "//#-end-hidden-code\n"
contents_with_import += page_contents
contents_with_import += @page_processor.strip_extraneous_newlines(page_contents)

Dir.chdir(page_dir_name) do
File.open(ContentsSwiftFileName, 'w') do |file|
Expand Down
86 changes: 86 additions & 0 deletions spec/playgroundbook_renderer_spec/page_processor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
require File.expand_path('../../spec_helper', __FILE__)

module Playgroundbook
describe PageProcessor do
let(:page_processor) { PageProcessor.new }

it 'removes newlines before markdown blocks' do
page_contents = <<-EOS
let a = 6
/*:
Some markdown.
*/
EOS
processed_page_contents = <<-EOS
let a = 6
/*:
Some markdown.
*/
EOS

expect(page_processor.strip_extraneous_newlines(page_contents)).to eq(processed_page_contents)
end

it 'removes newlines after markdown blocks' do
page_contents = <<-EOS
/*:
Some markdown.
*/
let a = 6
EOS
processed_page_contents = <<-EOS
/*:
Some markdown.
*/
let a = 6
EOS

expect(page_processor.strip_extraneous_newlines(page_contents)).to eq(processed_page_contents)
end

it 'removes newlines surrounding single-line markdown blocks' do
page_contents = <<-EOS
let a = 6
//: Some markdown.
let b = a
EOS
processed_page_contents = <<-EOS
let a = 6
//: Some markdown.
let b = a
EOS

expect(page_processor.strip_extraneous_newlines(page_contents)).to eq(processed_page_contents)
end

it 'does not strip newlines from code' do
page_contents = <<-EOS
let a = 6
let b = a
EOS

expect(page_processor.strip_extraneous_newlines(page_contents)).to eq(page_contents)
end

it 'it does not strip newlines from the markdown' do
page_contents = <<-EOS
/*:
# Header
Some markdown. The following lines are purposefull left blank.
*/
EOS

expect(page_processor.strip_extraneous_newlines(page_contents)).to eq(page_contents)
end
end
end
15 changes: 14 additions & 1 deletion spec/playgroundbook_renderer_spec/page_writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
module Playgroundbook
describe PageWriter do
include FakeFS::SpecHelpers
let(:page_writer) { PageWriter.new(test_ui) }
let(:page_writer) { PageWriter.new(page_processor, test_ui) }
let(:page_processor) { double(PageProcessor) }
let(:test_ui) { Cork::Board.new(silent: true) }
let(:page_name) { 'test page name' }
let(:page_dir_name) { 'test page name.playgroundpage' }
let(:page_contents) { "// Some swift goes here." }
let(:generated_page_contesnts) { "//#-hidden-code\nimport UIKit\n//#-end-hidden-code\n// Some swift goes here." }

before do
allow(page_processor).to receive(:strip_extraneous_newlines) do |page_contents|
# Returns the parameter, unprocessed.
page_contents
end
end

context 'with a pre-existing page directory' do
before do
Dir.mkdir(page_dir_name)
Expand All @@ -20,6 +28,11 @@ module Playgroundbook
end
end

it 'calls the page processor' do
expect(page_processor).to receive(:strip_extraneous_newlines)
page_writer.write_page!(page_name, page_dir_name, ['UIKit'], page_contents)
end

context 'as a consequence of writing rendering' do
before do
page_writer.write_page!(page_name, page_dir_name, ['UIKit'], page_contents)
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
require 'playgroundbook_renderer/contents_manifest_generator'
require 'playgroundbook_renderer/chapter_collator'
require 'playgroundbook_renderer/page_writer'
require 'playgroundbook_renderer/page_processor'

RSpec.configure do |config|
config.color = true
Expand Down

0 comments on commit d9a3d8f

Please sign in to comment.