Skip to content

Instantly share code, notes, and snippets.

@sixtyfive
Created October 12, 2018 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sixtyfive/e8dd69ba02a43be6fded715223086e23 to your computer and use it in GitHub Desktop.
Save sixtyfive/e8dd69ba02a43be6fded715223086e23 to your computer and use it in GitHub Desktop.
def add_translit_markup(input)
span_open = false
output = ''
input.each_char do |c|
unless c.match /\s/
if c.match /\p{Arabic}/
output += "</span>" if span_open
span_open = false
else
unless span_open
output += "<span class='translit'>"
span_open = true
end
end
end
output += c
end
return output
end
# » string = "حروف العربية und dann العربة مرة ثانية und nochmal deutsch وأخيرا العربية"
# » puts add_translit_markup(string)
# حروف العربية <span class='translit'>und dann </span>العربة مرة ثانية <span class='translit'>und nochmal deutsch </span>وأخيرا العربية
# => nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment