-- convert_Lingo_2_CSS_Html
-----------------------------------
-- CREATED:
-- 2006
-- PROPERTIES:
--!memberProperties: [#name: "convert_Lingo_2_CSS_Html", #scripttype: #movie, #scriptSyntax: #lingo, #comments: "~/Documents/Scripts/lingo/convert_Lingo_2_CSS_Html.ls"]
--
-- DESCRIPTION:
--              Create HTML code from a lingo script -- c06 Alex da Franca -- alex@farbflash.de
--
-- REQUIRES:
--              xtra "pregex" for fast search and replace functionality
--
-- USAGE:
--              Copy the scripttext from a script editor into the clipboard
--              make a new #text member
--              open the new #textmember in the text editor
--              paste the contents of the clipboard (now the text member has the script text WITH the formatting)
--              use the below handler "mConvertCode2HTML" with the html of the textmember as argument to get a CSS html string
-----------------------------------

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-- uses the selected #text member of the stage movie

on mCopyConvert2HTML
  
  withoutHead = the optiondown or the controldown
  
  htmstring = ""
  tell the stage
    cl = the activecastlib
    sel = the selection of castlib cl
    if not sel.count then
      alert "Please copy+paste scripttext into a #text member and select the #text member to convert the lingo into html with CSS."
      exit
    end if
    memref = member(sel[1][1], cl)
    if [#text, #field].getPos(memref.type) < 1 then
      alert "Please copy+paste scripttext into a #text member and select the #text member to convert the lingo into html with CSS."
      exit
    end if
    htmstring = memref.html
  end tell
  
  if length(htmstring) > 0 then 
    theText = mConvertCode2HTML(htmstring, withoutHead)
    
    f = member("pastefeld")
    if ilk(f) <> #member then
      f = new(#field)
      f.name = "pastefeld"
    end if
    if f.type <> #field then
      f = new(#field)
      f.name = "pastefeld"
    end if
    
    f.text = theText
    f.copyToClipboard()
  end if
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mConvertCode2HTML srcText, withoutHead
  
  if xscr().mCheckForXtra("PregEx") <> 1 then
    alert "You need the PregEx Xtra to use this function"
    return srcText
  end if
  
  clrs = ["C80000", "0000C8", "004000", "404040"]
  
  retlist = [srcText]
  
  PRegEx_Replace(retlist, "<p>", "igm", "\n")
  PRegEx_Replace(retlist, "<br>", "igm", "")
  
  sstring = "<font[^\>]+?color=\"&QUOTE&"\#([^\"&QUOTE&"]+)\"&QUOTE&".*?>(.+?)</font>"
  PRegEx_ReplaceExec(retlist, sstring, "igm", #mConvertCode2HTMLCallback, [clrs])
  PRegEx_Replace(retlist, "<font[^\>]+?>(.+?)</font>", "igm", "\1")
  
  if withoutHead = 1 then
    PRegEx_Replace(retlist, ".+?<body.*?>", "igm", "")
    PRegEx_Replace(retlist, "<\/body>.*", "igm", "")
    PRegEx_Replace(retlist, "(\n+)<\/span>", "igm", "<\/span>\1")
    srcText = retlist[1]
    put "<pre id="&QUOTE&"code"&QUOTE&">" before srcText
    put "</pre>" after srcText
  else
    PRegEx_Replace(retlist, "(<body.*?>)", "igm", "\1\n<pre id=\"&QUOTE&"code\"&QUOTE&">")
    PRegEx_Replace(retlist, "(<\/body>)", "igm", "</pre>\n</body>")
    PRegEx_Replace(retlist, "(\n+)<\/span>", "igm", "<\/span>\1")
    
    srcText = retlist[1]
    
    css = ""
    
    lev = TAB
    put lev & "<style type="&QUOTE&"text/css"&QUOTE&">" & RETURN after css
    put TAB after lev
    put lev & "#code" & RETURN after css
    put lev & "{" & RETURN after css
    put TAB after lev
    put lev & "margin: 10.0px 5.0px 10.0px 20.0px;" & RETURN after css
    put lev & "font: 9.0px Monaco,Courier;" & RETURN after css
    put lev & "background-color: #EEEEEE;" & RETURN after css
    put lev & "padding: 10px;" & RETURN after css
    put lev & "border-color: #AAAAAA;" & RETURN after css
    put lev & "border-width: 1px 2px 2px 1px;" & RETURN after css
    put lev & "border-style: solid;" & RETURN after css
    put lev & "text-align: left;" & RETURN after css
    put lev & "overflow: auto;" & RETURN after css
    delete the last char of lev
    put lev & "}" & RETURN after css
    
    clrscnt = count(clrs)
    repeat with n = 1 to clrscnt
      put lev & "#code #clr"&n&" { color: #"&clrs[n]&"; }" & RETURN after css
    end repeat
    delete the last char of lev
    put lev & "</style>" & RETURN after css
    
    
    offs = offset("</head>", srcText)
    if offs > 0 then
      put css & RETURN before char offs of srcText
    end if
    
  end if
  
  return srcText
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mConvertCode2HTMLCallback paramList, retstring
  
  mstring = PRegEx_GetMatchString(0)
  
  clr = PRegEx_GetMatchString(1)
  if clr <> "000000" then
    pos = paramList.getPos(clr)
    if pos < 1 then
      paramList.add(clr)
      pos = count(paramList)
    end if
    
    retval = "<span id="&QUOTE&"clr"&pos&QUOTE&">"&PRegEx_GetMatchString(2)&"</span>"
  else
    retval = PRegEx_GetMatchString(2)
  end if
  
  return retval
end


