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>DefinitionListBlockParser class.</p>
027 *
028 * @author Dave Syer
029 * @since 1.1
030 */
031public class DefinitionListBlockParser
032    implements BlockParser
033{
034    static final String LS = System.getProperty( "line.separator" );
035
036    /** {@inheritDoc} */
037    public boolean accept( String line, ByLineSource source )
038    {
039        return ( line.startsWith( "{note" ) || line.startsWith( "{tip" )
040            || line.startsWith( "{info" ) || line.startsWith( "{quote" ) );
041    }
042
043    /** {@inheritDoc} */
044    public Block visit( String line, ByLineSource source )
045        throws ParseException
046    {
047        StringBuilder text = new StringBuilder();
048        StringBuilder title = new StringBuilder();
049
050        int index = line.indexOf( "title=" );
051
052        if ( index >= 0 )
053        {
054            line = line.substring( index + 6 );
055
056            while ( !( line.contains( "}" ) ) && line != null )
057            {
058                append( title, line );
059                line = source.getNextLine();
060            }
061
062            if ( line != null )
063            {
064                append( title, line.substring( 0, line.indexOf( "}" ) ) );
065            }
066        }
067
068        while ( ( line = source.getNextLine() ) != null )
069        {
070            if ( line.startsWith( "{note" ) || line.startsWith( "{tip" )
071                || line.startsWith( "{info" ) || line.startsWith( "{quote" ) )
072            {
073                break;
074            }
075
076            append( text, line );
077        }
078
079        return new DefinitionListBlock( title.toString(), text.toString() );
080    }
081
082    private void append( StringBuilder title, String line )
083    {
084        if ( title.length() > 0 )
085        {
086            title.append( " " );
087        }
088
089        title.append( line.trim() );
090    }
091}