Skip to content

Remove special chars from dirnames #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/rspec_api_documentation/views/markup_example.rb
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@
module RspecApiDocumentation
module Views
class MarkupExample < Mustache
SPECIAL_CHARS = /[<>:"\/\\|?*]/.freeze

def initialize(example, configuration)
@example = example
@host = configuration.curl_host
@@ -19,12 +21,11 @@ def respond_to?(method, include_private = false)
end

def dirname
resource_name.to_s.downcase.gsub(/\s+/, '_').gsub(":", "_")
sanitize(resource_name.to_s.downcase)
end

def filename
special_chars = /[<>:"\/\\|?*]/
basename = description.downcase.gsub(/\s+/, '_').gsub(special_chars, '')
basename = sanitize(description.downcase)
basename = Digest::MD5.new.update(description).to_s if basename.blank?
"#{basename}.#{extension}"
end
@@ -87,6 +88,10 @@ def format_scope(unformatted_scope)
def content_type(headers)
headers && headers.fetch("Content-Type", nil)
end

def sanitize(name)
name.gsub(/\s+/, '_').gsub(SPECIAL_CHARS, '')
end
end
end
end
8 changes: 8 additions & 0 deletions spec/views/html_example_spec.rb
Original file line number Diff line number Diff line change
@@ -28,6 +28,14 @@
end
end

context "when resource name contains special characters for Windows OS" do
let(:metadata) { { :resource_name => 'foo<>:"/\|?*bar' } }

it "removes them" do
expect(html_example.dirname).to eq("foobar")
end
end

describe "multi-character example name" do
let(:metadata) { { :resource_name => "オーダ" } }
let(:label) { "Coffee / Teaが順番で並んでいること" }