PHP Class Markdownify\Converter

Show file Open project: pixel418/markdownify Class Usage Examples

Protected Properties

Property Type Description
$allowMixedChildren array html block tags that allow inline & block children
$bodyWidth integer wrap output, set to 0 to skip wrapping
$buffer array stores current buffers
$drop array html tags to be dropped (contents will not be parsed!)
$escapeInText array list of chars which have to be escaped in normal text
$footnotes array stores current buffers
$ignore array html tags to be ignored (contents will be parsed)
$indent string current indentation
$isMarkdownable array tags with elements which can be handled by markdown
$keepHTML boolean keep html tags which cannot be converted to markdown
$lastClosedTag string name of last closed tag
$lastWasBlockTag boolean wether last processed node was a block tag or not
$lineBreaks number of line breaks before next inline output
$linkPosition integer position where the link reference will be displayed
$minBodyWidth integer minimum body width
$notConverted array stack with tags which where not converted to html
$output string markdown output
$parser parseHTML html parser object
$skipConversion boolean skip conversion to markdown
$stack array node stack, e.g. for and tags
$wrappableIndents array Markdown indents which could be wrapped

Public Methods

Method Description
__construct ( integer $linkPosition = self::LINK_AFTER_CONTENT, integer $bodyWidth = MDFY_BODYWIDTH, boolean $keepHTML = MDFY_KEEPHTML ) : void constructor, set options, setup parser
parseString ( string $html ) : string parse a HTML string
setKeepHTML ( $keepHTML ) : void set keep HTML tags which cannot be converted to markdown
setLinkPosition ( integer $linkPosition ) : void set the position where the link reference will be displayed

Protected Methods

Method Description
_decode_hex ( array $matches ) : string callback for decode() which converts a hexadecimal entity to UTF-8
_decode_numeric ( array $matches ) : string callback for decode() which converts a numerical entity to UTF-8
buffer ( ) : void buffer next parser output until unbuffer() is called
decode ( $text, $quote_style = ENT_QUOTES ) decode email addresses
fixBlockElementSpacing ( ) Trims whitespace in block-level elements, on the left side.
fixInlineElementSpacing ( ) Moves leading/trailing whitespace from inline elements outside of the element. This is to fix cases like Text, which if converted to ** strong** would be incorrect Markdown.
flushFootnotes ( ) : void output footnotes
flushLinebreaks ( ) : void flush enqued linebreaks
getLinkReference ( array $tag ) : string return formated link reference
getStacked ( string $tagName ) : array get last stacked element of type $tagName
handleHeader ( integer $level ) : void handle header tags (

-

)
handleTagToText ( ) : void handle non Markdownable tags
handleTag_a ( ) : void handle tags
handleTag_a_converter ( array $tag, string $buffer ) : string handle tags conversion
handleTag_a_parser ( ) : void handle tags parsing
handleTag_b ( )
handleTag_blockquote ( ) : void handle
tags
handleTag_br ( ) : void handle
tags
handleTag_code ( ) : void handle tags
handleTag_em ( ) : void handle and tags
handleTag_h1 ( ) : void handle

tags

handleTag_h2 ( ) : void handle

tags

handleTag_h3 ( ) : void handle

tags

handleTag_h4 ( ) : void handle

tags

