RText: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
Aggiunto l'esempio di una macro Groovy che tenta l'apertura di un file. |
Aggiunta di una macro di esempio nella sezione Dettagli sulle macro. |
||
Riga 114:
Lo stesso esempio programmato in linguaggio [[Groovy]]:
<source lang="
import java.awt.*
import javax.swing.*
Riga 161:
} finally {
textArea.endAtomicEdit()
}
</source>
====Esempio di una macro che converte i caratteri speciali per il linguaggio HTML====
L'esempio che segue mostra lo script [[JavaScript]] di una macro che sostituisce qualsiasi testo selezionato con una versione di quel testo formattato in [[HTML]]:
<source lang="javascript">
function replaceMultipleSpaces(text) {
var p = java.util.regex.Pattern.compile(" +");
var m = p.matcher(text);
var sb = new java.lang.StringBuffer();
while (m.find()) {
var spaces = m.group();
m.appendReplacement(sb, spaces.replace(" ", " "));
}
m.appendTail(sb);
return sb.toString();
}
textArea.beginAtomicEdit();
try {
var text = textArea.selectedText;
if (text==null || text.length()==0) {
javax.swing.JOptionPane.showMessageDialog(rtext,
"Error: No selection.\n" +
"Text must be selected to HTML-ify.",
"Error", javax.swing.JOptionPane.ERROR_MESSAGE);
}
else {
text = text.replace("&", "&").replace("\"", """).
replace("<", "<").replace(">", ">").
replace("\t", "	").replace("\n", "<br>\n");
if (text.contains(" ")) { // Replace multiple spaces with sequences
text = replaceMultipleSpaces(text);
}
var start = textArea.getSelectionStart();
textArea.replaceSelection(text);
textArea.setSelectionStart(start);
textArea.setSelectionEnd(start+text.length());
}
} finally {
textArea.endAtomicEdit();
}
</source>
|