Skip to content

Commit

Permalink
Add format_code_blocks (#795)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Nov 11, 2024
1 parent 9d92f33 commit 6d0352e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ clean_all: clean clean_vendor
format_api_docs_links:
echo $(DOCS_FILES) | xargs sed -i -E -e 's@\bhttps?://(www\.)?crystal-lang\.org/api/([0-9]+(\.[0-9]+)+|latest|master)/([^ )]+\.html)\b@https://crystal-lang.org/api/\4@g'

.PHONY: format_code_blocks
format_code_blocks:
NO_COLOR=1 LINT=true make clean build 2>&1 | ./scripts/format-code-blocks.cr

.PHONY: help
help: ## Show this help
@echo
Expand Down
23 changes: 23 additions & 0 deletions scripts/format-code-block.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env crystal
#
# This script parses output from `mkdocs build --strict` to find ill-formatted
# code blocks and applies the updated formatting (from the formatter STDOUT).

io = STDIN
docs_path = Path["docs"]

loop do
line = io.gets || exit
line.starts_with?("WARNING - In file") || next
filename = line[/(?<=')[^']+(?=')/]? || next
filename = docs_path / filename

io.gets # skip -------- Input --------

input = IO::Delimited.new(io, "\n-------- Output --------\n").gets_to_end
output = IO::Delimited.new(io, "formatting 'STDIN' produced changes\n").gets_to_end

original_content = File.read(filename)
content = original_content.sub input, output
File.write(filename, content)
end

0 comments on commit 6d0352e

Please sign in to comment.