local p = {}

local function isCJKV(char)
  local code = mw.ustring.codepoint(char, 1)
  return (code >= 0x4E00 and code <= 0x9FFF) or
         (code >= 0x3400 and code <= 0x4DBF) or
         (code >= 0x20000 and code <= 0x2A6DF) or
         (code >= 0x2A700 and code <= 0x2B73F) or
         (code >= 0x2B740 and code <= 0x2B81F) or
         (code >= 0x2B820 and code <= 0x2CEAF) or
         (code >= 0x2CEB0 and code <= 0x2EBEF) or
         (code >= 0x30000 and code <= 0x3134F) or
         (code >= 0x31350 and code <= 0x323AF) or
         (code >= 0xF900 and code <= 0xFAFF) or
         (code >= 0x2F800 and code <= 0x2FA1F)
end

function p.repeatCJKV(frame)
  local input = frame.args[1] or ""
  local n = mw.ustring.len(input)
  local output = ""

  local textBefore = frame:preprocess('{{TEXTBEFORE}}')

  local cjkv_count = 0;
  local cjkv_string = "";
  for i = 1, mw.ustring.len(textBefore) do
    local char = mw.ustring.sub(textBefore, i, i)
    if isCJKV(char) then
      cjkv_string = cjkv_string .. char
      cjkv_count = cjkv_count + 1
    end
  end

  if n > 0 then
      local plain_cjkv_count = 0
      local plain_cjkv_string = ""
      for i = mw.ustring.len(cjkv_string), 1, -1 do
          local char = mw.ustring.sub(cjkv_string, i, i)
          plain_cjkv_string = char .. plain_cjkv_string
          plain_cjkv_count = plain_cjkv_count + 1
          if plain_cjkv_count == n then
             break
          end
      end

      output = plain_cjkv_string
  end

  return output
end

return p
No tags for this post.