From 7b7ac0210af3068663ad0aec8b69914c319f1fa4 Mon Sep 17 00:00:00 2001 From: icy Date: Sat, 9 Feb 2013 14:33:12 +0700 Subject: [PATCH] (generate_id) support Vietnamese accents Vietnamese chars with accents will be converted to non-accent verion that help the link id (anchor name) readable --- lib/kramdown/converter/base.rb | 11 ++++++++++- test/testcases/block/04_header/with_auto_ids.html | 2 ++ test/testcases/block/04_header/with_auto_ids.text | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/kramdown/converter/base.rb b/lib/kramdown/converter/base.rb index a17aaef7..dea7252e 100644 --- a/lib/kramdown/converter/base.rb +++ b/lib/kramdown/converter/base.rb @@ -164,7 +164,16 @@ def extract_code_language!(attr) # Uses the option +auto_id_prefix+: the value of this option is prepended to every generated # ID. def generate_id(str) - gen_id = str.gsub(/^[^a-zA-Z]+/, '') + # Convert Vietnamese accents to non-accent version + # This will help the link readable + vntext_src = "áàảãạăắằẳẵặâấầẩẫậđéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵ" + vntext_dst = "aaaaaaaaaaaaaaaaadeeeeeeeeeeeiiiiioooooooooooooooouuuuuuuuuuuuyyyyy" + vntext_src += "ÁÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬĐÉÈẺẼẸÊẾỀỂỄỆÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴ" + vntext_dst += vntext_dst.upcase + + gen_id = str + gen_id.tr!(vntext_src, vntext_dst) + gen_id.gsub!(/^[^a-zA-Z]+/, '') gen_id.tr!('^a-zA-Z0-9 -', '') gen_id.tr!(' ', '-') gen_id.downcase! diff --git a/test/testcases/block/04_header/with_auto_ids.html b/test/testcases/block/04_header/with_auto_ids.html index 8c073774..8b968f5f 100644 --- a/test/testcases/block/04_header/with_auto_ids.html +++ b/test/testcases/block/04_header/with_auto_ids.html @@ -17,3 +17,5 @@

33333

hallO

Header without ID

+ +

Đây là ví dụ với tiếng Việt

diff --git a/test/testcases/block/04_header/with_auto_ids.text b/test/testcases/block/04_header/with_auto_ids.text index 4142fbb3..78fa7695 100644 --- a/test/testcases/block/04_header/with_auto_ids.text +++ b/test/testcases/block/04_header/with_auto_ids.text @@ -20,3 +20,5 @@ Not now # Header without ID {: id=""} + +# Đây là ví dụ với tiếng Việt