-
-
Save nishimoto/f0e743dff91bf6eecf0606ea2d4f0270 to your computer and use it in GitHub Desktop.
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
line = "[yahoo.co.jp Yahoo]" | |
def markup_to_html(line) | |
arr = line.split(/(\[|\])/) | |
i = 0 | |
while arr[i] != nil | |
item = arr[i] | |
if arr[i] == "[" && arr[i+2] == "]" | |
if arr[i+1].index(/\s/) | |
url = arr[i+1].split(/\s/)[0] | |
str = arr[i+1].split(/\s/)[1] | |
arr[i+1] = "<a href=\"#{url}\">#{str}</a>" | |
arr.delete_at(i) | |
arr.delete_at(i+1) | |
else | |
arr[i+1] = "<a href=\"#{arr[i+1]}\">#{arr[i+1]}</a>" | |
arr.delete_at(i) | |
arr.delete_at(i+1) | |
end | |
end | |
i += 1 | |
end | |
return arr.join | |
end | |
print markup_to_html(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment