-
Notifications
You must be signed in to change notification settings - Fork 90
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 indentation of code blocks #133
Comments
Thanks for the helpful writeup, Anton! Since I'm the one reporting, I think I'd be able to tackle this over the weekend. If anyone wants to get it first, feel free to do so :) |
Can this be closed now that #134 is in? |
I was just testing on a "real-world" example and found one little snag, so not quite yet. Will comment in #134. |
I'm wondering if we shouldn't do the same thing for verbatim blocks, any opinion? |
Most likely. |
This issue is about adjusting the parser slightly, to delete some leading whitespace from code blocks.
Problem
When the author of docs writes
...the code block gets parsed as
...and rendered in the HTML with all that left whitespace, which shouldn't be there. This forces people to use the ugly workaround:
odoc should do this automatically instead. odoc should find a line with the least amount of left whitespace (in this case, both lines have 6 leading spaces), and delete that much leading whitespace from all lines in the code block, in this case resulting in:
...which will get displayed nicely.
This was originally reported in #132 by @bobbypriambodo.
Fixing it
The issue should be pretty easy to solve. odoc already postprocesses code blocks immediately after reading them from comments, to remove leading and trailing blank lines. We need to add one more postprocessing step to that code, to remove the leading whitespace.
The rest of this post is a guide on how to do that :)
(Setup) Clone the repo and install development dependencies:
(Sanity check) Run
make test
to check the setup, and make sure tests start out passing :)(Coding) The existing steps for postprocessing code blocks are here:
odoc/src/parser/lexer.mll
Lines 256 to 259 in af59e44
and the functions being called are defined here.
So, we basically just need to add a third function, that goes through all lines in the code block text, finds which one has the least leading whitespace, and then goes through the lines again and removes that much whitespace from each one (you are welcome to use any other method, this is just the obvious suggestion :)).
(Testing) The parser is pretty heavily tested. So, once you are calling this new function, some of the tests will start failing. This is a good thing :) The first test to fail should be this one:
odoc/test/parser/test.ml
Line 273 in af59e44
...and the message will be something like
The
-
line is what the tests expected the output to be. It's what the parser used to output (note the space beforefoo
). But now, we improved the parser, so we want the test to expect our new, corrected output (the line with the+
). To replace the expected output, run the command the test framework is suggesting:Then, run
make test
again. A few more tests that have leading whitespace will also fail. Inspect the output, and if the new output is correct, replace the expected output using the same command.See
CONTRIBUTING.md
for details on the testing setup.(Optional) Write a new test. For example, it could give the parser a multiline code block, with lines that have different indentation levels. The testing framework will fail on it, because there is not yet any expected output. But, it will tell you what your actual output was, and give you a command for creating the expected output from it :)
If you'd like to test your code "live" against a real project, you can install the development odoc with
This will install it from your development repo, since we pinned it earlier.
(Finishing up) Commit the new code (in
lexer.mll
), any changes to the tests (test/parser/test.ml
), and the new expected outputs (they will be intest/parser/expect/**
), and send the odoc repo a PR :)You may also want to unpin odoc now:
Thank you!! 🎉
The text was updated successfully, but these errors were encountered: