Modul:forcesubstitute

Definition från Wiktionary, den fria ordlistan.
Hoppa till navigering Hoppa till sök

Dokumentation för denna modul finns på /dok (redigera), /test


Syfte[redigera]

Genom att konvertera HTML tillbaka till wikikod hjälper att substituera innehåll som annars inte går att substituera utan problem. Substitueras alltid medelst subst:. Används på sidan Wiktionary:Balans efter språk och ordklass.

Parametrar[redigera]

  • (obligatorisk) "webhtml="
  • (obligatorisk) "beginpattern="
  • (obligatorisk) "endpattern="
  • (frivillig) "beginskip="
  • (frivillig) "endskip="
  • (frivillig) "nowiki=true"

Felhantering[redigera]

5 olika fel kan detekteras och påtalas. Ingen spårningskategori.

Behövda submoduler[redigera]

Inga.

-- obligatory named parameter "webhtml=" (embedded JS containing "}}" or "|" must be removed)
-- obligatory named parameter "beginpattern=" (inclusive)
-- obligatory named parameter "endpattern=" (inclusive)
-- optional named parameter "beginskip="
-- optional named parameter "endskip="
-- optional named parameter "nowiki=true"

local ooo = {}

local const_aennu = string.char(195,164) .. "nnu"

function ooo.go (arxframent)

  local arxourown = 0

  local temp_table = {}

  local web_html = '' -- min 16 chars
  local begin_pattern = '' -- min 6 chars
  local end_pattern = '' -- min 6 chars
  local begin_skip = '' -- "0" ... "8"
  local end_skip = '' -- "0" ... "8"

  local store_string = '' -- one char or longer hit
  local skip_string_a = ''
  local skip_string_b = ''
  local skip_string_c = ''

  local wiki_html = ''
  local strret = ''

  local error_code = 0 -- 0 OK | 1 2 para | 3 beg nofou | 4 end nofou | 5 brkn

  local web_html_length = 0
  local begin_pa_length = 0
  local end_pa_length = 0

  local multi_search_pos = 0
  local num_begin_skip = 0 -- from "begin_skip"
  local num_end_skip = 0 -- from "end_skip"
  local main_index = 0
  local temp_index = 0

  local skip_position_a = 0
  local skip_position_b = 0
  local skip_position_c = 0
  local skip_length_a = 0
  local skip_length_b = 0
  local skip_length_c = 0

  local boo_nowiki = false -- parameter

  local function compareAtPosition (main_position, reference_string) -- uses one upvalue "web_html"

    local main_length = 0
    local reference_length = 0
    local inner_index = 0
    local incoming_char = 0
    local reference_char = 0
    local boo_equal = true

    main_length = string.len(web_html) -- upvalue
    reference_length = string.len(reference_string)

    if ((main_position+reference_length)>main_length) then
      boo_equal = false -- no chance
    else
      while true do
        if (inner_index>=reference_length) then
          break -- found a match
        end--if
        incoming_char = string.byte(web_html,(main_position+inner_index+1),(main_position+inner_index+1)) -- upvalue
        reference_char = string.byte(reference_string,(inner_index+1),(inner_index+1))
        if (incoming_char~=reference_char) then
          boo_equal = false
          break -- give up immediately
        end--if
        inner_index = inner_index + 1 -- ZERO-based
      end--while
    end--if

    return boo_equal

  end--function compareAtPosition

  arxourown = arxframent.args

  web_html      = arxourown["webhtml"] or ''
  begin_pattern = arxourown["beginpattern"] or ''
  end_pattern   = arxourown["endpattern"] or ''
  begin_skip    = arxourown["beginskip"] or ''
  end_skip      = arxourown["endskip"] or ''
  boo_nowiki    = (arxourown["nowiki"]=="true")

  while true do -- fake upper loop

    web_html_length = string.len(web_html)
    begin_pa_length = string.len(begin_pattern)
    end_pa_length   = string.len(end_pattern)

    if (web_html_length==0) or (begin_pa_length==0) or (end_pa_length==0) then
      error_code = 1 -- param missing
      break
    end--if
    if (web_html_length<16) or (begin_pa_length<6) or (end_pa_length<6) then
      error_code = 2 -- too short
      break
    end--if
    num_begin_skip = tonumber (begin_skip) or 0
    num_end_skip = tonumber (end_skip) or 0
    if (num_begin_skip<0) or (num_begin_skip>8) or (num_end_skip<0) or (num_end_skip>8) then
      error_code = 2 -- out of range
      break
    end--if

    multi_search_pos = 0 -- ONE-based preceding
    while true do -- genuine begin trim loop
      multi_search_pos = string.find (web_html,begin_pattern,(multi_search_pos+1),true)
      if not multi_search_pos then
        error_code = 3 -- begin not found
        break
      end--if
      if (num_begin_skip==0) then
        break -- done begin
      end--if
      num_begin_skip = num_begin_skip - 1
    end--while
    if (error_code~=0) then
      break -- abort fake upper loop too
    end--if
    web_html = string.sub (web_html,multi_search_pos,web_html_length)
    web_html_length = string.len(web_html)

    if (web_html_length<16) then
      error_code = 2 -- too short after begin trim
    end--if
    if (error_code~=0) then
      break -- fake upper loop
    end--if

    multi_search_pos = 0 -- ONE-based preceding
    while true do -- genuine end trim loop
      multi_search_pos = string.find (web_html,end_pattern,(multi_search_pos+1),true)
      if not multi_search_pos then
        error_code = 4 -- end not found
        break
      end--if
      if (num_end_skip==0) then
        break -- done end
      end--if
      num_end_skip = num_end_skip - 1
    end--while
    if (error_code~=0) then
      break -- abort fake upper loop too
    end--if
    web_html = string.sub (web_html,1,(multi_search_pos+end_pa_length-1))
    web_html_length = string.len(web_html)

    if (web_html_length<16) then
      error_code = 2 -- too short after end trim
    end--if

    break
  end--while -- fake upper loop

  main_index = 0 -- ZERO-based
  temp_index = 1 -- ONE-based and runs independently from main index

  while true do -- genuine outer loop over the text

    if (error_code~=0) or (main_index>=web_html_length) then
      break -- failure or done
    end--if

    store_string = ''

    while true do -- fake inner loop

      if (compareAtPosition (main_index,'<tbody>')) then
        main_index = main_index + 7
        break -- skip it
      end--if

      if (compareAtPosition (main_index,'</tbody>')) then
        main_index = main_index + 8
        break -- skip it
      end--if

      if (compareAtPosition (main_index,'<a href="/w')) then
        skip_position_a = string.find (web_html,'title="',(main_index+1),true)
        if (skip_position_a==nil) then
          error_code = 5
          break
        end--if
        main_index = skip_position_a + 6
        skip_string_a = ' (inte skapad ' .. const_aennu .. ')">'
        skip_string_b = '">'
        skip_length_a = string.len(skip_string_a)
        skip_length_b = string.len(skip_string_b)
          skip_position_a = string.find (web_html,skip_string_a,(main_index+1),true) or web_html_length
          skip_position_b = string.find (web_html,skip_string_b,(main_index+1),true) or web_html_length
          if (skip_position_b<skip_position_a) then
            skip_position_a = skip_position_b -- min stored in "a" now
            skip_length_a = skip_length_b -- don't forget to copy the length to "a" too
          end--if
          if (skip_position_a>=web_html_length) then
            error_code = 5
            break
          end--if
          store_string = string.sub(web_html,(main_index+1),(skip_position_a-1)) .. "|"
          main_index = skip_position_a
          skip_string_c = '</a>'
        skip_length_c = string.len(skip_string_c)
        skip_position_a = string.find (web_html,skip_string_c,main_index,true)
        if (skip_position_a==nil) then
          error_code = 5
          break
        end--if
        store_string = store_string .. string.sub(web_html,(main_index+skip_length_a),(skip_position_a-1))
        main_index = skip_position_a + skip_length_c - 1
        if (string.sub(store_string,1,9)=="Kategori:") then
          store_string = ":" .. store_string -- colon rule
        end--if
        store_string = "[[" .. store_string .. "]]"
        break -- store a wikilink
      end--if

      store_string = string.sub(web_html,(main_index+1),(main_index+1)) -- if nothing found then just copy 1 char
      main_index = main_index + 1

      break
    end--while -- fake inner loop

    if (error_code~=0) then
      break -- genuine outer loop over the text
    end--if

    if (store_string~='') then
      temp_table[temp_index] = store_string
      temp_index = temp_index + 1 -- ONE-based
      if (temp_index==21) then
        wiki_html = wiki_html .. table.concat(temp_table)
        temp_table = {}
        temp_index = 1 -- ONE-based and runs independently from main index
      end--if
    end--if

  end--while -- genuine outer loop over the text

  wiki_html = wiki_html .. table.concat(temp_table)

  if (error_code==0) then
    if (boo_nowiki) then
      strret = mw.text.nowiki(wiki_html)
    else
      strret = wiki_html
    end--if
  end--if

  if (error_code==1) then
    strret = 'Obligatory named parameters (3, see documentation) not delivered.'
  end--if
  if (error_code==2) then
    strret = 'Input text or parameter too short or out of range.'
  end--if
  if (error_code==3) then
    strret = 'Begin pattern not found.'
  end--if
  if (error_code==4) then
    strret = 'End pattern not found.'
  end--if
  if (error_code==5) then
    strret = 'Incoming HTML is broken.'
  end--if
  if (error_code~=0) then
    strret = "ERROR: " .. strret
  end--if

  return strret

end

return ooo