Skip to content
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

Fix 699 unsafe filenames #700

Closed
wants to merge 9 commits into from
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@

##### Enhancements

* None.
* New config option "use_safe_filenames" encodes unsafe characters when
generating filenames. By default, documentation may receive filenames like
"/(_:_:).html". With "use_safe_filenames", the same file will receive the name
"_2F_28_5F_3A_5F_3A_29.html" instead.
[Jeremy David Giesbrecht](https://github.com/SDGGiesbrecht)
[#699](https://github.com/realm/jazzy/issues/699)
[#146](https://github.com/realm/jazzy/issues/146)
[#361](https://github.com/realm/jazzy/issues/361)
[#547](https://github.com/realm/jazzy/issues/547)
[#558](https://github.com/realm/jazzy/issues/558)

##### Bug Fixes

Expand Down
8 changes: 8 additions & 0 deletions lib/jazzy/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,14 @@ def expand_path(path)
Pathname(__FILE__).parent + 'themes' + t
end

config_attr :use_safe_filenames,
command_line: '--use-safe-filenames',
description: 'Replace unsafe characters in filenames with an encoded '\
'representation. This will reduce human readability of '\
'some URLs, but may be necessary for projects that '\
'expose filename-unfriendly functions such as /(_:_:)',
default: false

config_attr :template_directory,
command_line: ['-t', '--template-directory DIRPATH'],
description: 'DEPRECATED: Use --theme instead.',
Expand Down
11 changes: 10 additions & 1 deletion lib/jazzy/sourcekitten.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ def self.make_group(group, name, abstract)
end unless group.empty?
end

def self.sanitize_filename(unsafe_filename)
if Config.instance.use_safe_filenames
normalized = unsafe_filename.unicode_normalize(:nfc)
return CGI.escape(normalized).gsub('_', '%5F').tr('%', '_')
else
return unsafe_filename
end
end

# rubocop:disable Metrics/MethodLength
# Generate doc URL by prepending its parents URLs
# @return [Hash] input docs with URLs
Expand All @@ -72,7 +81,7 @@ def self.make_doc_urls(docs)
# Create HTML page for this doc if it has children or is root-level
doc.url = (
subdir_for_doc(doc) +
[doc.name + '.html']
[sanitize_filename(doc.name) + '.html']
).join('/')
doc.children = make_doc_urls(doc.children)
else
Expand Down