Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help creating custom CodeDefinition #30

Open
pedjas opened this issue Oct 14, 2014 · 3 comments
Open

Need help creating custom CodeDefinition #30

pedjas opened this issue Oct 14, 2014 · 3 comments

Comments

@pedjas
Copy link

pedjas commented Oct 14, 2014

Hi,

i want to creade BBCode which looks like this:

[insertimg imgid=22 style=left border=1 alt="alttext"]

Point is, instead of image url, i need image id so I can buildimage name (finally, I should read image info from database) and provide proper HTML markup to show that image.

I made more or less everything that I needed except, i cannot use this BBCode without closing tag [/insertimg]. i want to get rid of that closing tag. I read all I could and searched for answer but could not get it.

Basicaly, now I have to put tag in this form:
[insertimg imgid=22 style=left border=1 alt="alttext"][/insertimg]
but i want it to be simpler, this form:
[insertimg imgid=22 style=left border=1 alt="alttext"]

Here is my code:

class bbcInsertImg extends \JBBCode\CodeDefinition {



  public function __construct()  {
    $this->parseContent = false;
    $this->useOption = true;
    $this->setTagName('insertimg');
    $this->nestLimit = -1;
  }

  public function asHtml(\JBBCode\ElementNode $pElement) {

  $m_attributes = $pElement->getAttribute();

    if (isset ($m_attributes['imgid'])) {
      $img_url = $m_attributes['imgid'] . '.jpg';
    } else {
      $img_url = '';
    }

    $bodyHtml = '<img src="' . $img_url . '"';

    $bodyHtml .= $this->AttribToHTML($pElement, 'style');
    $bodyHtml .= $this->AttribToHTML($pElement, 'border');
    $bodyHtml .= $this->AttribToHTML($pElement, 'alt');    

    $bodyHtml .= '>';    

    return $bodyHtml;

  }

  private function AttribToHTML($pElement, $pAttrib, $pDefaultValue = '') {

    $m_attributes = $pElement->getAttribute();

    if (isset ($m_attributes[$pAttrib])) {
      $m_result = ' ' . $pAttrib . '="' . $m_attributes[$pAttrib] . '"';
    } else {
      if (strlen ($pDefaultValue) > 0) {
        $m_result = ' ' . $pAttrib . '="' . $pDefaultValue . '"';      
      } else {
        $m_result = '';
      }
    }
    return $m_result;
  }

}
@pedjas pedjas changed the title Need hel creating custom CodeDefinition Need help creating custom CodeDefinition Oct 14, 2014
@pedjas
Copy link
Author

pedjas commented Mar 4, 2015

I am still struggling with this issue. Can anybody give me some hint?

@Kubo2
Copy link
Contributor

Kubo2 commented Apr 10, 2015

@pedjas As far as I know, jBBCode does not have support for unary tags yet.
@jbowens Probably this one should be labeled as #bug, shouldn't it?

@shmax
Copy link
Contributor

shmax commented Apr 10, 2015

Until support for unary tags is added, why not use the positive qualities of the closed-tag format to your benefit?

[includeimg=22 style=left border=1]This is my alt text![/includeimg]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants