Revision 238 (by dpavlin, 2004/03/08 17:46:16) tagging openisis 0.9.0
<%@	page import="java.io.*, java.util.*, org.openisis.*"
	session="false"
%><%!
	// need at least Field.html
	static File _dir ; // have no reliable ServletConfig before init
	static long timestamp;
	static String[] files;
	static synchronized void refresh ( ServletContext a ) throws IOException {
		if ( null == _dir )
			_dir = new File( a.getRealPath("doc") );
		files = _dir.list( new FilenameFilter() {
			public boolean accept( File _dir, String name ) {
				return name.endsWith( ".txt" );
			}
		} );
		Arrays.sort( files );
		timestamp = _dir.lastModified();
	}

	// mini wiki-style doc
	static String mikidoc ( Writer o, BufferedReader in )
		throws IOException
	{
		String notes = null;
		String l;
		boolean inli = false;
		boolean br = false; // break on any line
		boolean code = false; // code mode
		while ( null != (l = in.readLine()) ) {
			int len = l.length();
			boolean isli = false;
			char c = 0 == len ? 0 : l.charAt(0);
			int tab = 1 < len && '\t' == l.charAt(1) ? 2 : 1;
			if ( code && '$' != c )
				l = Field.html( l );
			else switch( c ) {
			case 0:
				l = "<br>";
				break;
			case '$': // toggle code
				l = (code = !code) ? "<pre>" : "</pre>";
				break;
			case '*': // head
				String bg = "#CCCCCC";
				String anchor = "";
				if ( 1==tab && 2 < len && ' ' != l.charAt(1) ) { /* make anchor */
					int i = l.indexOf( '\t' );
					if ( 1 < i ) {
						tab = i+1;
						anchor = "<a name=\""+l.substring(1,i)+"\">&nbsp;</a>";
					}
				}
				l = "</td></tr><tr><td bgcolor=\""+bg+"\"><b>"
					+anchor+l.substring(tab)
					+"</b></td></tr><tr><td align=\"justify\">";
				break;
			case '-':
				if ( l.startsWith("---") ) { // rule
					l = "<hr>";
					break; // rule :)
				}
				// new list item
				l = (inli ? "</li>" : "<ul>") + "<li>" + l.substring(tab);
				if ( 40 > len ) // break after short heading
					l += "<br>";
				isli = true;
				break;
			case '\t': // indented
				isli = inli; // continue list item, if any
				if ( 1 == len )
					l = "\t<br>";
				break;
			case '>': // link
				isli = inli; // dont't break lists on links
				int t = l.indexOf('\t',tab);
				String ref, name;
				if ( 0 > t )
					ref = name = l.substring(tab).trim();
				else {
					ref = l.substring(tab,t).trim();
					name = l.substring(t+1).trim();
				}
				l = "<a href=\""+ref+"\">"+name+"</a>";
				break;
			case '.':
				if ( l.startsWith("...") ) { // continued
					notes = l.substring(3).trim();
					continue;
				}
				break;
			case '!': // pre
				br = true;
			case ';': // comment
				continue;
			}
			if ( inli && !isli )
				o.write( "</ul>" );
			o.write( l );
			if ( br && ! l.endsWith( "<br>" ) )
				o.write( "<br>\n" );
			else
				o.write( '\n' );
			inli = isli;
		}
		return notes;
	}

	static String mikidoc ( Writer o, String file )
		throws IOException
	{
		file = _dir+File.separator+file;
		try {
			return mikidoc( o, new BufferedReader( new InputStreamReader(
				new FileInputStream( file ) ) ) );
		} catch (IOException e) {
			o.write( "<!-- schnurps -->" );
		}
		return null;
	}

%><%
	if ( null == _dir || timestamp != _dir.lastModified() )
		refresh( application );

	String right = null;
	String file  = request.getParameter("file")+".txt";
	boolean sides = null == request.getParameter("print");
	String name = "";
	int f = files.length;
	while ( 0 < f-- && !files[f].equals(file) )
		;
	file = 0 > f ? "OverView.txt" : files[f];
	name = file.substring( 0, file.length()-4 );
%><html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  <title>OpenIsis - <%=name%></title>
	<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
</head>
<body bgcolor="#FFFFFF"
	marginwidth="0" marginheight="0" topmargin="0" leftmargin="0">
<% if ( sides ) { %>
<table width="100%" cellpadding="0" cellspacing="0"
	marginwidth="0" marginheight="0" topmargin="0" leftmargin="0" border="0">
<tr>
	<%-- left: the index --%>
	<td valign="top" width="160">
  <table border="0" cellspacing="5" width="100%" cols="1">
	<tr>
		<td valign="top">
<% mikidoc( out, ".index" ); %>
		<hr>
		<a href="<%=name%>?print=">print</a><br>
		<a href="/doc/<%=file%>">plaintext</a>
		</td>
	</tr>
	</table>
	</td>
	<%-- middle: the content --%>
	<td valign="top">
<% } // sides %>
  <table border="0" cellspacing="5" width="100%" cols="1">
	<tr>
		<td valign="top" bgcolor="#000000"><font color="#FFFFFF"><b>
	<%= name %>
		</b></font></td>
	</tr>
	<tr>
		<td align="justify">
<% right = mikidoc( out, file ); %>
		</td>
	</tr>
	</table>
<% if ( sides ) { %>
<%
	if ( null != right ) {
%>
	<%-- right: notes --%>
	</td>
	<td valign="top" width="25%">
  <table border="0" cellspacing="5" width="100%" cols="1">
	<tr>
		<td valign="top" bgcolor="#000000"><font color="#FFFFFF"><b>
	<%= right %>
		</b></font></td>
	</tr>
	<tr>
		<td align="justify">
<% mikidoc( out, right+".txt" ); %>
		</td>
	</tr>
	</table>
<%
	}
%>
	</td>
</tr>
</table>
<% } // sides %>
<!--
$Revision: 1.6 $ last changed $Date: 2003/01/06 19:54:29 $ by $Author: kripke $
-->
</body>
</html>