Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function lib.discord:getCodeBlockFromText(str)
- -- for getting ```... ```, returns an array which contains a dictionary of the content and file type
- -- possible ```lang? test``` case
- str = tostring(str)
- local result = {}
- local i = 1
- local function getStr(a, b)
- a = a or i
- b = b or a
- return str:sub(a, b)
- end
- local function isAtTargetString(subStr)
- local currentStr = getStr(i, i + #subStr - 1)
- if currentStr == subStr then
- i = i + #subStr
- return true
- end
- end
- local function captureStringUntil(subStr)
- local startI = i
- while getStr(i, i + #subStr - 1) ~= subStr and getStr() ~= '' do
- i = i + 1
- end
- local endI = i - 1
- local str = getStr(startI, endI)
- i = i + #subStr - 1
- return str
- end
- while true do
- local char = getStr()
- if char == ''then
- break
- elseif isAtTargetString('```')then
- -- in code block
- local blockContent = captureStringUntil('```')
- local j = 1
- local startJ = 1
- local endJ = 1
- local foundLang = false
- -- another loop for the blockContent
- while true do
- local char = blockContent:sub(j, j)
- -- only stop if whitespace is met
- if char:find'%A' or char == '' then
- if char == '\n'then
- -- we found our programming lang
- endJ = j - 1
- foundLang = true
- else
- -- we didnt
- endJ = 0
- end
- -- end of code block
- break
- end
- j = j + 1
- end
- -- separate lang and code
- local lang = blockContent:sub(startJ, endJ)
- local code = foundLang and blockContent:sub(endJ + 2) or blockContent
- -- insert result
- table.insert(result, {
- sourceCode = code;
- programmingLang= lang;
- })
- end
- i = i + 1
- end
- return result
- end
Add Comment
Please, Sign In to add comment