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 * @version $Id: FigureBlockParser.html 905940 2014-04-12 16:27:29Z hboutemy $ 029 * @since 1.1 030 */ 031public class FigureBlockParser 032 implements BlockParser 033{ 034 /** {@inheritDoc} */ 035 public boolean accept( String line, ByLineSource source ) 036 { 037 return line.startsWith( "!" ) && line.lastIndexOf( "!" ) > 1; 038 } 039 040 /** {@inheritDoc} */ 041 public Block visit( String line, ByLineSource source ) 042 throws ParseException 043 { 044 String image = line.substring( 1, line.lastIndexOf( "!" ) ); 045 if ( image.indexOf( "|" ) >= 0 ) 046 { 047 // DOXIA-303: handle figure attributes 048 image = image.substring( 0, image.indexOf( "|" ) ); 049 } 050 051 line = line.substring( line.lastIndexOf( "!" ) + 1 ).trim(); 052 053 if ( line.startsWith( "\\\\" ) ) 054 { 055 // ignore linebreak at start of caption 056 line = line.substring( 2 ); 057 } 058 059 String caption = line + appendUntilEmptyLine( source ); 060 061 if ( caption.trim().length() > 0 ) 062 { 063 return new FigureBlock( image, caption ); 064 } 065 066 return new FigureBlock( image ); 067 } 068 069 /** 070 * Slurp lines from the source starting with the given line appending them together into a StringBuilder until an 071 * empty line is reached, and while the source contains more lines. 072 * 073 * @param source the source to read new lines from 074 * @return a StringBuilder appended with lines 075 * @throws ParseException 076 */ 077 private String appendUntilEmptyLine( ByLineSource source ) 078 throws ParseException 079 { 080 StringBuilder text = new StringBuilder(); 081 082 String line; 083 084 while ( ( line = source.getNextLine() ) != null ) 085 { 086 087 if ( line.trim().length() == 0 ) 088 { 089 break; 090 } 091 092 if ( text.length() == 0 ) 093 { 094 text.append( line.trim() ); 095 } 096 else 097 { 098 text.append( " " + line.trim() ); 099 } 100 101 } 102 103 return text.toString(); 104 } 105}