From 4a52c88273a61abba28c6acd7d93cbdb0b21246d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0=20=D0=A4=D0=B0?= =?UTF-8?q?=D1=80=D0=B1=D0=B5=D1=80?= Date: Sun, 14 Dec 2025 21:48:12 +0400 Subject: [PATCH] init --- LICENSE | 2 +- README.md | 41 +++++++++++++++++++++-- test.lua | 11 ++++++ translit-1.0-0.rockspec | 27 +++++++++++++++ translit.lua | 74 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 test.lua create mode 100644 translit-1.0-0.rockspec create mode 100644 translit.lua diff --git a/LICENSE b/LICENSE index 57f3d62..1a3ac33 100644 --- a/LICENSE +++ b/LICENSE @@ -75,7 +75,7 @@ If you develop a new program, and you want it to be of the greatest possible use To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. - translit Copyright (C) 19yy Datenlabor + translit Copyright (C) 19yy Tank This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. diff --git a/README.md b/README.md index 253e35a..2e4bfe6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,40 @@ -# translit +# Транслитерация с кириллицы на английский и обратно (Cyrillic-to-English and English-to-Cyrillic transliterator) -Cyrillic-to-English and English-to-Cyrillic transliterator \ No newline at end of file +## Установка (Install) + +``` +luarocks install translit + +``` + +## Использование (Usage) + +### Транслитерация с киррилицы на английский (Cyrillic-to-English) + +``` +local translit = require('translit') + +local en_str = translit.en('Русский текст') + +``` + +### Транслитерация с киррилицы на русский (English-to-Cyrillic) + +``` +local translit = require('translit') + +local cy_str = translit.cy('English text') + +``` + +### Использование для URL (slug) + +``` +local function sluger(title) + local slug = translit.en(title) + slug = string.gsub(slug,'%s','-') + slug = string.gsub(slug,'[^%w-]','') + return string.lower(slug) +end + +``` diff --git a/test.lua b/test.lua new file mode 100644 index 0000000..66bed6d --- /dev/null +++ b/test.lua @@ -0,0 +1,11 @@ +local translit = require('translit') + +local str = 'Русский текст' + +local en_str = translit.en(str) + +print(en_str) + +local ru_str = translit.ru(en_str) + +print(ru_str) diff --git a/translit-1.0-0.rockspec b/translit-1.0-0.rockspec new file mode 100644 index 0000000..cc3a9a9 --- /dev/null +++ b/translit-1.0-0.rockspec @@ -0,0 +1,27 @@ +package = "translit" +version = "1.0-0" +source = { + url = "git+https://gitlabor.ru/Tank/translit", + tag = "1.0-0" +} +description = { + summary = "Cyrillic-to-English and English-to-Cyrillic transliterator", + detailed = [[ + Cyrillic-to-English and English-to-Cyrillic transliterator. Транслитерация с киррилицы на английский и обратно. + ]], + homepage = "https://gitlabor.ru/Datenlabor/translit", + license = "MIT/X11" +} +dependencies = { + "lua >= 5.1", + "utf8 >= 1.2" +} +external_dependencies = { + +} +build = { + type = "builtin", + modules = { + ["translit"] = "translit.lua", + } +} diff --git a/translit.lua b/translit.lua new file mode 100644 index 0000000..39842c2 --- /dev/null +++ b/translit.lua @@ -0,0 +1,74 @@ +--[[ + Copyright 2025 datenlabor + + Readme: https://gitlabor.ru/Datenlabor/translit + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. + + +--]] + +local utf8 = require('lua-utf8') + +local _M = {} + +local function replace(str,data) + for k,v in pairs(data) do + str = utf8.gsub(str,k,v) + end + return str +end + +function _M.en(input) + local gost = { + ['а']='a',['б']='b',['в']='v',['г']='g',['д']='d',['е']='e', + ['ё']='yo',['ж']='j',['з']='z',['и']='i',['й']='i',['к']='k', + ['л']='l',['м']='m',['н']='n',['о']='o',['п']='p',['р']='r', + ['с']='s',['т']='t',['у']='y',['ф']='f',['х']='h',['ц']='c', + ['ч']='ch',['ш']='sh',['щ']='sh',['ы']='i',['э']='e',['ю']='u', + ['я']='ya',['А']='A',['Б']='B',['В']='V',['Г']='G',['Д']='D', + ['Е']='E',['Ё']='Yo',['Ж']='J',['З']='Z',['И']='I',['Й']='I', + ['К']='K',['Л']='L',['М']='M',['Н']='N',['О']='O',['П']='P', + ['Р']='R',['С']='S',['Т']='T',['У']='Y',['Ф']='F',['Х']='H', + ['Ц']='C',['Ч']='Ch',['Ш']='Sh',['Щ']='Sh',['Ы']='I',['Э']='E', + ['Ю']='U',['Я']='Ya',['ь']='',['Ь']='',['ъ']='',['Ъ']='',['ї']='j', + ['і']='i',['ґ']='g',['є']='ye',['Ї']='J',['І']='I',['Ґ']='G',['Є']='YE' + } + return replace(input, gost) +end + + +function _M.cy(input) + local gost = { + ['ch']='ч',['sh']='ш',['sh']='щ',['YS']='ЫС',['Yo']='Ё',['yo']='ё', + ['YE']='Е',['SH']='Щ',['YA']='Я',['CH']='Ч',['SH']='Ш', + ['Ch']='Ч',['Sh']='Ш',['Sh']='Щ',['Ya']='Я',['JA']='Я', + ['JS']='ЙС',['ZH']='Ж',['ya']='я',['ye']='э',['a']='а', + ['b']='б',['v']='в',['g']='г',['d']='д',['e']='е',['j']='ж', + ['z']='з',['i']='и',['i']='й',['k']='к',['l']='л',['m']='м', + ['n']='н',['o']='о',['p']='п',['r']='р',['s']='с',['t']='т', + ['y']='у',['f']='ф',['h']='х',['c']='ц',['i']='ы',['e']='е', + ['u']='у',['A']='А',['B']='Б',['V']='В',['G']='Г',['D']='Д', + ['E']='Е',['J']='Ж',['Z']='З',['I']='И',['K']='К',['L']='Л', + ['M']='М',['N']='Н',['O']='О',['P']='П',['R']='Р',['S']='С', + ['T']='Т',['Y']='Ы',['F']='Ф',['H']='Х',['C']='Ц',['E']='Е', + ['U']='У',["'"]='ь',["'"]='Ь',["''"]='ъ',["''"]='Ъ',['j']='ї', + ['i']='и',['g']='ґ',['G']='Г' + } + return replace(input, gost) +end + +return _M