Advertisement
Guest User

HTML to BB

a guest
Nov 6th, 2011
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2.             function convert(htmlText)
  3.             {
  4.                 // maybe they put spaces around the = .  Remove them
  5.                 htmlText = htmlText.replace(/ = /gi, "=");
  6.                 // maybe they put quotes surrounding the value
  7.                 // so look for =" and ">
  8.                 htmlText = htmlText.replace(/=\"/gi, "=");
  9.                 //bbcodetext = bbcodetext.replace(/\">/gi, ">");
  10.                 // maybe they put single quote surrounding the value
  11.                 // so look for =' and '>
  12.                 htmlText = htmlText.replace(/=\'/gi, "="); 
  13.                 htmlText = htmlText.replace(/<A\s*HREF/i, "<A HREF");
  14.    
  15.                 htmlText = htmlText.replace(/<CODE>/gi, "[code]");
  16.                 htmlText = htmlText.replace(/<\/CODE>/gi, "[/code]");
  17.                 htmlText = do_quote(htmlText);
  18.                
  19.                 htmlText = htmlText.replace(/<A HREF/i, "[url");
  20.                 htmlText = htmlText.replace(/<\/A>/i, "[/url]");
  21.                 // This replaces all remaining HTML code between < and >
  22.                 htmlText = htmlText.replace(/<[^>]*>/g, "");
  23.  
  24.                 // This will convert all > into ] because we left
  25.                 // some open
  26.                 htmlText = htmlText.replace(/>/g, "]");
  27.                 htmlText = htmlText.replace(/\'>/g, "]");
  28.                 htmlText = htmlText.replace(/\">/g, "]");
  29.                 htmlText = htmlText.replace(/\']/g, "]");
  30.                 htmlText = htmlText.replace(/\"]/g, "]");
  31.    
  32.                 return htmlText;
  33.             }
  34.            
  35.             function do_quote(text)
  36.             {
  37.                 text = text.replace(/<BLOCKQUOTE>/gi, "[quote");
  38.                 text = text.replace(/<BLOCKQUOTE /gi, "[quote");
  39.                 text = text.replace(/<\/BLOCKQUOTE>/gi, "[/quote]");
  40.                 var i = 0;
  41.                 var pos;
  42.                 var citeFound = false;
  43.                 //Firstly remove cite= and its value
  44.                 for (pos = 0; pos != -1; pos)
  45.                 {
  46.                     pos = text.indexOf("cite=", pos);
  47.                     if (pos != -1)  // if cite= is found
  48.                     {  
  49.                         text = text.replace("cite", "")
  50.                         pos++;
  51.                         i++;
  52.                         citeFound = true;
  53.                     }  // end if <cite> is found
  54.                     else
  55.                     {
  56.                         //Add hardbracket after 'quote'
  57.                         text = text.replace("[quote","[quote]");
  58.                     }
  59.                 }
  60.                
  61.                 for (pos = 0; pos != -1; pos)
  62.                 {
  63.                     pos = text.indexOf("<cite>", pos);
  64.                     if (pos != -1)  // if <cite> is found
  65.                     {  
  66.                         text = text.replace("[quote]","[quote");
  67.                         pos = text.indexOf("<cite>", 0);
  68.                         text = text.replace("<cite>", "")
  69.                         var quote = text.substring(pos,text.indexOf("</cite>", pos));
  70.                         text = text.replace(quote, "");
  71.                         if(!citeFound)
  72.                         {
  73.                             //Removing the anchor tags if any
  74.                             quote = quote.replace(/<A HREF=/i, "");
  75.                             quote = quote.replace(/<\/A>/i, "");
  76.                             //Removing newz.dk anoying "Kilde"
  77.                             quote = quote.replace("Kilde", "");
  78.                             quote = quote.replace("\">", "");
  79.                             text = text.replace("[quote", "[quote="+quote+"]");
  80.                         }
  81.                         pos++;
  82.                         i++;           
  83.                     }  // end if <cite> is found
  84.                 }  // end for
  85.                 return text;
  86.             }
  87.         </script>
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement