היחידה מאפשרת לשלוף את שם הספר, המסכת והפרק.

שם הספר מוגדר כמילה אחת בלי רווחים, וכך גם שם הפרק.

לעומת זאת, בשם המסכת יכולים להיות רווחים.

שימוש ביחידה זו - בתבנית:ניווט משנה, וגם בתבנית:כותרת לפרק במסכת ובתבנית:כותרת לסעיף בפרק.

להדגמה, ראו ירושלמי פסחים א, ירושלמי בבא קמא א, ירושלמי פאה ו ג.

היחידה משתמשת בפונקציות מתוך יחידה:גימטריה.


local gimatria = require('יחידה:גימטריה');
local haqodem = gimatria.haqodem;
local haba = gimatria.haba;
local otiot2mispar = gimatria.gimatria;
local mispar2otiot = gimatria.mispar_lotiot;

local p = {}

local letter = "[א-ת]"
local letterspace = "[א-ת ]"
local letterspaceparen = "[א-ת ()]"

local letters = letter .. '+'
local letterspaces = letterspace .. '+'
local letterspaceparens = letterspaceparen .. '+'

local PATTERN_PARENTHESES = '%('..letterspaces..'%)'

-- חלוקה לשלושה חלקים: ספר (מילה אחת בלי רווחים), מסכת (עם או בלי רווחים), ופרק.
local PATTERN_PEREQ = '^('..letters..')([/ ])('..letterspaces..')([/ ])('..letters..')$';

-- חלוקה לשלושה חלקים: ספר (ומילה אחת בלי רווחים ועוד גירסה בסוגריים), מסכת (עם או בלי רווחים), ופרק.
local PATTERN_PEREQ_GIRSA = '^('..letters..' %('..letterspaces..'%))([/ ])('..letterspaces..')([/ ])('..letters..')$';

-- חלוקה לארבעה חלקים: ספר (מילה אחת בלי רווחים), מסכת  (עם או בלי רווחים), פרק, וסעיף.
local PATTERN_SEIF = '^('..letters..')%s+('..letterspaces..')%s+('..letters..')%s+('..letters..')$';

-- חלוקה לשלושה חלקים: ספר (עם או בלי רווחים), פרק, וסעיף.
local PATTERN_SEIF_BSEFER = '^([ א-ת]+)%s+([א-ת]+)%s+([א-ת]+)$';

-- חלוקה לשלושה חלקים: ספר (עם או בלי רווחים), פרק (בלי רווחים) ופסוק (בלי רווחים)
local PATTERN_BEUR_pasuq = '^ביאור:('..letterspaces..')%s+('..letters..')%s+('..letters..')$';

-- הפוקנציה מקבלת שם של דף, מפרקת אותו לשלושה חלקים (ספר, מסכת ופרק), ומכניסה את החלקים האלה לתבנית הנתונה.
-- למשל: אם שם הדף הוא "ירושלמי בבא קמא א",
-- והתבנית היא: "אאא <ספר> בבב <מסכת> גגג <פרק<"
-- אז הפונקציה תחזיר: "אאא ירושלמי בבב בבא קמא גגג א"
p["כותרת לפרק"] = function (frame)
    local title = frame.args[1]
    local template = frame.args[2]
    return p.koteret_lpereq_bsefer(title, template)
end


p.koteret_lpereq_bsefer =  function (title, template)
	-- title = title:gsub(PATTERN_PARENTHESES,'')
    local sefer, mafrid, maseket, mafrid1, pereq = string.match(title, PATTERN_PEREQ)
    if (sefer==nil) then
        sefer, mafrid, maseket, mafrid1, pereq = string.match(title, PATTERN_PEREQ_GIRSA)
    end
    local pereq_qodem = haqodem(pereq);
    local pereq_haba  = haba(pereq);
    template = template:gsub('<ספר>', sefer);
    template = template:gsub("<מסכת>", maseket);
    template = template:gsub("<פרק>", pereq);
    template = template:gsub("<פרק קודם>", pereq_qodem);
    template = template:gsub("<פרק הבא>", pereq_haba);
    template = template:gsub("<מפריד>", mafrid);
    return template
