inherit
172351
0
Sept 5, 2019 10:56:35 GMT -8
DarkPikachu
Complexity == Fun
320
October 2011
tcll
|
Post by DarkPikachu on Oct 31, 2013 19:22:51 GMT -8
I just need to know the function that takes the text from the current editor and converts it to HTML
|
|
inherit
172351
0
Sept 5, 2019 10:56:35 GMT -8
DarkPikachu
Complexity == Fun
320
October 2011
tcll
|
Post by DarkPikachu on Nov 1, 2013 5:43:12 GMT -8
in recent news, this function apparently makes server requests. if your code wouldn't make thousands of calls to this function (making the server mistake a DDOS attack), use this:
*function pending, please wait*
otherwize, you're better off writing your own function... so here's my function which is based off the PB parser: (meaning it converts it exactly like the PB parser)
Header:
function Spoiler_action(div) { if (div.getElementsByTagName('div')[0].style.display != 'block') { div.getElementsByTagName('div')[0].style.display = 'block'; div.getElementsByTagName('a')[0].innerHTML = 'SPOILER: Click to hide'; } else { div.getElementsByTagName('div')[0].style.display = 'none'; div.getElementsByTagName('a')[0].innerHTML = 'SPOILER: Click to show'; } };
Footer:
function parse_quote_bbc(fields, quote_content) { var quote_author = /author="(.+?)"/g.exec(fields); var quote_timestamp = /timestamp="(.+?)"/g.exec(fields); var quote_source = /source="(.+?)"/g.exec(fields); var newDate = new Date(); newDate.setTime(quote_timestamp*1000 + newDate.getTimezoneOffset()*60000); var quote_date_string = newDate.toUTCString(); if (fields==null) { var quote_header = '<div class="quote no_header"><div class="quote_body">' } else { var quote_header = '<div class="quote" source="' quote_header += quote_source quote_header += '" timestamp="' quote_header += quote_timestamp quote_header += '" author="' quote_header += quote_author quote_header += '"><div class="quote_body"><div class="quote_avatar_container">' quote_header += '<div class="avatar-wrapper avatar_size_quote avatar-1">' quote_header += '<img src="http://images.proboards.com/v5/defaultavatar.png" alt="' quote_header += quote_author quote_header += ' Avatar"/></div></div><div class="quote_header"><a href="' quote_header += quote_source quote_header += '"><abbr data-timestamp="' quote_header += quote_timestamp quote_header += '" class="time" title="' quote_header += quote_date_string quote_header += '">' quote_header += quote_date_string quote_header += '</abbr></a><span itemscope="" itemtype="http://schema.org/Person">' quote_header += '<a href="/user/1" class="user-link" itemprop="url" title="' quote_header += quote_author quote_header += '" style="color: #cc3300;" data-icon-check="1"><span itemprop="name"> ' quote_header += quote_author quote_header += '</span></a></span> said:</div>' }; return quote_header+quote_content+'<div class="quote_clear"/></div></div>' }; function display_bbc(code) { code = code.replace(/\[/g, "[") code = code.replace(/\]/g, "]") return code };
function convert_bbc_to_html( str ) { //where str is a PB UBBC string //display formatting str = str.replace(/</g, "<") str = str.replace(/>/g, ">") str = str.replace(/\n/g, '<br/>') str = str.replace(/\[code\](.+?)\[\/code\]/g, display_bbc('<code>$1</code>') ); str = str.replace(/\[quote\](.+?)\[\/quote\]/g, parse_quote_bbc(null, "$1") ) str = str.replace(/\[quote(.+?)\](.+?)\[\/quote\]/g, parse_quote_bbc("$1", "$2") ) //old-style BBC tags str = str.replace(/\[size=(.+?)\]/g,'<font size="$1">'); str = str.replace(/\[\/size\]/g,'</font>'); str = str.replace(/\[color=(.+?)\]/g,'<font color="$1">'); str = str.replace(/\[\/color\]/g,'</font>'); str = str.replace(/\[u\]/g,'<u>'); str = str.replace(/\[\/u\]/g,'</u>'); str = str.replace(/\[s\]/g,'<s>'); str = str.replace(/\[\/s\]/g,'</s>'); str = str.replace(/\[b\]/g,'<strong>'); str = str.replace(/\[\/b\]/g,'</strong>'); str = str.replace(/\[hr\]/g,'<hr>'); str = str.replace(/\[\/hr\]/g,'</hr>'); str = str.replace(/\[url=(.+?)\](.+?)\[\/url\]/g,'<a href="$1" target=“_blank">$2</a>'); str = str.replace(/\[url\](.+?)\[\/url\]/g,'<a href="$1" target=“_blank">$1</a>'); str = str.replace(/\[img\](.+?)\[\/img\]/g,'<img src="$1" />'); str = str.replace(/\[spoiler\](.+?)\[\/spoiler\]/g, '<div style="cursor:pointer;cursor:hand" onclick="Spoiler_action(this)"><a href="javascript:void(0);">SPOILER: Click to show</a><div style="display: none;">$1</div></div>'); //HTML BBC tags str = str.replace(/\[font(.+?)\]/g,'<font $1>'); str = str.replace(/\[\/font\]/g,'</font>'); str = str.replace(/\[table\]/g,'<table>'); str = str.replace(/\[\/table\]/g,'</table>'); str = str.replace(/\[tbody\]/g,'<tbody>'); str = str.replace(/\[\/tbody\]/g,'</tbody>'); str = str.replace(/\[tr\]/g,'<tr>'); str = str.replace(/\[\/tr\]/g,'</tr>'); str = str.replace(/\[td\]/g,'<td>'); str = str.replace(/\[\/td\]/g,'</td>'); return str };
this function is portable, and is fast enough to update text upon key release, and accurate enough to produce results similar to actual post display.
issues: I've gotten no feedback about the fixes for code sections and quotes... - quotes display properly (following the forum's CSS), but their header doesn't contain valid information about the quote - code sections have no styling applied to them what so ever (even though the HTML is exactly the same)
spoilers only imitate the actual PB spoilers, so any modifications there should also not be applied.
|
|