Programmatically removing newlines inside Google Documents - javascript

I wrote a trivial google apps script that runs thru a google document to divide pieces of text. Now, the text that my code need to divide seems to include newlines.
My problem is that I just don't understand how to remove these programmatically (I tried: mydoc.replaceText("/\n/",""); and mydoc.replaceText(/(\r\n|\n|\r)/gm,""); plus other similar variations with no success.

I can't comment but maybe something like this works for you? replaces whitespace with single spaces
mydoc.replaceText("/\s/"," ");

Related

How to paste plain text in TinyMCE without extra newlines?

I am having a problem in TinyMCE when I use paste_as_text: true in conjunction with forced_root_block: false. Pasting already plain-text in works fine, but pasting from Word adds extra <br> tags between every newline. It's not like I can simply parse these out, because that breaks correct double-newlines from plain text.
I have noticed that pasting with ctrl-shift-v fixes this issue, and would love to make that the default pasting method, but can't find how.
I'm currently trying to write a parser to use in paste_preprocess, but since it's possible to do in other ways, I figure there must be a better solution.
Pasting from Microsoft Word is broken in must copy and paste/Cliboard APIs. you will need to modify Newline.js or Clipboard.js manually.
For example, replace line 63 in Newline.js:
return p.split(/\n/).join('<br />');
with:
return p.replace(/\r?\n/g, '<br>');
If you can open an issue on the plugin page, I will create a proper pull request.

Ignoring whitespace in code and pre tags

I am having some issues regarding whitespace in my HTML code. This is what is currently looks like:
<pre>
<code>
-(void)respring {
system("killall -9 SpringBoard");
}
</code>
</pre>
The indentation marks are to format my HTML a little better, make it more readable. When I save it and upload it to my server, I get
this. I have already tried using the white-space CSS property to try to fix the issue, but all it does it mess up my codes formatting. I also looked at the JavaScript solutions to this problem via Google but they don't seem to work. I am using highlight js for syntax highlighting if that helps.
Thanks.
The whole point of a pre tag is to preserve the spacing that's displayed, including anything at the beginning of a line. This should fix it.
<code>-(void)respring {
system("killall -9 SpringBoard");
}</code>
It may not look pretty in code, but on the page it'll look just fine.

Grunt - Replace blank lines in a file - regex

I'm building up my first Grunt project and one of my tasks is introducing blank lines into the beginning of my output html files. I'd like to remove them.
I found grunt-line-remover but it requires me to list each html file separately, and I'd rather point it to a directory. (and I don't have time right now to figure out how to tweak the code to do that.)
I then found grunt-text-replace which supports file name pattern matching, but I can't figure out the Regex for finding a blank line in an html file.
I tried
/\n/g
but that replaces every new line whereas I'd like to just replace the empty lines. Really, I just want to replace the beginning blank lines, but if it removes every blank line that's fine.
Any help would be appreciated
If you want to remove empty space Try using a regex of [\r\n]{2,}
Instead of looking for newlines, you can look for empty lines with the regex
/^\s*$/gm
It should give you empty lines as well as lines containing spaces only.

Insert multiple line breaks into a JavaScript string (regex) (CodeMirror)

I have a few strings and I would like to insert some line breaks into them at certain points.
I figured out a few of the logistics but as a whole I can't seem to crack this problem, probably because I have limited experience with regex.
Basically I have a long string of XML tags that is all on one line. I want to add line breaks at certain points to get the data more formatted and looking nice. I am using CodeMirror to display this data on a webpage but for some reason its all on line #1.
So I need to go from something like this:
<Sample><Name></Name><PhoneNumber><AreaCode></AreaCode><Number></Number></PhoneNumber></Sample>
To something like this:
<Sample>
<Name></Name>
<PhoneNumber>
<AreaCode></AreaCode>
<Number></Number>
</PhoneNumber>
</Sample>
CodeMirror will take care of the rest of the formatting all I need to do is insert the line breaks in the right spot using regex or a loop of some sort. The Tags will or can change so I am guessing regex has to be used.
I have had success inserting line breaks with \n and &#xD but can't seem to get regex to detect the proper locations.
Any help would be greatly appreciated. Thanks.
UPDATE
I overlooked this but the brackets are in fact being sent as < and >
So example tag would look like:
<PhoneNumber>
or
</PhoneNumber>
So basically need to insert a \n after every > that is a closing tag or a beginning tag that contains children tags.
There be dragons here.
I'd like to point you to a very similar question answered awhile ago that does a good job of explaining why you should NOT try to parse XML yourself unless you REALLY know what you're doing.
Use an XML deserializer if you want to get nice line breaks and that sort of thing.
Try this regex pattern:
>\s*<(?!/)
Replacement string : >\n<
UPDATE:
>\s*<(?!/)

Django CMS / WYMEditor Stop Stripping of Whitespace

I'm aware of what WYMEditor is all about and that using Paragraphs for spacing is not intended, however the problem here is with the client requiring that we give them this functionality.
I've looked high and low to find where WYMEditor does it's stripping of whitespace and can't seem to find it at all.
It seems that when you press enter it creates a P visually, however when clicking the source it doesn't contain it. Furthermore, manually editing HTML source to contain <p> </p> doesn't work, as WYMEditor strips it out.
Just wondering if anybody has had this issue before and knows how to get rid of this functionality? It's worth noting that I believe the replacement is happening both in the 'text' module of Django-CMS, and also in the Javascript for WYMEditor.
Turns out, the function that does this stripping is very simply named, for some reason I missed it in (multiple!) searches for the word 'empty' in the script file.
It's located in jquery.wymeditor.js, line ~3440 there is the function WYMeditor.XhtmlSaxListener.prototype.removeEmptyTags, simply stop the replacement:
WYMeditor.XhtmlSaxListener.prototype.removeEmptyTags = function(xhtml)
{
return xhtml;// .replace(new RegExp('<('+this.block_tags.join("|").replace(/\|td/,'').replace(/\|th/, '')+')>(<br \/>| | |\\s)*<\/\\1>' ,'g'),'');
};
That obviously stops the stripping of whitespace!

Categories

Resources