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

Handle leading whitespace on code blocks #134

Merged
merged 7 commits into from
Apr 21, 2018
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ _coverage/

# For local experiments.
scratch/

# opam v2
_opam/
1 change: 1 addition & 0 deletions odoc.opam
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dev-repo: "http://github.com/ocaml-doc/odoc.git"

available: [ocaml-version >= "4.03.0"]
depends: [
"astring" {build}
"bos" {build}
"cmdliner" {build}
"cppo" {build}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/jbuild
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
(library
((name parser_)
(preprocess (pps (bisect_ppx)))
(libraries (model))))
(libraries (model astring))))
33 changes: 33 additions & 0 deletions src/parser/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,38 @@ let trim_trailing_blank_lines : string -> string = fun s ->
in
String.sub s 0 trim_from

let trim_leading_whitespace : string -> string = fun s ->
let count_leading_whitespace : string -> int = fun line ->
let rec count_leading_whitespace' : int -> int = fun index ->
if index >= String.length line then
index
else
match line.[index] with
| ' ' | '\t' -> count_leading_whitespace' (index + 1)
| _ -> index
in
count_leading_whitespace' 0
in
let lines = Astring.String.cuts ~sep:"\n" s in
let least_amount_of_whitespace =
lines
|> List.map count_leading_whitespace
(* Note that if [lines] is empty, [least_amount_of_whitespace] will be
[max_int]. But this is okay since if it's indeed empty, the value
will not be used when trying to remove whitespace below. *)
|> List.fold_left min max_int
in
let remove_whitespace : string -> string = fun line ->
String.sub
line
least_amount_of_whitespace
(String.length line - least_amount_of_whitespace)
in
lines
|> List.map remove_whitespace
|> String.concat "\n"




module Location = Model.Location_
Expand Down Expand Up @@ -256,6 +288,7 @@ rule token input = parse
| "{[" (code_block_text as c) "]}"
{ let c = trim_leading_blank_lines c in
let c = trim_trailing_blank_lines c in
let c = trim_leading_whitespace c in
emit input (`Code_block c) }

| "{v" (verbatim_text as t) "v}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
((output (ok (((f.ml (1 0) (2 6)) (code_block " foo"))))) (warnings ()))
((output (ok (((f.ml (1 0) (2 6)) (code_block foo))))) (warnings ()))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
((output (ok (((f.ml (1 0) (2 7)) (code_block "foo\
\n\tbar")))))
(warnings ()))
3 changes: 3 additions & 0 deletions test/parser/expect/code-block/leading-tab-two.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
((output (ok (((f.ml (1 0) (2 6)) (code_block "foo\
\nbar")))))
(warnings ()))
2 changes: 1 addition & 1 deletion test/parser/expect/code-block/leading-tab.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
((output (ok (((f.ml (1 0) (1 8)) (code_block "\tfoo"))))) (warnings ()))
((output (ok (((f.ml (1 0) (1 8)) (code_block foo))))) (warnings ()))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
((output (ok (((f.ml (1 0) (2 6)) (code_block "foo\r\
\nbar")))))
(warnings ()))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
((output (ok (((f.ml (1 0) (2 6)) (code_block " foo\
\nbar")))))
(warnings ()))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
((output (ok (((f.ml (1 0) (2 8)) (code_block "foo\
\n bar")))))
(warnings ()))
3 changes: 3 additions & 0 deletions test/parser/expect/code-block/leading-whitespace-two.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
((output (ok (((f.ml (1 0) (2 6)) (code_block "foo\
\nbar")))))
(warnings ()))
2 changes: 1 addition & 1 deletion test/parser/expect/code-block/leading-whitespace.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
((output (ok (((f.ml (1 0) (1 8)) (code_block " foo"))))) (warnings ()))
((output (ok (((f.ml (1 0) (1 8)) (code_block foo))))) (warnings ()))
6 changes: 6 additions & 0 deletions test/parser/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,13 @@ let tests : test_suite list = [
t "cr-lf" "{[foo\r\nbar]}";
t "blank-line" "{[foo\n\nbar]}";
t "leading-whitespace" "{[ foo]}";
t "leading-whitespace-two" "{[ foo\n bar]}";
t "leading-whitespace-two-cr-lf" "{[ foo\r\n bar]}";
t "leading-whitespace-two-different-indent" "{[ foo\n bar]}";
t "leading-whitespace-two-different-indent-rev" "{[ foo\n bar]}";
t "leading-tab" "{[\tfoo]}";
t "leading-tab-two" "{[\tfoo\n\tbar]}";
t "leading-tab-two-different-indent" "{[\tfoo\n\t\tbar]}";
t "leading-newline" "{[\nfoo]}";
t "leading-cr-lf" "{[\r\nfoo]}";
t "leading-newlines" "{[\n\nfoo]}";
Expand Down