001package org.apache.maven.doxia.module.confluence.parser;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.doxia.parser.ParseException;
023import org.apache.maven.doxia.util.ByLineSource;
024
025/**
026 * <p>FigureBlockParser class.</p>
027 *
028 * @since 1.1
029 */
030public class FigureBlockParser
031    implements BlockParser
032{
033    /** {@inheritDoc} */
034    public boolean accept( String line, ByLineSource source )
035    {
036        return line.startsWith( "!" ) && line.lastIndexOf( "!" ) > 1;
037    }
038
039    /** {@inheritDoc} */
040    public Block visit( String line, ByLineSource source )
041        throws ParseException
042    {
043        String image = line.substring( 1, line.lastIndexOf( "!" ) );
044        if ( image.contains( "|" ) )
045        {
046            // DOXIA-303: handle figure attributes
047            image = image.substring( 0, image.indexOf( "|" ) );
048        }
049
050        line = line.substring( line.lastIndexOf( "!" ) + 1 ).trim();
051
052        if ( line.startsWith( "\\\\" ) )
053        {
054            // ignore linebreak at start of caption
055            line = line.substring( 2 );
056        }
057
058        String caption = line + appendUntilEmptyLine( source );
059
060        if ( caption.trim().length() > 0 )
061        {
062            return new FigureBlock( image, caption );
063        }
064
065        return new FigureBlock( image );
066    }
067
068    /**
069     * Slurp lines from the source starting with the given line appending them together into a StringBuilder until an
070     * empty line is reached, and while the source contains more lines.
071     *
072     * @param source the source to read new lines from
073     * @return a StringBuilder appended with lines
074     * @throws ParseException
075     */
076    private String appendUntilEmptyLine( ByLineSource source )
077        throws ParseException
078    {
079        StringBuilder text = new StringBuilder();
080
081        String line;
082
083        while ( ( line = source.getNextLine() ) != null )
084        {
085
086            if ( line.trim().length() == 0 )
087            {
088                break;
089            }
090
091            if ( text.length() == 0 )
092            {
093                text.append( line.trim() );
094            }
095            else
096            {
097                text.append( " " ).append( line.trim() );
098            }
099
100        }
101
102        return text.toString();
103    }
104}