end


-- בטבלה זו, בכל כניסה בטבלה, צריכה להיות רשימה של מספרי המשניות בכל פרק במסכת.
-- למשל, במסכת פאה, בפרק א, יש 5 משניות, וכן הלאה.
local mispar_mishnayot_bepereq = {
    ['משנה'] = {
        ['פאה'] = 
            {5, 5, 7, 7, 7, 8, 7, 8},
        ['ברכות'] = 
            {6, 9, 6, 6, 5, 8, 5, 8,5},
        ['שקלים'] = 
            {7, 5, 3, 5, 4, 4, 3, 4},
    },
    ['ירושלמי'] = {
        ['ברכות'] = 
            {6, 9, 6, 6, 5, 8, 5, 8,5},
		['פאה'] = 
            {5, 5, 7, 7, 7, 8, 7, 8},
        ['דמאי'] = 
            {4, 5, 6, 5, 8, 8, 8},
        ['כלאים'] = 
            {9, 8, 6, 6, 7, 4, 6, 4, 6},
        ['שביעית'] =
            {6, 7, 7, 8, 4, 4, 2, 8, 6, 4},
        ['תרומות'] =     	
          	{5, 3, 5, 8, 3, 3, 3, 4, 3, 6, 5},
        ['מעשרות'] = 
            {5, 4, 4, 4, 3},
        ['מעשר שני'] =
            {3, 4, 6, 6, 5},
        ['חלה'] = 
            {6, 3, 5, 5},
        ['ערלה'] = 
             {5, 9, 7},
		['בכורים'] = 
            {9, 6, 6},

        ['שבת'] = 
             {11, 7, 8, 3, 4, 9, 4, 7, 7, 6, 6, 6, 7, 4, 3, 9, 7, 3, 6, 5},
        ['ערובין'] = 
        	{10,8,10,10,8,10,10,9,5,14},
        ['פסחים'] = 
        	{8,7,8,9,10,6,13,8,10,7},
        ['שקלים'] = 
            {7, 5, 3, 5, 4, 4, 3, 4},
		['יומא'] = 
			{8,4,9,6,7,7,3,7},
		['סוכה']=
			{12,10,12,7,8},
		['ביצה']=
			{12,10,9,7,8},
		['ראש השנה']=
			{8,8,10,10},
		['תענית']=
			{8,14,11,7},
		['מגילה']=
			{12,7,7,12},
		['מועד קטן']=
			{10,5,9},
		['חגיגה']=
			{8,7,8},
--נשים
		['יבמות'] = 
			{6,12,12,16,8,6,8,6,8,8,7,6,14,7,10,9},
     	['כתובות'] = 
     		{10,10,10,14,11,7,9,11,10,6,7,5,11},
     	['נדרים'] = 
     		{4,5,9,10,6,11,11,9,9,8,12},
     	['נזיר'] = 
     		{5,10,7,6,4,11,4,2,6},
     	['סוטה'] = 
     		{10,6,8,5,6,4,8,10,16},
     	['גיטין'] = 
     		{5,7,8,9,10,7,8,10,11},
     	['קידושין'] = 
     		{9,9,13,12},
--נזיקין
		['בבא קמא'] = 
			{5,8,11,9,8,8,7,8,10,11},
     	['בבא מציעא'] = 
     		{8,11,9,7,8,6,7,9,13,6},
     	['בבא בתרא'] = 
     		{5,11,11,8,5,4,4,9,9,10},
     	['סנהדרין'] = 
     		{4,6,12,9,5,10,13,9,7,8,6},
     	['מכות'] = 
     		{8,7},
     	['שבועות'] = 
     		{7,5,10,10,6,8,8,4},
    },
    ['בבלי'] = {
        ['פאה'] = 
            {0, 0, 0, 0, 0, 0, 0, 0},
        ['דמאי'] = 
            {0,0,0,0,0,0,0},
        ['ברכות'] = 
            {6, 9, 6, 6, 5, 8, 5, 8,5},
        ['שקלים'] = 
            {7, 5, 3, 5, 4, 4, 3, 4},
    },
}

p.koteret_lseif =  function (title, template)
    local sefer, maseket, pereq, seif = string.match(title, PATTERN_SEIF)
    local pereq_mispar = otiot2mispar(pereq)
    local seif_mispar = otiot2mispar(seif)
    local pereq_qodem = mispar2otiot(pereq_mispar-1)
    local pereq_haba  = mispar2otiot(pereq_mispar+1)
    local seif_qodem = mispar2otiot(seif_mispar-1)
    local seif_haba  = mispar2otiot(seif_mispar+1)
    
    local mispar_mishanyot = mispar_mishnayot_bepereq[sefer]
    if (mispar_mishanyot~=nil) then
        mispar_mishanyot = mispar_mishanyot[maseket]
    end

    -- חישוב פרק וסעיף קודם
    local pereq_wseif_qodem = pereq.." "..seif_qodem
    if seif_mispar==1 then
        if pereq_mispar==1 then  -- תחילת מסכת
            pereq_wseif_qodem = ""
        elseif mispar_mishanyot~=nil and mispar_mishanyot[pereq_mispar-1]~=nil then
            pereq_wseif_qodem = pereq_qodem.." "..mispar2otiot(mispar_mishanyot[pereq_mispar-1])
        end
    end

    -- חישוב פרק וסעיף הבא
    local pereq_wseif_haba  = pereq.." "..seif_haba
    if mispar_mishanyot~=nil and seif_mispar==mispar_mishanyot[pereq_mispar] and mispar_mishanyot[pereq_mispar]~=nil then
        if mispar_mishanyot[pereq_mispar+1]==nil then  -- סוף מסכת
            pereq_wseif_haba = ""
        else
            pereq_wseif_haba = pereq_haba.." ".."א"
        end
    end
    
    template = template:gsub('<ספר>', sefer);
    template = template:gsub("<מסכת>", maseket);
    template = template:gsub("<פרק>", pereq);
    template = template:gsub("<פרק קודם>", pereq_qodem);
    template = template:gsub("<פרק הבא>", pereq_haba);
    template = template:gsub("<סעיף>", seif);
    template = template:gsub("<סעיף קודם>", seif_qodem);
    template = template:gsub("<סעיף הבא>", seif_haba);
    template = template:gsub("<פרק וסעיף קודם>", pereq_wseif_qodem);
    template = template:gsub("<פרק וסעיף הבא>", pereq_wseif_haba);
    
    return template
end


-- הפוקנציה מקבלת שם של דף, מפרקת אותו לארבעה חלקים (ספר, מסכת, פרק, וסעיף/הלכה/משנה), ומכניסה את החלקים האלה לתבנית הנתונה.
-- למשל: אם שם הדף הוא "ירושלמי בבא קמא א ב",
-- והתבנית היא: "אאא <ספר> בבב <מסכת> גגג <פרק<"
-- אז הפונקציה תחזיר: "אאא ירושלמי בבב בבא קמא גגג א"
p["כותרת לסעיף"] = function (frame)
    local title = frame.args[1]
    local template = frame.args[2]
    return p.koteret_lseif(title, template)
end


p.koteret_lseif_bsefer =  function (title, template)
    local sefer, pereq, seif = string.match(title, PATTERN_SEIF_BSEFER)
    local seif_qodem = haqodem(seif);
    local seif_haba  = haba(seif);
    template = template:gsub('<ספר>', sefer);
    template = template:gsub("<פרק>", pereq);
    template = template:gsub("<סעיף>", seif);
    template = template:gsub("<סעיף קודם>",  seif_qodem);
    template = template:gsub("<סעיף הבא>",  seif_haba);
    return template
end


-- הפוקנציה מקבלת שם של דף, מפרקת אותו לשלושה
-- חלקים (ספר, פרק וסעיף), ומכניסה
-- את החלקים האלה לתבנית הנתונה.
p["כותרת לסעיף בספר"] = function (frame)
    local title = frame.args[1]
    local template = frame.args[2]
    return p.koteret_lseif_bsefer(title, template)
end



-- קלט: כותרת של דף של סעיף, למשל "ירושלמי בבא קמא א ב". וכן ספר אחר, לדוגמה "בבלי".
-- פלט: התבנית הנתונה בקלט, עם הנתונים של סעיף זה בספר המקביל, לדוגמה: "בבלי בבא קמא א ב".
-- אבל רק אם הסעיף המקביל אכן נמצא בספר השני. אם הסעיף לא נמצא - תוחזר מחרוזת ריקה.
p.seif_maqbil = function(title, template, sefer)
    local _, maseket, pereq, seif = string.match(title, PATTERN_SEIF)
    local pereq_mispar = otiot2mispar(pereq)
    local seif_mispar = otiot2mispar(seif)
    
    local mispar_mishanyot = mispar_mishnayot_bepereq[sefer]
    if (mispar_mishanyot~=nil) then
        mispar_mishanyot = mispar_mishanyot[maseket]
    end
    if (mispar_mishanyot~=nil) then
        mispar_mishnayot = mispar_mishanyot[pereq_mispar]
    end

    if (mispar_mishnayot~=nil and seif_mispar > mispar_mishnayot) then
        return "" -- הסעיף לא קיים בספר השני
    else
        template = template:gsub('<ספר>', sefer)
        template = template:gsub("<מסכת>", maseket)
        template = template:gsub("<פרק>", pereq)
        template = template:gsub("<סעיף>", seif)
        return template
    end
end

p["סעיף מקביל"] = function (frame)
    return p.seif_maqbil(frame.args[1], frame.args[2], frame.args[3])
end

-- בטבלה זו, בכל כניסה בטבלה, צריכה להיות רשימה של מספרי הפסוקים בכל פרק בספר.
---- select concat("['",sf.kotrt,"'] =<br/>        {",group_concat(count order by mspr),"},")
---- from
---- tnk.mspry_psuqim ms
---- inner join tnk.prqim pr on (ms.prq0=pr.kotrt)
---- inner join tnk.sfrim sf on (ms.sfr=sf.qod)
---- group by ms.sfr
local mispar_psuqim_bepereq = {
['איוב'] =
     {22,13,26,21,27,30,21,22,35,22,20,25,28,22,35,22,16,21,29,29,34,30,17,25,6,14,23,28,25,31,40,22,33,37,16,33,24,41,30,32,26,17},
['איכה'] =
     {22,22,66,22,22},
['במדבר'] =
     {54,34,51,49,31,27,89,26,23,36,35,16,33,45,41,35,28,32,22,29,35,41,30,25,18,65,23,31,39,17,54,42,56,29,34,13},
['בראשית'] =
     {31,25,24,26,32,22,24,22,29,32,32,20,18,24,21,16,27,33,38,18,34,24,20,67,34,35,46,22,35,43,54,33,20,31,29,43,36,30,23,23,57,38,34,34,28,34,31,22,33,26},
['דברים'] =
     {46,37,29,49,29,25,26,20,29,22,32,31,19,29,23,22,20,22,21,20,23,29,26,22,19,19,26,69,28,20,30,52,29,12},
['דברי הימים א'] =
     {54,55,24,43,41,66,40,40,44,14,47,41,14,17,29,43,27,17,19,8,30,19,32,31,31,32,34,21,30},
['דברי הימים ב'] =
     {18,17,17,22,14,42,22,18,31,19,23,16,23,14,19,14,19,34,11,37,20,12,21,27,28,23,9,27,36,27,21,33,25,33,27,23},
['דניאל'] =
     {21,49,33,34,30,29,28,27,27,21,45,13},
['הושע'] =
     {9,25,5,19,15,11,16,14,17,15,11,15,15,10},
['ויקרא'] =
     {17,16,17,35,26,23,38,36,24,20,47,8,59,57,33,34,16,30,37,27,24,33,44,23,55,46,34},
['זכריה'] =
     {17,17,10,14,11,15,14,23,17,12,17,14,9,21},
['חבקוק'] =
     {17,20,19},
['חגי'] =
     {15,23},
['יואל'] =
     {20,27,5,21},
['יהושע'] =
     {18,24,17,24,15,27,26,35,27,43,23,24,33,15,63,10,18,28,51,9,43,34,16,33},
['יחזקאל'] =
     {28,10,27,17,17,14,27,18,11,22,25,28,23,23,8,63,24,32,14,44,37,31,49,27,17,21,36,26,21,26,18,32,33,31,15,38,28,23,29,49,26,20,27,31,25,24,23,35},
['יונה'] =
     {16,11,10,11},
['ירמיהו'] =
     {19,37,25,31,31,30,34,23,25,25,23,17,27,22,21,21,27,23,15,18,14,30,40,10,38,24,22,17,32,25,39,44,26,22,19,32,21,28,18,16,18,22,13,30,5,28,7,47,39,46,64,34},
['ישעיהו'] =
     {31,22,26,6,30,13,25,23,20,34,16,6,22,32,9,14,14,7,25,6,17,25,18,23,12,21,13,29,24,33,9,20,24,17,10,22,38,22,8,31,29,25,28,28,25,13,15,22,26,11,23,15,12,17,13,12,21,14,21,22,11,12,19,11,25,24},
['מיכה'] =
     {16,13,12,14,14,16,20},
['מלכים א'] =
     {53,46,28,20,32,38,51,66,28,29,43,33,34,31,34,34,24,46,21,43,29,54},
['מלכים ב'] =
     {18,25,27,44,27,33,20,29,37,36,20,22,25,29,38,20,41,37,37,21,26,20,37,20,30},
['מלאכי'] =
     {14,17,24},
['משלי'] =
     {33,22,35,27,23,35,27,36,18,32,31,28,25,35,33,33,28,24,29,30,31,29,35,34,28,28,27,28,27,33,31},
['נחום'] =
     {14,14,19},
['נחמיה'] =
     {11,20,38,17,19,19,72,18,37,40,36,47,31},
['אסתר'] =
     {22,23,15,17,14,14,10,17,32,3},
['עובדיה'] =
     {21},
['עזרא'] =
     {11,70,13,24,17,22,28,36,15,44},
['עמוס'] =
     {15,16,15,13,27,14,17,14,15},
['צפניה'] =
     {18,15,20},
['קהלת'] =
     {18,26,22,17,19,12,29,17,18,20,10,14},
['רות'] =
     {22,23,18,22},
['שופטים'] =
     {36,23,31,24,31,40,25,35,57,18,40,15,25,20,20,31,13,31,30,48,25},
['שיר השירים'] =
     {17,17,11,16,16,12,14,14},
['שמואל א'] =
     {28,36,21,22,12,21,17,22,27,27,15,25,23,52,35,23,58,30,24,42,16,23,29,22,44,25,12,25,11,31,13},
['שמואל ב'] =
     {27,32,39,12,25,23,29,18,13,19,27,31,39,33,37,23,29,32,44,26,22,51,39,25},
['שמות'] =
     {22,25,22,31,23,30,29,28,35,29,10,51,22,31,27,36,16,27,25,22,37,30,33,18,40,37,21,43,46,38,18,35,23,35,35,38,29,31,43,38},
['תהלים'] =
     {6,12,9,9,13,11,18,10,21,18,7,9,6,7,5,11,15,51,15,10,14,32,6,10,22,12,14,9,11,13,25,11,22,23,28,13,40,23,14,18,14,12,5,27,18,12,10,15,21,23,21,11,7,9,24,14,12,12,18,14,9,13,12,11,14,20,8,36,37,6,24,20,28,23,11,13,21,72,13,20,17,8,19,13,14,17,7,19,53,17,16,16,5,23,11,13,12,9,9,5,8,29,22,35,45,48,43,14,31,7,10,10,9,8,18,19,2,29,176,7,8,9,4,8,5,6,5,6,8,8,3,18,3,3,21,26,9,8,24,14,10,8,12,15,21,10,20,14,9,6},
}

-- יוצר סרגל ניווט עם כל הפסוקים בפרק הנתון.
-- לדוגמה ראו:
-- [[תבנית:מצודות]]
p.niwut_psuqim_bpereq = function(sefer, pereq, template_for_pasuq)
    local pereq_mispar = otiot2mispar(pereq)
    local mispar_psuqim = mispar_psuqim_bepereq[sefer][pereq_mispar]
    local result = ''
    for pasuq_mispar = 1,mispar_psuqim do
    	local pasuq = mispar2otiot(pasuq_mispar)
    	local template = template_for_pasuq
	    template = template:gsub('<ספר>', sefer);
	    template = template:gsub("<פרק>", pereq);
	    template = template:gsub("<פסוק>", pasuq);
    	result = result .. template
	end
	return result
end

p["ניווט פסוקים בפרק"] = function (frame)
    local sefer = frame.args[1]
    local pereq = frame.args[2]
    local template_for_pasuq = frame.args[3]
    return p.niwut_psuqim_bpereq(sefer, pereq, template_for_pasuq)
end

-- פונקציות הקשורות לביאורי פסוקים
p.koteret_lbeur_pasuq =  function (title, template)
    local sefer, pereq, pasuq = string.match(title, PATTERN_BEUR_pasuq)
    local pereq_mispar = otiot2mispar(pereq)
    local pasuq_mispar = otiot2mispar(pasuq)
    local pereq_qodem = mispar2otiot(pereq_mispar-1)
    local pereq_haba  = mispar2otiot(pereq_mispar+1)
    local pasuq_qodem = mispar2otiot(pasuq_mispar-1)
    local pasuq_haba  = mispar2otiot(pasuq_mispar+1)

    local mispar_psuqim = mispar_psuqim_bepereq[sefer]

    -- חישוב פרק ופסוק קודם
    local pereq_wpasuq_qodem = pereq.." "..pasuq_qodem
    if pasuq_mispar==1 then
        if pereq_mispar==1 then  -- תחילת ספר
            pereq_wpasuq_qodem = ""
        elseif mispar_psuqim~=nil and mispar_psuqim[pereq_mispar-1]~=nil then
            pereq_wpasuq_qodem = pereq_qodem.." "..mispar2otiot(mispar_psuqim[pereq_mispar-1])
        end
    end

    -- חישוב פרק וסעיף הבא
    local pereq_wpasuq_haba  = pereq.." "..pasuq_haba
    if mispar_psuqim~=nil and mispar_psuqim[pereq_mispar]~=nil and pasuq_mispar==mispar_psuqim[pereq_mispar] then
        if mispar_psuqim[pereq_mispar+1]==nil then  -- סוף ספר
            pereq_wpasuq_haba = ""
        else
            pereq_wpasuq_haba = pereq_haba.." ".."א"
        end
    end
    
    template = template:gsub('<ספר>', sefer);
    template = template:gsub("<פרק>", pereq);
    template = template:gsub("<פסוק>", pasuq);
    template = template:gsub("<פרק קודם>", pereq_qodem);
    template = template:gsub("<פרק הבא>", pereq_haba);
    template = template:gsub("<פסוק קודם>",  pasuq_qodem);
    template = template:gsub("<פסוק הבא>", pasuq_haba);
    template = template:gsub("<פרק ופסוק קודם>", pereq_wpasuq_qodem);
    template = template:gsub("<פרק ופסוק הבא", pereq_wpasuq_haba);
    
    return template
end

-- הפוקנציה מקבלת שם של דף שהוא ביאור על פסוק, 
-- מפרקת אותו לשלושה חלקים (ספר, פרק ופסוק), ומכניסה 
-- את החלקים האלה לתבנית הנתונה.
p["כותרת לביאור פסוק"] = function (frame)
    local title = frame.args[1]
    local template = frame.args[2]
    return p.koteret_lbeur_pasuq(title, template)
end


return p