asciidoctor / asciidoctor-extensions-lab

A lab for testing and demonstrating Asciidoctor extensions. Please do not use this code in production. If you want to use one of these extensions in your application, create a new project, import the code, and distribute it as a RubyGem. You can then request to make it a top-level project under the Asciidoctor organization.
Other
104 stars 101 forks source link

Use pure Ruby to find files in TreeBlockMacro #91

Closed nightscape closed 5 years ago

nightscape commented 6 years ago

I tried to use the tree block macro inside the official asciidoctor Docker image, but that unfortunately doesn't have the tree command. I created a pure Ruby version for listing files, but it might not yet match the original output as I didn't fully understand the interplay between the tree command and the gsubs. I'd be thankful for any pointers of what to improve :smiley:

mojavelinux commented 5 years ago

@nightscape This is a great start. I agree this extension will be much more useful and portable if the tree is created using Ruby.

Here are a few things that need to be changed in order for it to match the original behavior:

  1. Sort folders before files (at same depth)
  2. Include the filesize if requested (your code computes it, but then never adds it to the line)
  3. Make the units match (use K instead of KiB, don't use B for bytes)
  4. Align filesize to right (pad to left)
  5. Strip the base depth away from the indentation so that the tree always starts the left margin

I recommend generating a tree using the tree command so that you can compare the result. Also be sure to test an absolute path, not just a relative one.

mojavelinux commented 5 years ago

I pushed another commit to the PR that implements the features listed in my previous comment. Give it a try and see if you like it. If so, we can proceed with merging.

I do wonder if we should still provide an option to use the tree command. I can't really see a benefit to keeping that code since it's a) system dependent, b) required a lot of hacking of the output, and c) doesn't add any value. Would you agree we should just stick with a pure Ruby approach?

mojavelinux commented 5 years ago

I'm going to go ahead and proceed with the merge. If there are any changes you'd like to make, feel free to follow-up with another PR.