On my site, I have a ton of legacy content, some from Joomla's JCEditor, some with semantic html markup, and some without any markup at all.

I need to preserve line breaks when editing, but currently CKEditor mashes all the content together, and instead of converting double line breaks as a

or

, they are all removed, and content runs together.

For the WYSIWYG module, there is a plugin/patch that fixes this problem: #513998: Plugin to convert p- and br-tags to newlines, but I haven't seen anything similar for FCK/CKEditor. Do you know if you could incorporate this, perhaps as an option in the "Cleanup and Output" section of the CKEditor settings?

It would be helpful on more than one site I run... and it would allow me to use this module (rather than WYSIWYG) on two huge new sites I'm working on.

Comments

mcrittenden’s picture

Sub. I need this feature as well.

mephir’s picture

Maybe it could help http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Output_Formatting . You should add on the end of ckeditor.config.js something like below, for an example:

CKEDITOR.on( 'instanceReady', function( ev ) { 
  this.dataProcessor.writer.setRules( 'p',
  {
    indent : false,
    breakBeforeOpen : true,
    breakAfterOpen : false,
    breakBeforeClose : false,
    breakAfterClose : true
  });
});
geerlingguy’s picture

Perhaps... I don't have time to test before the site launch, so I'm going to have to defer to someone else for now :(

bradezone’s picture

Indeed one could call this "a bummer" or "unfortunate" since you can set about ruining your content in major ways if you install CKeditor module and edit any existing content. Good thing we have revisions turned on, but geez, isn't this a fundamental disaster of a problem? Why does CK want to condense everything, and not preserve those line breaks?

mcrittenden’s picture

Title: Plugin to convert p- and br-tags to newlines (cross posted with WYSIWYG) » Plugin to convert new lines to <br /> or <p> (cross posted with WYSIWYG)

Changing title...seems like what we need is to convert new lines to line breaks, not the other way around. I kind of wonder if the easiest method is just a quick nl2br() in hook_nodeapi() for $op == prepare? Thoughts on that? A bit of a hack, but nothing better comes to mind at the moment.

P.S. Hi Brade! Your post popped up in my recent issues!

bradezone’s picture

Ahoy, crittenden!

BTW there is a very easy fix for this major issue. Simply set the "FormatSource" config option to false. This way you can keep "convert line breaks" on and not worry about your existing content getting messed up.

I did this in "config.js" which is part of the ckeditor download itself, so the file now looks like:

CKEDITOR.editorConfig = function( config )
{
	// Define changes to default configuration here. For example:
	// config.language = 'fr';
	// config.uiColor = '#AADC6E';
	config.FormatSource = false;
};

But there's probably a better place to put it for the purpose of fixing this module.

bradezone’s picture

Hmmm, this may only work part of the time. Found a few pages where it still crumples the text together...

mephir’s picture

@bradezone: yes, there are two better places, for an example ckeditor.config.js or in "Custom Javascript Configuration" on profile settings page in Advanced section.

One question. Why you use ckeditor and nl2br? It only help when you creating page without any editor.

mcrittenden’s picture

mephir: the problem is if you use CKEditor to edit pages that were created before CKEditor was installed. Drupal's default input formats convert new lines to br/p tags, so you don't have to manually enter <p> every time you want a new paragraph, you can just hit enter twice.

However, this conversion happens on node *view*, not on node *save*, so the p/br tags don't exist in the DB, they just appear when the content is being viewed. Therefore, they're not there when you edit the node. So if you install CK and then edit a node that was created before CK was installed, it doesn't convert those new lines to p tags on the edit form, and it squishes all the code together so that they don't get preserved (and therefore Drupal can't convert them to paragraphs like it did before CK was installed).

We're trying to figure out the best way to make CKEditor either convert them to p tags, or not squish the lines together (so Drupal will do the converting).

geerlingguy’s picture

We're trying to figure out the best way to make CKEditor either convert them to p tags, or not squish the lines together (so Drupal will do the converting).

I would prefer that CKEditor simply preserves the new lines, as Drupal core does... this way when an editor is turned off again, it won't add markup to otherwise un-marked-up content...

mcrittenden’s picture

Re: #10, that's what bradezone's solution attempts to accomplish in #6, but apparently it has problems (see #7).

TapSkill’s picture

I notice the lack of control over how the source code looks, is that what this issue topic is about?

wwalc’s picture

Status: Active » Needs review

Ok... let's forget for a while about controlling CKEditor output through dataProcessor.writer and focus on preserving line breaks / paragraphs.
There is no way to tell CKEditor to remember about white characters. CKEditor is a HTML editor and things like double line breaks in source code are ignored by browsers, so does CKEditor.

Anyway, I think we can handle that in CKEditor module. The problem is that having the following source:

aaa
bbb

ccc

ddd

after enabling CKEditor module and editing existing node, we want still to have in the final output something like:

<p>
aaa
<br/>
bbb
</p>
<p>ccc</p>
<p>ddd</p>

I think we can deal with that in a similar way like we call security filters. I'm attaching a patch that should handle such situation.
In "Cleanup and output" section another configuration setting is added: "Line break converter". If you enable it, CKEditor will call the line break converter when editing existing nodes. It means that you will no longer have in the source code:

aaa
bbb

ccc

ddd

but the final result should look more similar to what you expect.

Please test it and let me know.

(just a note that I've made some little cleanup in the code so the patch does not look that nice, however it should )

wwalc’s picture

StatusFileSize
new3.26 KB
wwalc’s picture

(please apply the patch to the CVS version, 6--1 branch)

wwalc’s picture

Assigned: Unassigned » wwalc

Hmm anyone? I'd like to put it into the next release (6.x-1.1), but needs to be sure first that this is exactly what you need.

geerlingguy’s picture

Sorry - I'll take a look at it tomorrow, as time allows.

jcisio’s picture

Status: Needs review » Needs work

I'd like to test, but after apply the patch, select Enable on the "Cleanup and output" section, enable the "Line break convertor" filter in my format (in fact, I didn't use this filter, the line break convertor is done by the HTML Purifier module). But nothing happens. When I view the html source (on the browser), I don't see br or p tags.

In the Security setup, I select the HTML Purifier filter, but it doesn't convert like the way it does when I view content.

I didn't look into the source code of ckeditor.module, but perhaps because each filter has an config attached to a format. Does the module miss it?

And why doesn't just list "Line break convertor" into the security filters list?

jcisio’s picture

Ooops I didn't read the patch before applying it. What it does it not applying the filter, but add the config.FormatSource=true in CKEditor. That doesn't solve my problem, neither.

wwalc’s picture

@jcisio - did you test the patch from comment #14? I'm asking because there is no "FormatSource" there.

It should work in exactly the same way as if the "Line break converter" was checked in the security filters list. It is not listed there because it is not a security filter (to avoid confusion).

Make sure you have enabled the "Line break converter'" in CKEditor profile after applying the patch.

jcisio’s picture

Yes, I did test the patch #14. So I was right in #18. But it didn't work (I enable the line break converter in the used format and enable that in CKEditor Cleanup & output).

About #19: sorry I was too quick, I saw "p;div;pre;address;h1;h2;h3;h4;h5;h6" just above and think that's something about the editor settings themselves.

I will check it again right now.

jcisio’s picture

I doesn't work. In my comment, the format was "Simple HTML" with the following filters:
- HTML Purifier
- Line break converter (I added this to test)
- Nofollow list filter

Then in CKEditor I enable the "Line break converter:" option.

I add a comment with CKEditor turned off (click on "Switch to plain text editor"), add a few text, submit. Then edit, with CKEditor enable, now all text is in a single paragraph. In the HTML source, the value in <textarea> tag is not format (that means no p or br tags).

That is strange too, as I enable Autoparagraph in HTML Purifier, and it displays as expected, but not when edit with CKEditor.

anniewang’s picture

Status: Needs work » Active

Hi,

I was wondering if any progress has been made on this. I'm having a huge problem, having switched from markitup editor to CKEditor and now all of my line and paragraph breaks are gone!

Can someone please help and advise some way to solve the problem? It seems like none of the existing strategies are working very well? Or maybe they are, I just don't really understand the technical language. Please help!

Thank you in advance.

mcrittenden’s picture

Status: Active » Needs work

This is CNW since there's already a patch.

mcrittenden’s picture

Patch in #14 only halfway worked for me. It worked great on about 50% of nodes, and the other 50% it didn't do anything to. I spent some time trying to figure out why it would affect some and not others, with no luck. Sorry I can't help more.

dczepierga’s picture

Status: Needs work » Closed (fixed)
mcrittenden’s picture

Status: Closed (fixed) » Needs work

How is this fixed? Not saying it's not, but some explanation would be nice.

rrizia’s picture

Hi,
I tried each of the above provided solution but could not get it fixed.

Anyone can please help.

Thanks in advance
Rizwana Rizia

jcisio’s picture

Category: bug » feature

I was rethinking about this issue. Conversion of old contents could be hard when mixing different formats. It could be avoided by a simple converter script which applies the auto paragraph filter on old contents and save the formatted content instead of original content (without <p> and <br> tags).

However, we can do other thing on the client side. For most users who don't write comments or who don't need CKEditor, the editor can be disabled by default. However we need a plugin to convert newlines to <p> or <br> when users enable the editor, or when users submit comment without enable the editor. In this case, no autoparagraph filter on the server side.

Briefly, we need a plugin that converts
- when user click on "Switch to rich text editor" at the first time
- or, when the form is submitted and the editor is never enabled

jcisio’s picture

StatusFileSize
new1.62 KB

This patch does the first thing in #29. I don't know how to do the second ("when the form is submitted and the editor is never enabled") properly.

This patch is for website with CKEditor disabled by default, but user can turn it on, even when there is some text in the textarea.

jcisio’s picture

geerlingguy’s picture

Also, please note that there's a new project that's an addon for the WYSIWYG project, that does just this (and works with CKEditor, as provided through WYSIWYG): WYSIWYG Force Linebreaks. I've been using it on a variety of sites, and it does exactly this.

andypost’s picture

patch #30 works by the right way

Suppose second issue should be renamed to "Convert when detach event released" also cares about possible conflicts with wysiwyg and others

jcisio’s picture

dczepierga’s picture

Status: Needs work » Closed (fixed)

I think we could closed this issue, as i see everything was said and fixed.
If i'm wrong pls correct me :)

Greetings