package parser

import (
	

	
)

// sFigureLine checks if there's a figure line (e.g., !--- ) at the beginning of data,
// and returns the end index if so, or 0 otherwise.
func sFigureLine( []byte,  string) ( int,  string) {
	,  := 0, 0

	 := len()
	// skip up to three spaces
	for  <  &&  < 3 && [] == ' ' {
		++
	}

	// check for the marker characters: !
	if +1 >=  {
		return 0, ""
	}
	if [] != '!' || [+1] != '-' {
		return 0, ""
	}
	++

	 := [] // i.e. the -

	// the whole line must be the same char or whitespace
	for  <  && [] ==  {
		++
		++
	}

	// the marker char must occur at least 3 times
	if  < 3 {
		return 0, ""
	}
	 = string([- : ])

	// if this is the end marker, it must match the beginning marker
	if  != "" &&  !=  {
		return 0, ""
	}

	// there is no syntax modifier although it might be an idea to re-use this space for something?

	 = skipChar(, , ' ')
	if  >=  || [] != '\n' {
		if  ==  {
			return , 
		}
		return 0, ""
	}
	return  + 1,  // Take newline into account.
}

// figureBlock returns the end index if data contains a figure block at the beginning,
// or 0 otherwise. It writes to out if doRender is true, otherwise it has no side effects.
// If doRender is true, a final newline is mandatory to recognize the figure block.
func ( *Parser) ( []byte,  bool) int {
	,  := sFigureLine(, "")
	if  == 0 ||  >= len() {
		return 0
	}

	var  bytes.Buffer

	for {
		// safe to assume beg < len(data)

		// check for the end of the code block
		,  := sFigureLine([:], )
		if  != 0 {
			 += 
			break
		}

		// copy the current line
		 := skipUntilChar(, , '\n') + 1

		// did we reach the end of the buffer without a closing marker?
		if  >= len() {
			return 0
		}

		// verbatim copy to the working buffer
		if  {
			.Write([:])
		}
		 = 
	}

	if ! {
		return 
	}

	 := &ast.CaptionFigure{}
	.AddBlock()
	.Block(.Bytes())

	defer .Finalize()

	if , ,  := .caption([:], []byte("Figure: "));  > 0 {
		 := &ast.Caption{}
		.Inline(, )

		.HeadingID = 

		.addChild()

		 += 
	}
	return 
}