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