How to break a text at 80 chars precisely? - html

After having asked this question in the DB section and being adived to ask it here, here is my problem:
I have the problem, that I have a long text that is being read from a database. The text itself is just a one liner. The problem is that it needs to be broken at exactly 80 chars even when in the middle of a word.
HTML or other languages will make the line break if the next word doesn't fit in the remaining chars and that is not what I want. The pages are done in jsf.
For example:
textarea= cols: 8 rows: 3
input= break these texts
normal:
break
these
texts
what I need:
break th
ese text
s
Any ideas on how I can do this?

you can use the below function just pass the string (you want to brake after every 80 char) to the function
function breakText(str)
{
i=0;
outputStr="";
str= str.replace(/(\r\n|\n|\r)/g," ");
while(i<str.length)
{
outputStr += str.substr(i,80) +"<br>"; // replace br with any line break you want
i=i+80;
}
return outputStr
}
NOTE : this function will replace all the line break and insert a <br> after every 80 char
Working DEMO

Related

Exclude some characters from a Lex regex

I am trying to build a regex for lex that match the bold text in mardown syntax. For example: __strong text__ I thought this:
__[A-Za-z0-9_ ]+__
And then replace the text by
<strong>Matched text</strong>
But in Lex, this rule causes the variable yytext to be __Matched Text__. How could I get rid of the underscores? It would be better to create a regex that does not match the underscores or proccess the variable yytext to remove it?
With capturing groups it would be easer, because I would only need the regex:
__([A-z0-9 ]+)__
And use \1. But Lex does not support capturing groups.
Answer
I finally take the first option offer by João Neto, but a little modified:
yytext[strlen(yytext)-len]='\0'; // exclude last len characters
yytext+=len; // exclude first len characters
I've tried with Start conditions as he mentioned as second option, but did not work.
You can process yytext by removing the first and last two characters.
yytext[strlen(yytext)-2]='\0'; // exclude last two characters
yylval.str = &yytext[2]; // exclude first two characters
Another option is to use stack
%option stack
%x bold
%%
"__" { yy_push_state(bold); yylval.str = new std::string(); }
<bold>"__" { yy_pop_state(); return BOLD_TOKEN; }
<bold>.|\n { yylval.str += yytext; }

AS3 TextField: Append text at a certain line?

I'm trying to find a way to append text (appendText) at a certain TextField line number.
I found a way to return the first character of a line:
tf.text.charAt(tf.getLineOffset(10)); //selects line 10
But I haven't found a way to append text. Any help would be appreciated!
This should do the trick (put the supplied text at the start of the supplied line), though there may be a more efficient way of doing it.
function prependToLine(textField:TextField, line:int, text:String):void {
var lineOffset:int = textField.getLineOffset(line-1);
textField.text = textField.text.substring(0,lineOffset) + text + textField.text.substr(lineOffset);
}

split data(Text) of a text field into two strings, and use each string on different section of a visualforce page

I have an account with a Note in notesandattachment related list.
I want to display the text of 8 lines, out of 50 lines on a section of visualforce page.
And remaining 42 lines in another section of same visualforce page.
what i know :
I need to split the notes.body into two substrings. One, with 8 lines of text and second one with 42 line.I need to use div tags to get this done on the visualforce page. I might be wrong.
Please suggest me, how do i achieve this.
Appreciate the help.
You can split the text in controller by using split() method like this
String allLines[] = allText.split('\n');
String first8Lines = '';
String restLines ='';
for(Integer i =0; i<8 ; i++){
first8Lines += allLines[i];
}
for(Integer i =7; i<50 ; i++){
restLines += allLines[i];
}
And then put {!first8Lines} in one place and {!restLines} in another

Detect number of lines required by piece of text

This one is for all the TextField wizards out there. I have a text field that displays 2 paragraphs of texts. The first one I want to be truncated to show only 3 lines of text maximum. The 2nd paragraph is to be appended to that text.
Is there a way I can show only the first 3 lines of the first paragraph? Think of it like an abstract for an article for the first paragraph.
I tried doing it with String.subStr(0, guestimate of 3 lines of chars) but it's pretty inconsistent in that some strings will take up 4 lines of the textfield.
I thought about doing 2 separate textfields but in the case of the first paragraph having only 1 line worth of chars, there will be gap of 2 lines worth of chars between the 2nd paragraph.
textField = new TextField();
textField.antiAliasType = AntiAliasType.ADVANCED;
textField.width = _boundaryWidth;
textField.autoSize = TextFieldAutoSize.NONE;
textField.multiline = true;
textField.wordWrap = true;
textField.text = _text;
textField.setTextFormat(textFormat);
while( textField.numLines > 3 )
{
var txt:String = textField.getLineText(textField.numLines - 1 );
textField.replaceText(textField.text.length - txt.length,textField.text.length, "");
var numLines:int = textField.numLines;
}
This piece of code should do what you are looking for.
All that you need to do is check for the number of lines the text is covering. Set the width initially because that helps the textfield decide how many lines it needs to spread out into. I have not created a function which encompasses all this. But I guess that should be pretty simple to do since the whole function body is here.
Have a look at TextField#getLineOffset(). It will return the character index of the first character on the specified line - you can use substr or substring to truncate the text accordingly.

Controlling tab space in a <pre> using CSS?

Is it possible to specify how many pixels, etc. the tab space occupies in a <pre> using CSS?
for example, say i have a piece of code appearing in a <pre> on a web page:
function Image()
{
this.Write = function()
{
document.write(this.ToString());
return this;
};
...
}
Image.prototype = new Properties();
...
is it possible to specify a different amount of space the tab indents the line using CSS?
If not, is there any workarounds?
While the above discussion provides some historical background, times have changed, and more relevant information and possible solutions can be found here: Specifying Tab-Width?
attn admin: possible duplicate of ref'ed question.
From CSS 2.1, § 16.6.1 The 'white-space' processing model:
All tabs (U+0009) are rendered as a horizontal shift that lines up the start edge of the next glyph with the next tab stop. Tab stops occur at points that are multiples of 8 times the width of a space (U+0020) rendered in the block's font from the block's starting content edge.
CSS3 Text says basically the same thing.
From HTML 4.01 § 9.3.4 Preformatted text: The PRE element
The horizontal tab character (decimal 9 in [ISO10646] and [ISO88591] ) is usually interpreted by visual user agents as the smallest non-zero number of spaces necessary to line characters up along tab stops that are every 8 characters. We strongly discourage using horizontal tabs in preformatted text since it is common practice, when editing, to set the tab-spacing to other values, leading to misaligned documents.
If you're concerned with leading tabs, it's a simple matter to replace them with spaces.
/* repeat implemented using Russian Peasant multiplication */
String.prototype.repeat = function (n) {
if (n<1) return '';
var accum = '', c=this;
for (; n; n >>=1) {
if (1&n) accum += c;
c += c;
}
return accum;
}
String.prototype.untabify = function(tabWidth) {
tabWidth = tabWidth || 4;
return this.replace(/^\t+/gm, function(tabs) { return ' '.repeat(tabWidth * tabs.length)} );
}