Modifying SQLFORM text output - line breaks, paragraphs (web2py framework) - line

Model:
Field('text1', type='text', length=1000, notnull=True)
Function:
def textinput():
f=SQLFORM(db.example, fields=['text1'])
if f.accepts(request.vars, session):
return dict(form=f)
I want to be able to display the 'text1' field with proper line spacing/formatting. If a user was to press the [enter] key in the form to start a new line or whole new paragraph, I want this to reflect on the view.
For example, if a user enters the following into a SQLFORM:
This is a paragraph. Blah blah blah blah blah blah blah.
Blah blah blah blah blah blah blah blah blah.
Blah blah blah blah.
This is another paragraph. Blah blah blah blah blah.
Blah blah blah blah blah.
I want it to come out exactly like that in the view, instead of having it all crammed up with no spacing. How would I go about doing this? I was thinking about using the .replace method and replacing all [enter] keystrokes with a line break, but I'm not sure how to do it. I've searched all over Google but I can't find my exact issue.
Any help would be greatly appreciated. Thanks.

The easiest method is to wrap the text in a <pre> tag and use CSS to control the styling. You can also replace the line breaks ('\n') with <br /> tags. If you are displaying the text via a readonly SQLFORM, a Crud read form, a SQLTABLE, or SQLFORM.grid, then you can set the field's "represent" attribute to control the display:
Using <pre>:
Field('text1', type='text', length=1000, notnull=True,
represent=lambda text, row: PRE(text))
Using line break replacement:
Field('text1', type='text', length=1000, notnull=True,
represent=lambda text, row: XML(text.replace('\n', '<br />'),
sanitize=True, permitted_tags=['br/']))
If you're inserting the text manually into a view, you can simply use either of the above methods directly in the view. For example:
{{=PRE(row.text1)}}
Note, browsers typically display text in a <pre> tag with a fixed-width font. If you don't want that, you'll need to use CSS to change the font.

Related

template formatting - allow readable format

I'm creating some apache velocity templates and formatting the code(intellij Idea) breaks the template, because the text parts of the template are automatically indented.
When there's only a single line of the output text, then the indent is not preserved in the output:
#macro(body)
#if(${someCondition})
Hello
#end
#end
The output is:
Hello
The problem with multiline text output is this:
#macro(body)
#if(${someCondition})
Hello
World
#end
#end
And the output:
Hello
World
Is there some way to strip the indent of the text part based on the sorrounding code? I'd like to avoid ugly formatted templates like this:
#macro(body)
#if(${someCondition})
Hello
World
#end
#end
The code which uses the template:
final VelocityEngine engine = new VelocityEngine();
engine.init();
final VelocityContext context = new VelocityContext();
//put variables into context
engine.evaluate(context, outputWriter, "LOG", inputString);
Maybe there's something I can put into VelolocityContext, but I wasn't able to find it.

Code styling without modifying the binaries

On the WebStorm for example and I believe in any Intellij product.
You can easily refeactor the code and style it as you like from the setting 'Code Style'.
But, the styling and refactoring actually change the binaries of the file.
for example if you decide you want new line after { it will add \n in each place.
I want to know if it possible to only visually display those different to the coder.
If I'm coding like this:
var func = function()
{
// Blah
}
And another programmer on the team code like this:
var func = function() {
// Blah
}
and also if I code like this:
var text = "";
and the other like this:
var text = '';
The thing is that I don't really care how it would actually be like in the saved file. I only care how it would be displayed to the programmer.
It is possible to achieve this ?
Simple answer: No. That's because coding rules exist and besides things like changed binaries you have the problem of version control as well. which style of your code should be versioned? While VCS' are able to deal with different line endings, what you ask for isn't supported anywhere.

adding a symbolic trait to a selected range

what i need to do is adding a symbolic trait to a selected range so if it is italic and i pressed bold button then it should be italic and bold
- (IBAction)boldedSelectedText:(UIButton*)sender {
UIFontDescriptor* bodyFontDescriptor=[UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
UIFontDescriptor* boldFontdescriptor=[bodyFontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
self.body.font=[UIFont fontWithDescriptor:boldFontdescriptor size:0.0];
}
- (IBAction)italicSelectedText:(UIButton *)sender {
UIFontDescriptor* bodyFontDescriptor=[UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
UIFontDescriptor* italicFontdescriptor=[bodyFontDescriptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic];
self.body.font=[UIFont fontWithDescriptor:italicFontdescriptor size:0.0];
}
the problem is when i select a text and make it bold then italic , it become italic only and vice it can not be bold and italic at the same time i don't know what should i do to add anew trait to previous traits?
The Font Symbolic Trait is a bitmask (NS_OPTIONS). You need to set the flag to either one or both based on your selected options.
Something like :
UIFontDescriptor* italicBoldFontdescriptor = [bodyFontDescriptor
fontDescriptorByAddingAttributes:#{UIFontDescriptorTraitsAttribute:
#{ UIFontSymbolicTrait: #(UIFontDescriptorTraitItalic | UIFontDescriptorTraitBold) }
}];
The fontDescriptorByAddingAttributes will, as the per the doc,
returns a new font descriptor that is the same as the receiver but with the specified attributes taking precedence over the existing one
This will allow you to keep other settings.

input dialog box blender

How to make a simple entry dialog box (like in the image) in blender and processing the text entered through python.I am unable to find any good tutorial on this.
For the dialog box the answer from how to show a message from a blender script? might be a starting point.
But I think a better approach is integrating input into the panel like e.g.
To do this you have to add a StringProperty to your add-on and place it inside your panel (see Addon Tutorial for more information). The basic steps are:
def draw(self, context) :
col = self.layout.column(align = True)
col.prop(context.scene, "my_string_prop")
...
def register() :
bpy.types.Scene.my_string_prop = bpy.props.StringProperty \
(
name = "My String",
description = "My description",
default = "default"
)
...
def unregister() :
del bpy.types.Scene.my_string_prop
...
You can access the string by context.scene.my_string_prop
There is another mode to integrate input. When you add for example a text to your scene you can change the parameters after the operator has been called and see the changes immediately:
Changing Location will move the newly created text object at another place. I haven't worked with this but it should be similar to the code above.

Using NCalc in Visual Studio 2010 - VB.Net

As the title states, I need to use NCalc in Visual Studio 2010. I am writing a program that will calculate a multi-step math problem in VB, but I'm not quite sure how I'd use NCalc. The user inputs a math problem into a text box, then clicks a button which will tell the program to calculate the answer. I've already added NCalc as a reference, and imported it into the project, I'm just not sure what to do next.
You'd need to add, at the top of your file:
using NCalc.Domain;
You could then write something in a button press like:
Expression exp = new Expression(textBox1.Text); // Get the text box text
try
{
object result = exp.Evaluate();
textBox2.Text = result.ToString(); // Place the "answer"
}
catch(EvaluationException e)
{
// This happens if the user enters a "bad" expression
textBox2.Text = "Unable to compute: " + e.Message;
}
For details, see the examples on NCalc's homepage.