DMelt:General/7 Working with text

From jWork.ORG
Jump to: navigation, search
Limitted access. Reguest membership or login to this link first if you are already a member
Contents

Working with text in IDE

You can use DataMelt IDE as a usual text editor, jeHEP. The editor is a highly configurable text editor built to enable efficient text editing. You can do copy, paste, replace and find using the tool bar menu, as for any other editor. The editor also has syntax highlighting almost for any programming language.

But you can do something more: The main idea of DataMelt is that you can use Jython scripts to access any Java library. This also means that you can access the TextArea of the scripts itself and all internal variables of the IDE at runtime.

Note Note: In fact, a script can modify itself at runtime, or even design itself while executing itself. This offers a tremendous power!

Let us consider how to access internal variable of the DataMelt IDE. Load this script into the IDE and execute it:

from jehep.ui import SetEnv
from jehep.ui import Editor
print Editor.DocName() # name of this script
print Editor.DocDir()  # directory of this script
print SetEnv.ProjDir   # project directory
print SetEnv.ClassPath # Java classpath

Now let us make a Jython script which, after being loaded to DataMelt IDE, prints itself:

print view.getText()                 # access the TextArea of this script and prints itself
cc=view.getCaretPosition()           # get the current caret position
view.insertString("inserted text at position 4", 4) # inset some text at position 4

In fact, this script simply destroys itself since it inserts some line in itself! Note "view" in this case is an instance if the TextArea which is used to keep the file. This class is very comprehensive, it has about 100 methods for text manipulation.

Now, let us do something useful and constrictive. What we want to do is to generate an HTML page with LaTeX equation. As we know from above, we can use double dollar character to enclose LateX equation and generate an HTML file with equation image using "CodeView" ([View]-[CodeView] menu).

But we do not need to use GUI for this, in DataMelt one can use just a script which will do everything you need.

"""
<H2>This is an equation </H2>
Our equation is 
$$
y=frac{sin(x)}{2*sqrt(x)}
$$
"""
# 1 means to display HTML file
view.generateHtml(1)

Look at view.generateHtml(1) method. The script executes itself, generates an HTML file from the multiline comment and opens a HTML browser inside DataMelt. Your HTML file with the equation is ready! The file is located in the directory "cachedir" together with the image of this equation.


Text processing using Python macros

In this section we will discuss how to write simple Python macros to manipulate text loaded to the DataMelt IDE. While executing these macros, you will see how the text changes while Jython performs various operations. We will write this macros in Python, but will call active Java objects used to keep the text in DataMelt IDE edit window. In this Python macro, we:

 - Load a text file into DataMelt IDE text area
 - Remove some part of the text
 - Determine the current caret position
 - Inset a text at some specific caret position


More information on this topic is in DMelt books