Skip to content

Instantly share code, notes, and snippets.

@hymkor
Last active June 30, 2024 13:57
Show Gist options
  • Save hymkor/d2f4ebca9bbe83669cef44f7c9aac668 to your computer and use it in GitHub Desktop.
Save hymkor/d2f4ebca9bbe83669cef44f7c9aac668 to your computer and use it in GitHub Desktop.
nyagosでシングルクォートを UNIX 風に使えるようにするフィルタ。"で囲まれていない ' を " にかえ、' で囲まれた " を \" に置換するだけ
local function sq2dq(source)
local sq = false
local dq = false
return (string.gsub(source,"[\"']",function(c)
if c == "'" then
if not dq then
sq = not sq
return '"'
end
end
if c == '"' then
if sq then
return [[\"]]
end
dq = not dq
end
return c
end))
end
if nyagos then
share.org_filter_for_sq2dq = nyagos.filter
nyagos.filter = function(s)
s = sq2dq(s)
if share.org_filter_for_sq2dq then
s = share.org_filter_for_sq2dq(s)
end
return s
end
else
assert(sq2dq[['(print "ahaha")']] == [["(print \"ahaha\")"]])
assert(sq2dq[["(print \"ahaha\")"]] == [["(print \"ahaha\")"]])
assert(sq2dq[["(print 'ahaha')"]] == [["(print 'ahaha')"]] )
end
@hymkor
Copy link
Author

hymkor commented Jun 30, 2024

.nyagos に直接コピペしてもいいですし、.nyagos に次のように書いてもいいです( ディレクトリの \\\ に変えておくこと )

package.path = package.path ..  ";(sq2dq.luaの置き場所のディレクトリ)\\?.lua"
require "sq2dq"

コマンドラインの色付けが対応してませんが、これは仕方ないかなと…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment