-
-
Notifications
You must be signed in to change notification settings - Fork 398
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
Store extra files in their own directory #1289
base: main
Are you sure you want to change the base?
Conversation
Sorry, was missing the merge commit for some reason. |
3b2a711
to
ec809b5
Compare
When generating HTML, store extra files in their own directory ("_file") instead of prefixing their names with "file." and writing them to the web root.
ec809b5
to
8fa6608
Compare
Hi @bfad, I'm unclear on the purpose of this change. As is, this is a pretty large breaking change, since this would change URL structure of any statically deployed documentation, which is pretty significant and would need a good justification. I get that there's an attempt to bring static generation more in line with If the intent is to fix #1277, there are probably other ways to do that without changing the naming scheme on disk. Presumably this could be done with similar custom file metadata attributes to |
@lsegal thanks for looking this over. The ultimate end goal is the ability to have a hierarchical structure for these extra documents. The idea being that I would use globing and the file hierarchy would be replicated, not just at the file-system level, but also with disclosure triangles in the HTML sidebar (just like namespace hierarchies). This could still be done with something like a |
Backing up a bit, it seems like we're solving for two separate problems. The location that YARD uses to serialize files on disk, as well as the expected URL structure by YARD actually stores the full location to each extra file alongside the file object inside the Now, duplicate/conflicting filenames from subdirs is a separate problem that still exists even if you solve the above. That said, the location that YARD chooses to serialize files to should generally not be a user concern and should typically be hidden from the user, so realistically, there are a few options to disambiguate conflicting filenames that could work without a breaking change or even introducing an attribute:
Hope this helps / makes sense. |
Sidenote: these can and should be two separable and independent PRs. Specifically, the latter issue could be turned into a PR and accepted to fix the underlying issue of dupes without needing to change how files render in the template. |
Here's a first-attempt patch for supporting dupe files: diff --git a/lib/yard/cli/yardoc.rb b/lib/yard/cli/yardoc.rb
index 5332196f..8a6804e1 100644
--- a/lib/yard/cli/yardoc.rb
+++ b/lib/yard/cli/yardoc.rb
@@ -222,6 +222,7 @@ module YARD
@save_yardoc = true
@has_markup = false
@fail_on_warning = false
+ @extra_file_dupes = {}
if defined?(::Encoding) && ::Encoding.respond_to?(:default_external=)
utf8 = ::Encoding.find('utf-8')
@@ -413,7 +414,17 @@ module YARD
files.map! {|f| f.include?("*") ? Dir.glob(f) : f }.flatten!
files.each do |file|
if extra_file_valid?(file)
- options.files << CodeObjects::ExtraFileObject.new(file)
+ extra_file = CodeObjects::ExtraFileObject.new(file)
+ options.files << extra_file
+
+ if @extra_file_dupes[extra_file.name]
+ extra_file.attributes[:title] ||= extra_file.filename
+ extra_file.name = extra_file.filename.
+ gsub(/\.[^.]+$/, '').
+ gsub(/\//, '_')
+ else
+ @extra_file_dupes[extra_file.name] = true
+ end
end
end
end The gsub stuff could be refactored into ExtraFileObject's name= setter to remove the extension, as well as a change to FileSystemSerializer to correctly escape |
What if, instead of messing with diff --git a/lib/yard/code_objects/extra_file_object.rb b/lib/yard/code_objects/extra_file_object.rb
index c0f53a3e..eb9bad6f 100644
--- a/lib/yard/code_objects/extra_file_object.rb
+++ b/lib/yard/code_objects/extra_file_object.rb
@@ -8,6 +8,7 @@ module YARD::CodeObjects
attr_accessor :filename
attr_writer :attributes
attr_accessor :name
+ attr_accessor :path
# @since 0.8.3
attr_reader :locale
@@ -18,6 +19,7 @@ module YARD::CodeObjects
def initialize(filename, contents = nil)
self.filename = filename
self.name = File.basename(filename).gsub(/\.[^.]+$/, '')
+ self.path = filename.gsub(/\.[^.]+$/, '')
self.attributes = SymbolHash.new(false)
@original_contents = contents
@parsed = false
@@ -25,8 +27,6 @@ module YARD::CodeObjects
ensure_parsed
end
- alias path name
-
def attributes
ensure_parsed
@attributes
diff --git a/templates/default/fulldoc/html/setup.rb b/templates/default/fulldoc/html/setup.rb
index 201ba63c..bd0e1954 100644
--- a/templates/default/fulldoc/html/setup.rb
+++ b/templates/default/fulldoc/html/setup.rb
@@ -7,8 +7,11 @@ def init
return serialize_onefile if options.onefile
generate_assets
serialize('_index.html')
+
+ dup_file_names = {}
options.files.each_with_index do |file, _i|
- serialize_file(file, file.title)
+ serialize_file(file, dup_file_names[file.name] ? file.path : file.name)
+ dup_file_names[file.name] = true
end
options.delete(:objects)
@@ -60,10 +63,10 @@ end
# @param [String] title currently unused
#
# @see layout#diskfile
-def serialize_file(file, title = nil) # rubocop:disable Lint/UnusedMethodArgument
+def serialize_file(file, file_name)
options.object = Registry.root
options.file = file
- outfile = 'file.' + file.name + '.html'
+ outfile = 'file.' + file_name.gsub(/\//, '_') + '.html'
serialize_index(options) if file == options.readme
Templates::Engine.with_serializer(outfile, options.serializer) do |
44c07a8
to
112a890
Compare
@lsegal @bfad Hello. If I may ask, when will this PR get merged? I have some documentation for a pet project I'm working on, and the docs have some markdown files that link to each other, but those links are broken; one of the reasons they are broken is because YARD's flat file structure is incompatible with the file/folder structure the browser expects. I'm hoping this gets merged ASAP because I released V1 of my project with broken documentation because I knew that a fix would not come soon. |
Description
Currently when creating HTML documentation, extra files are stored at the web root with "file." prefixed to their name. This PR changes this practice to instead store them in their own directory (named "_file" to prevent conflicts with Ruby objects).
This brings the output more inline with the server which hosts the files under
/file/
path. In fact, the server keeps the relative path to the extra file as part of the URL path. (For example, while yard's changelog is found at/file/CHANGELOG.md
the getting started guide is at/file/docs/GettingStarted.md
.) If this PR is accepted, I would like to work on doing something similar for HTML creation. (This might fix #1277 as all the READMEs would be listed there, and it's easy enough to set custom title attributes to help distinguish.) If all of that is successful, I would like to have a setting that allows for showing the extra file hierarchically similar to how nested Ruby objects are presented with disclosure triangles.Completed Tasks
bundle exec rake
locally (if code is attached to PR).