Skip to content

Commit

Permalink
Correct formatting in preview.
Browse files Browse the repository at this point in the history
  • Loading branch information
dabidan committed Nov 26, 2015
1 parent 4eb1ac9 commit cf08910
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 48 deletions.
20 changes: 11 additions & 9 deletions ext/o0johntam0o/codeboxplus/event/main_listener.php
Expand Up @@ -60,6 +60,7 @@ static public function getSubscribedEvents()
'core.posting_modify_template_vars' => 'posting_event',
'core.viewtopic_post_rowset_data' => 'viewtopic_event',
'core.modify_format_display_text_after' => 'message_parser_event',
'core.modify_text_for_display_after' => 'message_parser_event',
);
}

Expand Down Expand Up @@ -90,7 +91,9 @@ public function message_parser_event($event)
if (isset($event['text']))
{
$text = $event['text'];
$text = preg_replace('#' . preg_quote('<div class="codebox" title="' . $this->user->lang['CODEBOX_PLUS_TITLE'] . '"><p>') . '.*?' . preg_quote('</code></div>') . '#msi', $this->codebox_template('NULL', 'NULL'), $text);
$post_id = 0;
$part = 0;
$text = preg_replace('#<div class="codebox" title="' . preg_quote($this->user->lang['CODEBOX_PLUS_TITLE']) . '" data-language="(.*?)" data-filename="(.*?)"><p>.*?</p><code>(.*?)</code></div>#msie', "\$this->codebox_template(preg_replace('#\<br\\s*/?\>#msi', '\n', '\$3'), '\$1', '\$2', \$post_id, ++\$part)", $text);
$event['text'] = $text;
}
}
Expand Down Expand Up @@ -131,12 +134,7 @@ public function viewtopic_event($event)
$bbcode_uid = isset($rowset_data['bbcode_uid']) ? $rowset_data['bbcode_uid'] : '';
$post_id = isset($rowset_data['post_id']) ? $rowset_data['post_id'] : 0;
$part = 0;

while (preg_match("#\[codebox=[a-z0-9_-]+ file=(.*?):" . $bbcode_uid . "\](.*?)\[/codebox:" . $bbcode_uid . "\]#msi", $post_text))
{
$part++;
$post_text = preg_replace("#\[codebox=([a-z0-9_-]+) file=(.*?):" . $bbcode_uid . "\](.*?)\[/codebox:" . $bbcode_uid . "\]#msie", "\$this->codebox_template('\$3', '\$1', '\$2', \$post_id, \$part)", $post_text, 1);
}
$post_text = preg_replace("#\[codebox=(.*?) file=(.*?):$bbcode_uid\](.*?)\[/codebox:$bbcode_uid\]#msie", "\$this->codebox_template('\$3', '\$1', '\$2', \$post_id, ++\$part)", $post_text);

if (isset($rowset_data['post_text']) && $part > 0)
{
Expand Down Expand Up @@ -188,7 +186,7 @@ public function codebox_template($code = '', $lang = 'text', $file = '', $id = 0
$re .= '<a href="#" onclick="codebox_plus_select(this, 1); return false;">[' . $this->user->lang['SELECT_ALL_CODE'] . ']</a>';
$re .= '&nbsp;<a href="#" onclick="codebox_plus_toggle(this, 1); return false;">[' . $this->user->lang['CODEBOX_PLUS_EXPAND'] . '/' . $this->user->lang['CODEBOX_PLUS_COLLAPSE'] . ']</a>';

if ($this->download_enabled && $lang != 'NULL')
if ($id != 0 && $this->download_enabled && $lang != 'NULL')
{
$re .= '&nbsp;<a href="' . $this->helper->route('o0johntam0o_codeboxplus_download_controller', array('id' => $id, 'part' => $part)) . '" onclick="window.open(this.href); return false;">';
$re .= '[' . $this->user->lang['CODEBOX_PLUS_DOWNLOAD'] . ']</a> ' . '('. $file . ')';
Expand Down Expand Up @@ -221,11 +219,15 @@ private function codebox_parse_code($code = '', $lang = 'text')
{
return '';
}
// Remove newline at the beginning
// Remove newline at the beginning and ending
if (!empty($code) && $code[0] == "\n")
{
$code = substr($code, 1);
}
if (!empty($code) && substr($code, -1) == "\n")
{
$code = substr($code, 0, -1);
}

if ($this->syntax_highlighting_enabled)
{
Expand Down
73 changes: 34 additions & 39 deletions ext/o0johntam0o/codeboxplus/migrations/v200.php
Expand Up @@ -51,52 +51,47 @@ public function update_data()
// CUSTOM ==============================================================
public function install_bbcode_codebox()
{
$sql = 'SELECT bbcode_id FROM ' . $this->table_prefix . 'bbcodes WHERE LOWER(bbcode_tag) = "codebox="';
$sql = 'DELETE FROM ' . $this->table_prefix . 'bbcodes WHERE LOWER(bbcode_tag) = "codebox="';
$this->db->sql_query($sql);

// Create new BBCode
$sql = 'SELECT MAX(bbcode_id) AS max_bbcode_id FROM ' . $this->table_prefix . 'bbcodes';
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if (!$row)

if ($row)
{
// Create new BBCode
$sql = 'SELECT MAX(bbcode_id) AS max_bbcode_id FROM ' . $this->table_prefix . 'bbcodes';
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);

if ($row)
{
$bbcode_id = $row['max_bbcode_id'] + 1;
$bbcode_id = $row['max_bbcode_id'] + 1;

// Make sure it is greater than the core BBCode ids...
if ($bbcode_id <= NUM_CORE_BBCODES)
{
$bbcode_id = NUM_CORE_BBCODES + 1;
}
}
else
// Make sure it is greater than the core BBCode ids...
if ($bbcode_id <= NUM_CORE_BBCODES)
{
$bbcode_id = NUM_CORE_BBCODES + 1;
}

if ($bbcode_id <= BBCODE_LIMIT)
{
$this->db->sql_query('INSERT INTO ' . $this->table_prefix . 'bbcodes ' . $this->db->sql_build_array(
'INSERT',
array(
'bbcode_tag' => 'Codebox=',
'bbcode_id' => (int) $bbcode_id,
'bbcode_helpline' => '',
'display_on_posting' => 0,
'bbcode_match' => '[Codebox={SIMPLETEXT1} file={SIMPLETEXT2}]{TEXT}[/Codebox]',
'bbcode_tpl' => '<div class="codebox" title="{L_CODEBOX_PLUS_TITLE}"><p>{L_CODE}{L_COLON} <a href="#" onclick="selectCode(this); return false;">{L_SELECT_ALL_CODE}</a></p><code>{TEXT}</code></div>',
'first_pass_match' => '!\[codebox\=([a-zA-Z0-9-+.,_ ]+) file\=([a-zA-Z0-9-+.,_ ]+)\](.*?)\[/codebox\]!ies',
'first_pass_replace' => '\'[codebox=${1} file=${2}:$uid]\'.str_replace(array("\\r\\n", \'\\"\', \'\\\'\', \'(\', \')\'), array("\\n", \'"\', \'&#39;\', \'&#40;\', \'&#41;\'), trim(\'${3}\')).\'[/codebox:$uid]\'',
'second_pass_match' => '!\[codebox\=([a-zA-Z0-9-+.,_ ]+) file\=([a-zA-Z0-9-+.,_ ]+):$uid\](.*?)\[/codebox:$uid\]!s',
'second_pass_replace' => '<div class="codebox" title="{L_CODEBOX_PLUS_TITLE}"><p>{L_CODE}{L_COLON} <a href="#" onclick="selectCode(this); return false;">{L_SELECT_ALL_CODE}</a></p><code>${3}</code></div>'
)
));
}
}
else
{
$bbcode_id = NUM_CORE_BBCODES + 1;
}

if ($bbcode_id <= BBCODE_LIMIT)
{
$this->db->sql_query('INSERT INTO ' . $this->table_prefix . 'bbcodes ' . $this->db->sql_build_array(
'INSERT',
array(
'bbcode_tag' => 'Codebox=',
'bbcode_id' => (int) $bbcode_id,
'bbcode_helpline' => '',
'display_on_posting' => 0,
'bbcode_match' => '[Codebox={SIMPLETEXT1} file={SIMPLETEXT2}]{TEXT}[/Codebox]',
'bbcode_tpl' => '<div class="codebox" title="{L_CODEBOX_PLUS_TITLE}" data-language="{SIMPLETEXT1}" data-filename="{SIMPLETEXT2}"><p>{L_CODE}{L_COLON} <a href="#" onclick="selectCode(this); return false;">{L_SELECT_ALL_CODE}</a></p><code>{TEXT}</code></div>',
'first_pass_match' => '!\[codebox\=([a-zA-Z0-9-+.,_ ]+) file\=([a-zA-Z0-9-+.,_ ]+)\](.*?)\[/codebox\]!ies',
'first_pass_replace' => '\'[codebox=${1} file=${2}:$uid]\'.str_replace(array("\\r\\n", \'\\"\', \'\\\'\', \'(\', \')\'), array("\\n", \'"\', \'&#39;\', \'&#40;\', \'&#41;\'), \'${3}\').\'[/codebox:$uid]\'',
'second_pass_match' => '!\[codebox\=([a-zA-Z0-9-+.,_ ]+) file\=([a-zA-Z0-9-+.,_ ]+):$uid\](.*?)\[/codebox:$uid\]!s',
'second_pass_replace' => '<div class="codebox" title="{L_CODEBOX_PLUS_TITLE}" data-language="${1}" data-filename="${2}"><p>{L_CODE}{L_COLON} <a href="#" onclick="selectCode(this); return false;">{L_SELECT_ALL_CODE}</a></p><code>${3}</code></div>'
)
));
}
}
}
}

0 comments on commit cf08910

Please sign in to comment.