private String prepareFile(JoeTree tree, DocumentInfo docInfo, String lineEnding, String template) {
		
		// Figure out max_style_depth
		int max_style_depth = 0;
		String match = "{!max_style_depth:";
		int start = template.indexOf(match);
		if (start != -1) {
			start += match.length();
			int end = template.indexOf("}", start);
			if (end != -1) {
				try {
					max_style_depth = Integer.parseInt(template.substring(start, end));
					template = StringTools.replace(template, "{!max_style_depth:" + max_style_depth + "}", "");
				} catch (Exception e) {
					System.out.println("ERROR: Could not parse max_style_depth: " + e.getMessage());
				}
			}
		}
		
		// Do replacements on the template.
		template = StringTools.replace(template, "{$encoding}",      XMLTools.escapeHTML(PropertyContainerUtil.getPropertyAsString(docInfo, DocumentInfo.KEY_ENCODING_TYPE)));
		template = StringTools.replace(template, "{$title}",         XMLTools.escapeHTML(PropertyContainerUtil.getPropertyAsString(docInfo, DocumentInfo.KEY_PATH)));
		template = StringTools.replace(template, "{$date_created}",  XMLTools.escapeHTML(PropertyContainerUtil.getPropertyAsString(docInfo, DocumentInfo.KEY_DATE_CREATED)));
		template = StringTools.replace(template, "{$date_modified}", XMLTools.escapeHTML(PropertyContainerUtil.getPropertyAsString(docInfo, DocumentInfo.KEY_DATE_MODIFIED)));
		template = StringTools.replace(template, "{$owner_name}",    XMLTools.escapeHTML(PropertyContainerUtil.getPropertyAsString(docInfo, DocumentInfo.KEY_OWNER_NAME)));
		template = StringTools.replace(template, "{$owner_email}",   XMLTools.escapeHTML(PropertyContainerUtil.getPropertyAsString(docInfo, DocumentInfo.KEY_OWNER_EMAIL)));
		
		// Do Font and Color Replacements
		String fontFace = Preferences.getPreferenceString(Preferences.FONT_FACE).cur;
		template = StringTools.replace(template, "{$font_face}", fontFace);
		
		String fontSize = "" + Preferences.getPreferenceInt(Preferences.FONT_SIZE).cur;
		template = StringTools.replace(template, "{$font_size}", fontSize);
		
		String desktopBackgroundColor = convertColorToWebColorValue(Preferences.getPreferenceColor(Preferences.DESKTOP_BACKGROUND_COLOR).cur);
		template = StringTools.replace(template, "{$desktop_background_color}", desktopBackgroundColor);
		
		String panelBackgroundColor = convertColorToWebColorValue(Preferences.getPreferenceColor(Preferences.PANEL_BACKGROUND_COLOR).cur);
		template = StringTools.replace(template, "{$panel_background_color}", panelBackgroundColor);
		
		String textareaBackgroundColor = convertColorToWebColorValue(Preferences.getPreferenceColor(Preferences.TEXTAREA_BACKGROUND_COLOR).cur);
		template = StringTools.replace(template, "{$textarea_background_color}", textareaBackgroundColor);
		
		String textareaForegroundColor = convertColorToWebColorValue(Preferences.getPreferenceColor(Preferences.TEXTAREA_FOREGROUND_COLOR).cur);
		template = StringTools.replace(template, "{$textarea_foreground_color}", textareaForegroundColor);
		
		String textareaCommentColor = convertColorToWebColorValue(Preferences.getPreferenceColor(Preferences.TEXTAREA_COMMENT_COLOR).cur);
		template = StringTools.replace(template, "{$textarea_comment_color}", textareaCommentColor);
		
		String selectedChildColor = convertColorToWebColorValue(Preferences.getPreferenceColor(Preferences.SELECTED_CHILD_COLOR).cur);
		template = StringTools.replace(template, "{$selected_child_color}", selectedChildColor);
		
		String lineNumberColor = convertColorToWebColorValue(Preferences.getPreferenceColor(Preferences.LINE_NUMBER_COLOR).cur);
		template = StringTools.replace(template, "{$line_number_color}", lineNumberColor);
		
		String lineNumberSelectedColor = convertColorToWebColorValue(Preferences.getPreferenceColor(Preferences.LINE_NUMBER_SELECTED_COLOR).cur);
		template = StringTools.replace(template, "{$line_number_selected_color}", lineNumberSelectedColor);
		
		String lineNumberSelectedChildChildColor = convertColorToWebColorValue(Preferences.getPreferenceColor(Preferences.LINE_NUMBER_SELECTED_CHILD_COLOR).cur);
		template = StringTools.replace(template, "{$line_number_selected_child_color}", lineNumberSelectedChildChildColor);
		
		
		// Outline
		StringBuffer buf = new StringBuffer();
		
		Node node = tree.getRootNode();
		for (int i = 0; i < node.numOfChildren(); i++) {
			buildOutlineElement(node.getChild(i), lineEnding, buf, max_style_depth);
		}
		
		template = StringTools.replace(template, "{$outline}", buf.toString());
		
		return template;
	}