Last active
August 29, 2015 14:03
-
-
Save vzvu3k6k/f2fb593869aebf2a5921 to your computer and use it in GitHub Desktop.
Update your asciidoc to fit the new Asciidoctor's `inline literal passthrough` spec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Since a1b1751, Asciidoctor no longer renders "それは`Promise.all`です。" as | |
# "...それは<code>Promise.all</code>です。...", but as "...それは`Promise.allです。...". | |
# This script updates your asciidoc to fit the new Asciidoctor behavior. | |
# | |
# resolves #892 match word characters defined by Unicode · a1b1751 · asciidoctor/asciidoctor | |
# https://github.com/asciidoctor/asciidoctor/commit/a1b17518d6f6#diff-9f476fe67d53af1b92adf5d43d06183eL874 | |
# | |
# Breaking changes in inline code literal? · Issue #984 · asciidoctor/asciidoctor | |
# https://github.com/asciidoctor/asciidoctor/issues/984 | |
# | |
# Usage: | |
# ruby codefix.rb **/*.adoc | |
# License: | |
# CC0 | |
def replace(text) | |
pass_lit = / | |
(?<head>^|[^`\w]) | |
(?<option>\[([^\]]+?)\])? | |
(?<body>\\?`([^`\s]|[^`\s].*?\S)`) | |
(?![`\w]) | |
/mx | |
replaced = text.gsub(pass_lit){ | |
match = $~ | |
head_space = | |
if match[:head] =~ /\p{Word}/ && match[:head] !~ /\w/ | |
" " | |
end | |
tail_space = begin | |
tail = text[match.end(0)] | |
if tail =~ /\p{Word}/ && tail !~ /\w/ | |
" " | |
end | |
end | |
[match[:head], head_space, match[:option], match[:body], tail_space].map(&:to_s).join("") | |
} | |
end | |
if ARGV.empty? # run tests | |
[ | |
["<<promise.then,`.then`>>", | |
"<<promise.then,`.then`>>"], | |
["<<Promise.all, `Promise.all`>> あああ<<Promise.race, `Promise.race`>>あああ。", | |
"<<Promise.all, `Promise.all`>> あああ<<Promise.race, `Promise.race`>>あああ"], | |
["`Promise.all`です。", | |
"`Promise.all` です。",], | |
["ハロー`Promise.all`", | |
"ハロー `Promise.all`",], | |
["今日も`Promise.all`だ。", | |
"今日も `Promise.all` だ。",], | |
["今日も `Promise.all`だ。", | |
"今日も `Promise.all` だ。",], | |
["今日も`Promise.all` だ。", | |
"今日も `Promise.all` だ。",], | |
["今日も `Promise.all` だ。", | |
"今日も `Promise.all` だ。",], | |
["あ`then`い`catch`う", | |
"あ `then` い `catch` う"] | |
].each do |(text, expected)| | |
result = replace(text) | |
if result != expected | |
p result | |
end | |
end | |
else | |
ARGV.each do |path| | |
p path | |
new_content = replace(File.read(path)) | |
File.write(path, new_content) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment