יחידה:ויקישור

מתוך ויקיפדיה, האנציקלופדיה החופשית

לשימוש ביחידה יש לקרוא לה בצורה: {{#invoke:ויקישור|פונקציה|פרמטרים}}.

פונקציות

הפונקציות להן ניתן לקרוא:

  • dewiki: ממיר [[קישור]] ל-קישור, [[קישור|טקסט]] ל-טקסט.
  • keepfile: השתמשו לפני dewiki (כלומר: {{#invoke:ויקישור|main|function=keepfile dewiki|text}}) בכדי לשמור מתמונה להפוך לטקסט. אחרת תמונות הופכות לטקסט ב-alt=, או אם זה לא קיים, מה שקיים בשדה האחרון של [[קובץ:|]] או [[תמונה:|]].
  • delink: ממיר [http://Link טקסט] ל-טקסט.
  • wikiall: ממיר "רצף של מילים" ל-[[רצף]] [[של]] [[מילים]]
  • wikiline: גם ממיר רצף של מילים לקישורים פנימיים, אך כל המילים בשורה אחת של טקסט הופכות לקישור פנימי אחד. שימו לב ששבירת שורה בודדת תוסר בתצוגה (כל המילים תופענה בשורה אחת) אבל עדיין שימושי למטרת קיבוץ מספר מילים לקישור אחד.
  • main: לא עושה כלום בעצמו. בשבילו ובשביל כל הפונקציות, המרכיב של הפרמטר function יופעל לאחר מכן.

פרמטרים

  • function=: מאפשר שימוש במספר פונקציות, שמופעלות בסדר לאחר הפונקציה (לא שהסדר משנה לרוב...). לכן {{#invoke:ויקישור|dewiki|function=delink|text}} קודם יעשה dewiki, ולאחר מכן delink על הטקסט.
  • nowiki=: כל דבר שאינו ריק יוביל לתגי <nowiki> לצידי הפלט.
  • text=: הטקסט שיש לעבד. הפרמטר text= חייב להיות בשימוש כאשר בטקסט עלול להיות =.

באגים

  • שימו לב ש-"[[" מקרי ימנע מפירוש הקריאה ליחידה אם הוא נכתב ישירות ל-#invoke, עם זאת זה לא יקרה אם יקראו לו בצורה מוכללת.

local p = {}

function proc(text,fcn) -- processes text sequentially according to a list of functions separated by spaces fcn
    text=text or ""
    fcn=fcn or ""
    local keepfile
    repeat
        fcnext=mw.ustring.match(fcn,"(%S+)")
        if fcnext then
            fcn=mw.ustring.gsub(fcn,fcnext,"") -- function names SHALL not contain pattern chars
            if fcnext=="dewiki" then          
                 -- protect all single [ and ] by temporarily converting to a password.  This allows links to remain inside file text while doing only a dewiki
                text=mw.ustring.gsub(text,"([^%[])%[([^%[])","%1<יחידה:ויקישור internal lsbracket token>%2")
                text=mw.ustring.gsub(text,"^%[([^%[])","<יחידה:ויקישור internal lsbracket token>%1")
                text=mw.ustring.gsub(text,"([^%[])%[$","%1<יחידה:ויקישור internal lsbracket token>")
                text=mw.ustring.gsub(text,"([^%]])%]([^%]])","%1<יחידה:ויקישור internal rsbracket token>%2")
                text=mw.ustring.gsub(text,"^%]([^%]])","<יחידה:ויקישור internal rsbracket token>%1")
                text=mw.ustring.gsub(text,"([^%]])%]$","%1<יחידה:ויקישור internal rsbracket token>")
                if keepfile then
                    text=mw.ustring.gsub(text,"%[%[%s*קובץ:([^%[%]])%]%]","<יחידה:ויקישור internal filestart token>%1<יחידה:ויקישור internal 2rsbracket token>")
                    text=mw.ustring.gsub(text,"%[%[%s*תמונה:([^%[%]])%]%]","<יחידה:ויקישור internal imagestart token>%1<יחידה:ויקישור internal 2rsbracket token>")                
                end

                 -- process קובץ: and תמונה: links, using alt text when available
                text=mw.ustring.gsub(text,"%[%[קובץ:[^%[%]]-|%s*alt=([^%[%]|]-)|[^%[%]]-%]%]","%1") -- case for קובץ: where alt= text is present but not at end
                text=mw.ustring.gsub(text,"%[%[תמונה:[^%[%]]-|%s*alt=([^%[%]|]-)|[^%[%]]-%]%]","%1") -- case for תמונה: where alt= text is present but not at end
                text=mw.ustring.gsub(text,"%[%[קובץ:[^%[%]]-|%s*alt=([^%[%]|]-)%]%]","%1") -- case for קובץ: where alt= text is present at end
                text=mw.ustring.gsub(text,"%[%[תמונה:[^%[%]]-|%s*alt=([^%[%]|]-)%]%]","%1") -- case for תמונה: where alt= text is present at end
                
                text=mw.ustring.gsub(text,"%[%[[^%[%]]-|([^%[%]|]-)%]%]","%1") -- link, text separated by "|".  Handles case of קובץ: when no alt= is specified, -assuming- last field is the legend
                text=mw.ustring.gsub(text,"%[%[([^%[%]|]-)%]%]","%1") -- link with no funny |s at all
                 -- deprotect all tokens
                text=mw.ustring.gsub(text,"<יחידה:ויקישור internal lsbracket token>","[")
                text=mw.ustring.gsub(text,"<יחידה:ויקישור internal rsbracket token>","]")
                text=mw.ustring.gsub(text,"<יחידה:ויקישור internal filestart token>","[[קובץ:")
                text=mw.ustring.gsub(text,"<יחידה:ויקישור internal imagestart token>","[[תמונה:")
                text=mw.ustring.gsub(text,"<יחידה:ויקישור internal 2rsbracket token>","]]")
                
            elseif fcnext=="delink" then
                text=mw.ustring.gsub(text,"%[%s*http://%S*%s+([^%]%[]+)%]","%1")
            elseif fcnext=="wikiall" then
                text=mw.ustring.gsub(text,"(%S+)","[[%1]]")
            elseif fcnext=="wikiline" then
                text=mw.ustring.gsub(text,"([^\n]+)","[[%1]]")
            elseif fcnext=="keepfile" then
                keepfile=true
            end -- if fcnext==etc.
        end -- if fcnext
    until not fcnext
    return text --- when all functions are used up (or no valid function given) return the input text straight
    end
    
function p.main(frame,fcn) -- gets the parameters and sets up to call proc
    local args=frame.args
    local parent=frame.getParent(frame)
    if parent then pargs=parent.args else pargs={} end
    local text=args.text or args[1] or pargs.text or pargs[1] or ""
    local nowiki=args.nowiki or pargs.nowiki
    fcn=(fcn or "") ..  (args["function"] or pargs["function"] or "")  
    --local page=mw.title.getCurrentTitle() --- in the long run this cheesy tactic won't fly - I should rewrite as a proper module and recommend Module:Page invocation in the #invoke instead.
    --if page then text=page.getContent(page) else return "error didn't get the page contents :(" end
    text=proc(text,fcn)
    if nowiki then text="<nowiki>" .. text .. "</nowiki>" end
    return frame.preprocess(frame,text)
end

 --- the function "parameter" is added to the beginning of the list of functions to be performed as specified by "function =".
function p.dewiki(frame)
    return p.main(frame,"dewiki ")
end

function p.delink(frame)
    return p.main(frame,"delink ")
end

function p.wikiall(frame)
    return p.main(frame,"wikiall ")
end

function p.wikiline(frame)
    return p.main(frame,"wikiline ")
end

function p.keepfile(frame)
    return p.main(frame,"keepfile ")
end

function p.keepfile_dewiki(frame)
    return p.main(frame,"keepfile dewiki")
end
return p