Changing Aptana3's auto bracket completion to { ... } instead of {...} - aptana

I'm programming in Ruby, where convention often dictates spacing around curly brakets like so:
grades = { Rob: 82, Billy: 58 }
I have been trying to get Aptana to change it's auto braket completion feature to insert a space when I type '{' so that I end up with my cursor in the middle of two curly braces which already have space padding like so: { | }.
I have edited my ruby ruble smart_typing_pairs variable from
smart_typing_pairs['source.ruby'] = ['"', '"', '|', '|', '(', ')', '{', '}', '[', ']', "'", "'", '', '']
to
smart_typing_pairs['source.ruby'] = ['"', '"', '|', '|', '(', ')', '{', ' }', '[', ']', "'", "'", '', '']
(Notice the space in the second })
But this just breaks bracket completion for braces. I can't seem to find an automatic string replacement feature in Aptana either.
Can anyone give me any hints? Thanks!

Related

Regular expression meaning in R : "( \n|\n )"

I am new to R programming and was trying out the gsub function for text replacement in pandas dataframe series(i.e new_text).
It a vast series so will not be able to print all here.
It is just a series with strings containing postal address.
I came across this gsub code : gsub(pattern = "( \n|\n )", replacement = " ", x = new_text) -> new_text
can you please let me know the meaning of this regex expression as well as the python alternative using regex expression.
Your pattern, slightly rewritten, is [ ]\n|\n[ ], which says to match:
[ ]\n a space followed by a newline
| OR
\n[ ] a newline followed by a space
Note that you might be able to use [ ]?\n[ ]? to the same effect, depending on the actual text you are using with gsub.

Conditionally creating formatted string from elements and attributes in XQuery

I'm trying to convert an xml document into a specific tab separated flat file structure. Most of the elements can be mapped to single columns or concatenated simply using fn:string-join(), but I have some elements where the mapping is more complicated. An example element looks like this:
<record>
<details>
<passports>
<passport country="">0018061/104</passport>
<passport country="UK">0354761445</passport>
<passport country="USA">M001806145</passport>
</passports>
</details>
<record>
and I need to create a column that looks like this:
0018061/104;(UK) 0354761445;(USA) M001806145
so if the #country attribute is not "" it is put in (), otherwise it is omitted. The element value follows and each element is separated by ;.
Here's what I have done so far:
for $record in //record
return concat($record/#uid/string(),
(: ... other columns ... :)
" ", <S>{for $r in //$record/details/passports/passport
return concat("(", $r/#country, ") ", $r, ";")}</S>/string()
,"
")
I'm sure there's an easier way, but this almost does the job - it produces:
() 0018061/104;(UK) 0354761445;(USA) M001806145
Ideally I'd like to know the correct way to do this, otherwise just removing the empty brackets where #country="" would suffice.
Use an if clause right in the outer concat (I added some newlines for better readability in the answer, you can of course remove them as you wish):
concat(
if ($r/#country != "")
then concat("(", $r/#country, ") ")
else "",
$r,
";"
)
New result of the query:
0018061/104; (UK) 0354761445; (USA) M001806145;
You could also go for an implicit loop
/record/details/passports/passport/string-join(
(
" ",
if (#country != "")
then "(" || #country || ") "
else (),
.
), ""
)
or explicitly loop over the results and still have a cleaner query (by replacing the concatenation operator || by respective concat(...) calls, you would stay XQuery 1.0 compatible):
for $record in /record/details/passports/passport
return (
" " || (
if ($record/#country != "")
then "(" || $record/#country || ") "
else ()
) || $record
)
Both cases use the implicit newlines inserted by BaseX in-between tokens, alternatively you can of course add them as you had before.

Why does the gsub function in R doesn't work on '.' operator?

I have an array named myfile which has all the dates in character format.
For eg - "2014.01.29" "2014.02.02" "2014.01.09" "2014.01.23" "2014.01.09" "2014.01.29"
Now, I want to replace this '.' operator to '-'. So I want "2014.01.29" to be like "2014-01-29".
When I use the code
gsub('.' , '-' , myfile[1])
I get the output as '----------'. The command works absolutely normal when I replace '.' in gsub with anything else. Any help would be appreciated.
You need to escape the . which can be done either putting it in a [.] or \\..
gsub('[.]', '-', myfile[1])
or
gsub('\\.', '-', myfile[1])

Why does this Selenium XPath expression fail to return results when concat() is used?

When I use the following XPath expression in Selenium, it works correctly. Its finds an ancestor div of the current element which contains a certain string in its class attribute:
inputEl.findElement(By.xpath("ancestor::div[contains(#class, 'x-form-item')]")).getAttribute("class")
The answer it gives is "x-form-item " (notice the trailing space).
However, I want ancestors which precisely have the x-form-item class, and not other classes like x-form-item-label. So I changed the expression as follows:
inputEl.findElement(By.xpath("ancestor::div[contains(concat(' ', #class, ' '), ' x-form-item ')]")).getAttribute("class")
However, once this in place, Selenium is unable to find the element. It gives this error:
org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == ancestor::div[contains(concat(' ', #class, ' '), ' x-form-item ')] (WARNING: The server did not provide any stacktrace information)
At first I thought I had some kind of mistake, so to simplify, I removed the leading/trailing space. Presumably this is semantically the same as my very first, working query:
inputEl.findElement(By.xpath("ancestor::div[contains(concat('', #class, ''), 'x-form-item')]")).getAttribute("class")
However, this also fails:
org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == ancestor::div[contains(concat('', #class, ''), 'x-form-item')] (WARNING: The server did not provide any stacktrace information)
So my fundamental question is, why are these two resulting in different strings?
#class
concat('', #class, '')
Note: I am using the IEDriver with IE9
To achieve what you want, you don't even need concat(' ', #class, ' ').
You should be able to use
By.xpath("ancestor::div[contains(#class, ' x-form-item ')]")
because you're looking for ' x-form-item ' in the '#class' tag, not looking in the ' #class ' tag.

How to sanitize the post_name value before inserting in WordPress?

How to sanitize the post_name value before inserting in WordPress?
Simple:
$post_title = sanitize_title_with_dashes($post_title);
But WordPress does this for you already. I assume you need it for something different?
I'm guessing you're sanitizing by direct SQL insertion. Instead, consider using wp_post_insert() in your insertion script.
$new_post_id = wp_insert_post(array(
'post_title' => "This <open_tag insane title thing<b>LOL!;drop table `bobby`;"
));
At this point, you just worry about your title - and not the slug, post name, etc. WP will take care of the rest and (at least security) sanitization. The slug, as demonstrated in the screenshot, becomes fairly usable.
This function can be used by simply doing include( "wp-config.php" ); and going about your business without any other PHP overhead.
If you are dealing with some funky titles to begin with, a simple strip_tags(trim()) might do the trick. Otherwise, you've got other problems to deal with ;-)
Some solution might be found at http://postedpost.com/2008/06/23/ultimate-wordpress-post-name-url-sanitize-solution/
Also, you might want to do it as follows:
$special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}");
$post_name = str_replace(' ', '-', str_replace($special_chars, '', strtolower($post_name)));

Resources