handleTag_h5 ( ) : void handle
tags
handleTag_h6 ( ) : void handle
tags
handleTag_hr ( ) : void handle
tags
handleTag_i ( )
handleTag_img ( ) : void handle tags
handleTag_li ( ) : void handle
  • tags
  • handleTag_ol ( ) : void handle
      tags
    handleTag_p ( ) : void handle

    tags

    handleTag_pre ( ) : void handle
     tags    
    handleTag_strong ( ) : void handle and tags
    handleTag_ul ( ) : void handle
      tags
    handleText ( ) : void handle plain text
    hasParent ( string $tagName ) : boolean check if current node has a $tagName as parent (somewhere, not only the direct parent)
    indent ( string $str, boolean $output = true ) : void indent next output (start tag) or unindent (end tag)
    isMarkdownable ( ) : boolean check if current tag can be converted to Markdown
    out ( string $put, boolean $nowrap = false ) : void append string to the correct var, either directly to $this->output or to the current buffers
    parent ( ) : string get tagName of direct parent tag
    parse ( ) : void iterate through the nodes and decide what we shall do with the current node
    resetState ( ) Resetting the state forces the instance to behave as a fresh instance.
    setLineBreaks ( integer $number ) : void set number of line breaks before next start tag
    stack ( ) : void add current node to the stack this only stores the attributes
    strlen ( string $str ) : integer UTF-8 strlen()
    unbuffer ( ) : string end current buffer and return buffered output
    unichr ( $dec ) : string UTF-8 chr() which supports numeric entities
    unstack ( ) : array remove current tag from stack
    wordwrap ( string $str, $width, $break, $cut = false ) : string wordwrap for utf8 encoded strings

    Method Details

    __construct() public method

    constructor, set options, setup parser
    public __construct ( integer $linkPosition = self::LINK_AFTER_CONTENT, integer $bodyWidth = MDFY_BODYWIDTH, boolean $keepHTML = MDFY_KEEPHTML ) : void
    $linkPosition integer define the position of links
    $bodyWidth integer whether or not to wrap the output to the given width defaults to false
    $keepHTML boolean whether to keep non markdownable HTML or to discard it defaults to true (HTML will be kept)
    return void

    _decode_hex() protected method

    callback for decode() which converts a hexadecimal entity to UTF-8
    protected _decode_hex ( array $matches ) : string
    $matches array
    return string UTF-8 encoded

    _decode_numeric() protected method

    callback for decode() which converts a numerical entity to UTF-8
    protected _decode_numeric ( array $matches ) : string
    $matches array
    return string UTF-8 encoded

    buffer() protected method

    buffer next parser output until unbuffer() is called
    protected buffer ( ) : void
    return void

    decode() protected method

    decode email addresses
    protected decode ( $text, $quote_style = ENT_QUOTES )

    fixBlockElementSpacing() protected method

    Trims whitespace in block-level elements, on the left side.
    protected fixBlockElementSpacing ( )

    fixInlineElementSpacing() protected method

    Examples: * leading: Text becomes Text * trailing: Text becomes Text
    protected fixInlineElementSpacing ( )

    flushFootnotes() protected method

    output footnotes
    protected flushFootnotes ( ) : void
    return void

    flushLinebreaks() protected method

    flush enqued linebreaks
    protected flushLinebreaks ( ) : void
    return void

    getLinkReference() protected method

    return formated link reference
    protected getLinkReference ( array $tag ) : string
    $tag array
    return string link reference

    getStacked() protected method

    get last stacked element of type $tagName
    protected getStacked ( string $tagName ) : array
    $tagName string
    return array

    handleHeader() protected method

    handle header tags (

    -

    )
    protected handleHeader ( integer $level ) : void
    $level integer 1-6
    return void

    handleTagToText() protected method

    handle non Markdownable tags
    protected handleTagToText ( ) : void
    return void

    handleTag_blockquote() protected method

    handle
    tags
    protected handleTag_blockquote ( ) : void
    return void

    handleTag_br() protected method

    handle
    tags
    protected handleTag_br ( ) : void
    return void

    handleTag_code() protected method

    handle tags
    protected handleTag_code ( ) : void
    return void

    handleTag_em() protected method

    handle and tags
    protected handleTag_em ( ) : void
    return void

    handleTag_h1() protected method

    handle

    tags

    protected handleTag_h1 ( ) : void
    return void

    handleTag_h2() protected method

    handle

    tags

    protected handleTag_h2 ( ) : void
    return void

    handleTag_h3() protected method

    handle

    tags

    protected handleTag_h3 ( ) : void
    return void

    handleTag_h4() protected method

    handle

    tags

    protected handleTag_h4 ( ) : void
    return void

    handleTag_h5() protected method

    handle
    tags
    protected handleTag_h5 ( ) : void
    return void

    handleTag_h6() protected method

    handle
    tags
    protected handleTag_h6 ( ) : void
    return void

    handleTag_hr() protected method

    handle
    tags
    protected handleTag_hr ( ) : void
    return void

    handleTag_i() protected method

    protected handleTag_i ( )

    handleTag_img() protected method

    handle tags
    protected handleTag_img ( ) : void
    return void

    handleTag_li() protected method

    handle
  • tags
  • protected handleTag_li ( ) : void
    return void

    handleTag_ol() protected method

    handle
      tags
    protected handleTag_ol ( ) : void
    return void

    handleTag_p() protected method

    handle

    tags

    protected handleTag_p ( ) : void
    return void

    handleTag_pre() protected method

    handle
     tags        
    protected handleTag_pre ( ) : void
    return void

    handleTag_strong() protected method

    handle and tags
    protected handleTag_strong ( ) : void
    return void

    handleTag_ul() protected method

    handle
      tags
    protected handleTag_ul ( ) : void
    return void

    handleText() protected method

    handle plain text
    protected handleText ( ) : void
    return void

    hasParent() protected method

    check if current node has a $tagName as parent (somewhere, not only the direct parent)
    protected hasParent ( string $tagName ) : boolean
    $tagName string
    return boolean

    indent() protected method

    indent next output (start tag) or unindent (end tag)
    protected indent ( string $str, boolean $output = true ) : void
    $str string indentation
    $output boolean add indendation to output
    return void

    isMarkdownable() protected method

    check if current tag can be converted to Markdown
    protected isMarkdownable ( ) : boolean
    return boolean

    out() protected method

    append string to the correct var, either directly to $this->output or to the current buffers
    protected out ( string $put, boolean $nowrap = false ) : void
    $put string
    $nowrap boolean
    return void

    parent() protected method

    get tagName of direct parent tag
    protected parent ( ) : string
    return string $tagName

    parse() protected method

    iterate through the nodes and decide what we shall do with the current node
    protected parse ( ) : void
    return void

    parseString() public method

    parse a HTML string
    public parseString ( string $html ) : string
    $html string
    return string markdown formatted

    resetState() protected method

    Ideal for running within a loop where you want to maintain a single instance.
    protected resetState ( )

    setKeepHTML() public method

    set keep HTML tags which cannot be converted to markdown
    public setKeepHTML ( $keepHTML ) : void
    return void

    setLineBreaks() protected method

    set number of line breaks before next start tag
    protected setLineBreaks ( integer $number ) : void
    $number integer
    return void

    setLinkPosition() public method

    set the position where the link reference will be displayed
    public setLinkPosition ( integer $linkPosition ) : void
    $linkPosition integer
    return void

    stack() protected method

    add current node to the stack this only stores the attributes
    protected stack ( ) : void
    return void

    strlen() protected method

    UTF-8 strlen()
    protected strlen ( string $str ) : integer
    $str string
    return integer

    unbuffer() protected method

    end current buffer and return buffered output
    protected unbuffer ( ) : string
    return string

    unichr() protected method

    UTF-8 chr() which supports numeric entities
    protected unichr ( $dec ) : string
    return string UTF-8 encoded

    unstack() protected method

    remove current tag from stack
    protected unstack ( ) : array
    return array

    wordwrap() protected method

    wordwrap for utf8 encoded strings
    protected wordwrap ( string $str, $width, $break, $cut = false ) : string
    $str string
    return string

    Property Details

    $allowMixedChildren protected property

    html block tags that allow inline & block children
    protected array $allowMixedChildren
    return array

    $bodyWidth protected property

    wrap output, set to 0 to skip wrapping
    protected int $bodyWidth
    return integer

    $buffer protected property

    stores current buffers
    protected array $buffer
    return array

    $drop protected property

    html tags to be dropped (contents will not be parsed!)
    protected array $drop
    return array

    $escapeInText protected property

    list of chars which have to be escaped in normal text
    protected array $escapeInText
    return array

    $footnotes protected property

    stores current buffers
    protected array $footnotes
    return array

    $ignore protected property

    html tags to be ignored (contents will be parsed)
    protected array $ignore
    return array

    $indent protected property

    current indentation
    protected string $indent
    return string

    $isMarkdownable protected property

    tags with elements which can be handled by markdown
    protected array $isMarkdownable
    return array

    $keepHTML protected property

    keep html tags which cannot be converted to markdown
    protected bool $keepHTML
    return boolean

    $lastClosedTag protected property

    name of last closed tag
    protected string $lastClosedTag
    return string

    $lastWasBlockTag protected property

    wether last processed node was a block tag or not
    protected bool $lastWasBlockTag
    return boolean

    $lineBreaks protected property

    number of line breaks before next inline output
    protected $lineBreaks

    $linkPosition protected property

    position where the link reference will be displayed
    protected int $linkPosition
    return integer

    $minBodyWidth protected property

    minimum body width
    protected int $minBodyWidth
    return integer

    $notConverted protected property

    stack with tags which where not converted to html
    protected array $notConverted
    return array

    $output protected property

    markdown output
    protected string $output
    return string

    $parser protected property

    html parser object
    protected parseHTML $parser
    return parseHTML

    $skipConversion protected property

    skip conversion to markdown
    protected bool $skipConversion
    return boolean

    $stack protected property

    node stack, e.g. for and tags
    protected array $stack
    return array
    Markdown indents which could be wrapped
    protected array $wrappableIndents
    return array