1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved. 22 * Portions Copyright (c) 2017, Chris Fraire <cfraire@me.com>. 23 */ 24 25 package org.opengrok.indexer.analysis.document; 26 27 import java.io.IOException; 28 import java.io.StringReader; 29 import java.io.StringWriter; 30 import java.io.Writer; 31 import org.opengrok.indexer.analysis.JFlexNonXref; 32 import org.opengrok.indexer.analysis.JFlexXref; 33 import org.opengrok.indexer.analysis.plain.PlainXref; 34 import org.opengrok.indexer.configuration.RuntimeEnvironment; 35 %% 36 %public 37 %class MandocXref 38 %extends JFlexNonXref 39 %unicode 40 %int 41 %char 42 %init{ 43 /* 44 * Keep this antiquated management of yyline for a JFlexNonXref subclass. 45 * Hopefully JFlexNonXref will be retired before too long. 46 */ 47 yyline = 1; 48 %init} 49 %include ../CommonLexer.lexh 50 // do not include CommonXref.lexh in JFlexNonXref subclasses 51 %{ 52 protected boolean didStartTee; 53 protected boolean didStartMandoc; 54 protected final MandocRunner mandoc = new MandocRunner(); 55 protected StringWriter plainbuf; 56 57 /** 58 * As with {@link TroffXref} we do not want the initial line number 59 * written by {@link JFlexNonXref}. 60 */ 61 @Override write(Writer out)62 public void write(Writer out) throws IOException { 63 this.out = out; 64 while(yylex() != YYEOF) { 65 } 66 } 67 startTee()68 protected void startTee() throws IOException { 69 plainbuf = new StringWriter(); 70 71 didStartMandoc = false; 72 if (RuntimeEnvironment.getInstance().getMandoc() != null) { 73 try { 74 mandoc.start(); 75 didStartMandoc = true; 76 } catch (IOException|MandocException e) { 77 // nothing we can do here 78 } 79 } 80 81 didStartTee = true; 82 } 83 writeTee()84 protected void writeTee() throws IOException { 85 if (!didStartTee) startTee(); 86 String s = yytext(); 87 plainbuf.write(s); 88 if (didStartMandoc) mandoc.write(s); 89 } 90 %} 91 92 %eof{ 93 boolean usePlain = true; 94 if (didStartMandoc) { 95 try { 96 String result = mandoc.finish(); 97 usePlain = false; 98 out.write("</pre><div id=\"mandoc\">"); 99 out.write(result); 100 out.write("</div><pre>"); 101 } catch (IOException|MandocException e) { 102 // nothing we can do here 103 } 104 } 105 try { 106 StringReader rdr = new StringReader(plainbuf.toString()); 107 plainbuf = new StringWriter(); 108 109 JFlexXref plainxref = new JFlexXref(new PlainXref(rdr)); 110 plainxref.setProject(this.project); 111 plainxref.setAnnotation(this.annotation); 112 plainxref.write(plainbuf); 113 114 loc = plainxref.getLOC(); 115 116 if (usePlain) { 117 String result = plainbuf.toString(); 118 out.write(result); 119 } catch(IOException e)120 } catch (IOException e) { 121 // nothing we can do here 122 } 123 124 didStartTee = false; 125 mandoc.destroy(); 126 plainbuf = null; 127 %eof} 128 129 %include ../Common.lexh 130 %% 131 <YYINITIAL> { 132 {EOL} { 133 writeTee(); 134 setLineNumber(++yyline); 135 } 136 137 {WhspChar}+ | 138 \w+ | 139 [^] { 140 writeTee(); 141 } 142 } 143