[opencms-dev] Create a txt file with fixed header/footer

Paul-Inge Flakstad flakstad at npolar.no
Fri Nov 14 14:26:14 CET 2014


Hi Corrado,

What Kai is suggesting is the better approach, but you *can* use "Page with free text" if that's easier. Just strip all HTML (using CmsHtmlExtractor) from the content before you output it. 

The bad side of this approach is that the editors will of course still see the formatting tools when editing the content. Perhaps they'll wonder why any formatting they've applied is "lost" when viewing the website..? (Fair warning given ;)

An example on how to strip HTML markup (extremely verbose):

<%@ page import="org.opencms.jsp.*, org.opencms.main.*, org.opencms.file.*, org.opencms.util.*, java.util.*"
%><%
CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
CmsObject cmso = cms.getCmsObject();
Locale locale = cms.getRequestContext().getLocale();

String resourceUri = "/path/to/page-with-free-text.html";
CmsResource resource = cmso.readResource(resourceUri);
String resourceEnc = OpenCms.getLocaleManager().getResourceEncoding(cmso, resource);
String pageElement = "body";

String content = cms.getContent(resourceUri, pageElement, locale);
String contentAsPlainText = CmsHtmlExtractor.extractText(content, resourceEnc);

out.println(CmsStringUtil.escapeHtml(contentAsPlainText)); // escapeHtml is just for good measure.
%>

Alternatively, if you want to strip *only* the p tags, you can hack it like this:
	
<%@ page import="org.opencms.jsp.*, org.opencms.main.*, org.opencms.file.*, java.util.*"
%>
<%!
    /**
     * Strips a string from paragraph tags, replaces paragraph tags inside
     * the string with: break tag + blank space + break tag.
     *
     * @param str  The string to remove paragraph tags from.
     * @return  The stripped string.
     */
    public static String stripParagraph(String str) {
        if (str == null)
                return str;
        str = str.trim();
        if (str.length() < 7) // The length of the String "<p></p>" is 7
                return str;
        // if the string ends with a </p>, strip this away clean so
        // there won't be line breaks at the end
        if (str.substring(str.length()-4, str.length()).equalsIgnoreCase("</p>")) {
                str = str.substring(0, str.length()-4);
        }
        // strip all <p> and <p class=... rel=... style=...> type tags away
        String regex = "<p\\s.+?>|<p>";
        str = str.replaceAll(regex, "");
        // and replace remaining </p> tags with extra line breaks
        str = str.replaceAll("</p>", "<br /> <br />");
        return str;
    }
%><%
CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
CmsObject cmso = cms.getCmsObject();
Locale locale = cms.getRequestContext().getLocale();

String resourceUri = "/path/to/page-with-free-text.html";
CmsResource resource = cmso.readResource(resourceUri);
String resourceEnc = OpenCms.getLocaleManager().getResourceEncoding(cmso, resource);
String pageElement = "body";

String content = cms.getContent(resourceUri, pageElement, locale);
out.println(stripParagraph(content));
%>

HTH :)

Cheers,
Paul

-----Original Message-----
From: opencms-dev-bounces at opencms.org [mailto:opencms-dev-bounces at opencms.org] On Behalf Of Schliemann, Kai
Sent: 14. november 2014 13:35
To: The OpenCms mailing list
Subject: Re: [opencms-dev] Create a txt file with fixed header/footer

Hi Corrado,
you could create a XML Content type with just one element. This element uses the TextareaWidget type (more info: https://opencms.cse.unsw.edu.au/alkacon-documentation/documentation_xmlcontent/step3-advancedxsd.html).
HTH
Best regards
Kai

-----Ursprüngliche Nachricht-----
Von: opencms-dev-bounces at opencms.org [mailto:opencms-dev-bounces at opencms.org] Im Auftrag von Paoletti Corrado
Gesendet: Freitag, 14. November 2014 12:06
An: opencms-dev at opencms.org
Betreff: [opencms-dev] Create a txt file with fixed header/footer

Hi all,
I would like to create a template with which to allow editors to create a simple text file (.txt). Each file will have a header and footer and variabile body.

I have created the following simple code (JSP):

<%@ page session="false" %>
<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>

***HEADER***
<cms:include element="body" />
***FOOTER***

When I create a "Page with free text" the editor inserts <p></p> tags around text I'm writing but... I do not want html tags! The best would be have an editor very simple as if I choose to create a "text file".

How can I do it?

I use OpenCms 8.0.1.

Thanks

Corrado
_______________________________________________
This mail is sent to you from the opencms-dev mailing list To change your list options, or to unsubscribe from the list, please visit http://lists.opencms.org/cgi-bin/mailman/listinfo/opencms-dev



_______________________________________________
This mail is sent to you from the opencms-dev mailing list To change your list options, or to unsubscribe from the list, please visit http://lists.opencms.org/cgi-bin/mailman/listinfo/opencms-dev






More information about the opencms-dev mailing list