Skip to content

Commit 262acf4

Browse files
committed
Drop support for specifying paths as plain strings
This simplifies the implementation code and removes any confusion between single line content and a file path.
1 parent 5d7ec0f commit 262acf4

File tree

7 files changed

+33
-42
lines changed

7 files changed

+33
-42
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,10 @@ There are two section types you can use to construct a playground in Ruby:
118118

119119
#### `DocumentationSection`
120120

121-
These contain HTML that is rendered within the playground. You can construct a `DocumentationSection` with either a path to an HTML file or the raw HTML content itself (either as a String or an IO object):
121+
These contain HTML that is rendered within the playground. You can construct a `DocumentationSection` with either an IO object or a string representing the contents of the HTML file:
122122

123123
```ruby
124124
# All of the following are valid values for content:
125-
content = '/path/to/file.html'
126125
content = Pathname.new('/path/to/file.html')
127126
content = File.open('/path/to/file.html')
128127
content = <<-HTML
@@ -145,11 +144,10 @@ The content you provide _must_ be an HTML fragment - if a `<html>`, `<head>` or
145144

146145
#### `CodeSection`
147146

148-
These contain the executable swift code, and each playground must contain at least one of these sections. Constructing these sections is the same as `DocumentationSection` - you can use either a path to a swift file, or the raw swift code itself (either as a String or an IO object):
147+
These contain the executable swift code, and each playground must contain at least one of these sections. Constructing these sections is the same as `DocumentationSection` - you can use either an IO object or a string representing the contents of the swift file:
149148

150149
```ruby
151150
# All of the following are valid values for content:
152-
content = '/path/to/file.swift'
153151
content = Pathname.new('/path/to/file.swift')
154152
content = File.open('/path/to/file.swift')
155153
content = <<-SWIFT

lib/swift/playground/asset.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Playground
55
autoload :Javascript, assets_path.join('javascript')
66

77
class Asset
8-
include Util::PathOrContent
8+
include Util::SourceIO
99

1010
class << self
1111
protected
@@ -19,7 +19,7 @@ def default_filename(filename = nil)
1919
attr_accessor :content
2020

2121
def initialize(content, options = {})
22-
pathname_or_content = path_or_content_as_io(content)
22+
pathname_or_content = source_as_io(content)
2323
self.content = pathname_or_content.read
2424

2525
filename = options[:filename] || derived_filename(pathname_or_content)

lib/swift/playground/generator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ module Swift
44
class Playground
55
class Generator
66
class << self
7-
include Util::PathOrContent
7+
include Util::SourceIO
88

99
def generate(markdown, options={})
10-
markdown_file = path_or_content_as_io(markdown)
10+
markdown_file = source_as_io(markdown)
1111

1212
playground = Playground.new
1313

lib/swift/playground/section.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Playground
77
autoload :CodeSection, sections_path.join('code_section')
88

99
class Section
10-
include Util::PathOrContent
10+
include Util::SourceIO
1111

1212
class TemplateContext
1313
attr_accessor :content, :number
@@ -68,7 +68,7 @@ def xcplayground(options = nil)
6868
end
6969

7070
def initialize(content)
71-
@content = path_or_content_as_io(content).read
71+
@content = source_as_io(content).read
7272
@content.freeze
7373
end
7474

lib/swift/playground/util.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
require_relative 'util/syntax_highlighting'
22
require_relative 'util/pipeline'
3-
require_relative 'util/path_or_content'
3+
require_relative 'util/source_io'

lib/swift/playground/util/path_or_content.rb

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'pathname'
2+
3+
module Swift::Playground::Util
4+
module SourceIO
5+
def source_as_io(source)
6+
# Return path_or_content if it is an IO-like object
7+
return source if source.respond_to?(:read)
8+
9+
unless source.is_a?(String)
10+
raise "You must provide either a String or an IO object when constructing a #{self.class.name}."
11+
end
12+
13+
StringIO.new(source)
14+
end
15+
16+
def derived_filename(source)
17+
if source.respond_to?(:basename)
18+
source.basename.to_s
19+
elsif source.respond_to?(:path)
20+
File.basename(source.path)
21+
end
22+
end
23+
end
24+
end

0 commit comments

Comments
 (0)