I'm trying to modify a file name just before the extension, in this case.
So, for example the file is called image1.jpg and I would like to upload it as image1_thumbnail.jpg.
Currently the code places thumbnail_ at the beginning so it outputs thumbnail_image1.jpg but I need it at the end.
I am using at the moment…
<cfimage action="resize"
height="#thumbHeight#"
width="#thumbWidth#"
source="#uploadedImage#"
destination="#MediaPath#/thumbs/thumbnail_#file.serverFile#"
overwrite="true"/>
<cfoutput>
<img src="photos/thumbs/thumbnail_#file.serverFile#"
height="#thumbHeight#" width="#thumbWidth#" align="left" hspace="10">
Any ideas very much appreciated.
You can treat file.serverfile as a period delimited list.
destination = ListFirst(file.serverfile, '.')
& "_thumbnail."
& ListRest(file.serverfile, '.')
Per Leigh's suggestion, this is probably easier done with something like this:
<cfset thumbfileName = cffile.serverFileName & "_thumbnail." & cffile.serverFileExt>
...
destination="#MediaPath#/thumbs/#variables.thumbfileName#"
...
...
<img src="photos/thumbs/#variables.thumbfileName#" ...
Related
I'm trying to read some values from the XML file which I created, but it gives me the following error:
coldfusion.runtime.UndefinedElementException: Element MYXML.UPLOAD is undefined in XMLDOC.
Here is my code
<cffile action="read" file="#expandPath("./config.xml")#" variable="configuration" />
<cfset xmldoc = XmlParse(configuration) />
<div class="row"><cfoutput>#xmldoc.myxml.upload-file.size#</cfoutput></div>
Here is my config.xml
<myxml>
<upload-file>
<size>15</size>
<accepted-format>pdf</accepted-format>
</upload-file>
</myxml>
Can someone help me to figure out what is the error?
When I am printing the entire variable as <div class="row"><cfoutput>#xmldoc#</cfoutput></div> it is showing the values as
15 pdf
The problem is the hyphen - contained in the <upload-file> name within your XML. If you are in control of the XML contents the easiest fix will be to not use hyphens in your field names. If you cannot control the XML contents then you will need to do more to get around this issue.
Ben Nadel has a pretty good blog article in the topic - Accessing XML Nodes Having Names That Contain Dashes In ColdFusion
From that article:
To get ColdFusion to see the dash as part of the node name, we have to "escape" it, for lack of a better term. To do so, we either have to use array notation and define the node name as a quoted string; or, we have to use xmlSearch() where we can deal directly with the underlying document object model.
He goes on to give examples. As he states in that article, you can either quote the node name to access the data. Like...
<div class="row">
<cfoutput>#xmldoc.myxml["upload-file"].size#</cfoutput>
</div>
Or you can use the xmlSearch() function to parse the data for you. Note that this will return an array of the data. Like...
<cfset xmlarray = xmlSearch(xmldoc,"/myxml/upload-file/")>
<div class="row">
<cfoutput>#xmlarray[1].size#</cfoutput>
</div>
Both of these examples will output 15.
I created a gist for you to see these examples as well.
I am trying to take convert urls in a block of html to ensure they are lowercase.
Some of the links are a mix of uppercase and lowercase and they need to be converted to just lowercase.
It would be impossible to run round the site and redo every link so was looking to use a Regex when outputting the text.
<p>Hello world Some link.</p>
Needs to be converted to:
<p>Hello world Some link.</p>
Using a ColdFusion Regex such as below (although this doesn't work):
<cfset content = Rereplace(content,'(http[*])','\L\1','All')>
Any help much appreciated.
I think I would use the lower case function, lCase().
Put your URL into a variable, if it's not already:
<cfset MyVar = "http://www.ThisSite.com">
Force it to lower case here:
<cfset MyVar = lCase(MyVar)>
Or here:
<cfoutput>
Some Link
</cfoutput>
UPDATE: Actually, I see that what you are actually asking is how to generate your entire HTML page (or a big portion) and then go back through it, find all of the links, and then lower their cases. Is that what you are trying to do?
Since you have the HTML stored in a database, there is a bit more work that needs to be done than just using lcase(). I would wrap the functionality into a function that can be easily reused. Check out this code for an example.
content = '<p>Hello world Some link.</p>
<p>Hello world Some link.</p>
<p>Hello world <a href=''http://www.somelink.com/BLARG''>Some link</a>.</p>';
writeDump( content );
writeDump( fixLinks( content ) );
function fixLinks( str ){
var links = REMatch( 'http[^"'']*', str );
for( var link in links ){
str = replace( str, link, lcase( link ), "ALL" );
}
return str;
}
This has only been tested in CF9 & CF10.
Using REMatch() you get an array of matches. You then simply loop over that array and use replace() with lcase() to make the links lowercase.
And...based on Leigh's suggestion, here is a solution in one line of code using REReplace()
REReplace( content, '(http[^"'']*)', '\L\1', 'all' )
Use a HTML parser to parse HTML, not regex.
Here's how you can do it with jQuery:
<!doctype html>
<script src="jquery.js"></script>
<cfsavecontent variable="HtmlCode">
<p>Hello world Some link.</p>
</cfsavecontent>
<pre></pre>
<script>
var HtmlCode = "<cfoutput>#JsStringFormat(HtmlCode)#</cfoutput>";
HtmlCode = jQuery('a[href]',HtmlCode).each( lowercaseHref ).end().html();
function lowercaseHref(index,item)
{
var $item = jQuery(item);
// prevent non-links from being changed
// (alternatively, can check for specific domain, etc)
if ( $item.attr('href').startsWith('#') )
return
$item.attr( 'href' , $item.attr('href').toLowerCase() );
}
jQuery('pre').text(HtmlCode);
</script>
This works for href attributes on a tags, but can of course be updated for other things.
It will ignore in-page links like <a href="#SomeId"> but not stuff like <a href="/HOME/#SomeId"> - if that's an issue you'd need to update the function to exclude page fragment part (e.g. split on # then rejoin, or whatever). Same goes if you might have case-sensitive querystrings.
And of course the above is just jQuery because I felt like it - you could also use a server-side HTML parser, like jSoup to achieve this.
Consider test.cfm file with the following content:
<html>
<body>
<cfif foo EQ bar>
<cfset test = "something" />
</cfif>
<p>Hello!</p>
</body>
</html>
When run in the browser, the source code of the output of this file will look like this:
<html>
<body>
<p>Hello!</p>
</body>
</html>
Is there any way to fix this?
Is there any way to fix this?
There's nothing to fix - the HTML is perfectly valid and functional.
If your issue is the size of request, use gzip encoding.
If your issue is reading the source for debugging/etc, use developer tools such as Firebug/etc.
However, general things you should be doing to improve maintainability (which at the same time also reduces whitespace output) are:
1) Move anything that isn't display logic out of your views.
2) Convert display logic to functions and custom tags as appropriate, which both make it easier to prevent/control output.
To prevent unwanted content being output, you can:
Wrap the entire section in cfsilent, to ensure nothing gets output.
Enable enablecfoutputonly attribute of cfsetting then only use cfoutput around things you want to be output.
Always set output=false on component and function tags.
When you want to selectively output some text, wrap non-tag non-output segments in CFML comments <!---...---> (e.g. useful for preventing newline output in custom tags)
(I never bother with cfprocessingdirective, everything mentioned above solves the issues better.)
If you have access to the CF Administrator, there is an option to suppress white space.
It is under 'Server Settings' --> 'Settings' its called 'Enable Whitespace Management'.
Try <cfprocessingdirective suppressWhiteSpace="true">
Reference: http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-76de.html
I’ve created a file index of all of my ColdFusion files so I can quickly search the files and find what I’m looking for. So far, it’s working great except it doesn’t seem to be searching inside any ColdFusion tags.
For example…
<p>If I searched for this text, It would return a result</p>
<cfset variables.foo = "however, If I search for this text it wouldn’t return any results." />
Does anyone know if there’s a way to search inside of a ColdFusion tag like that?
This is my index..
<cfindex
collection = "fileIndex"
action="refresh"
type="path"
key="d:\my-websites-location\"
urlpath="http://mywebsite/"
extensions=".cfm, .cfml, .cfc"
recurse="Yes">
This is my search…
<cfsearch
name = "testSearch"
collection = "fileIndex"
type="internet"
criteria = "variables.foo"
/>
Any ideas?
Thanks,
Paul : )
It looks like the type="internet" may be your issue. Try removing the "type" attribute and see what you get.
Use a query that does get the record and look at the "summary" field of your result. I suspect the markup is being stripped.
On ColdFusion 9, with solr it doesn't index the markup, however Verity does. A workaround you could use a combination of cffile/cfdirectory to read each file one by one and feed it into the collection. This will preserve the markup and make it searchable.
Or you can enclose your criteria variable with ##.
<cfsearch
name = "testSearch"
collection = "fileIndex"
type="internet"
criteria = "#variables.foo#"
/>
Is there a more succinct/correct/pythonic way to do the following:
url = "http://0.0.0.0:3000/authenticate/login"
re_token = re.compile("<[^>]*authenticity_token[^>]*value=\"([^\"]*)")
for line in urllib2.urlopen(url):
if re_token.match(line):
token = re_token.findall(line)[0]
break
I want to get the value of the input tag named "authenticity_token" from an HTML page:
<input name="authenticity_token" type="hidden" value="WTumSWohmrxcoiDtgpPRcxUMh/D9m7O7T6HOhWH+Yw4=" />
Could you use Beautiful Soup for this? The code would essentially look something like so:
from BeautifulSoup import BeautifulSoup
url = "hhttp://0.0.0.0:3000/authenticate/login"
page = urlli2b.urlopen(page)
soup = BeautifulSoup(page)
token = soup.find("input", { 'name': 'authenticity_token'})
Something like that should work. I didn't test this but you can read the documentation to get it exact.
You don't need the findall call. Instead use:
m = re_token.match(line)
if m:
token = m.group(1)
....
I second the recommendation of BeautifulSoup over regular expressions though.
there's nothing "pythonic" with using regex. If you don't want to use BeautifulSoup(which you should ideally), just use Python's excellent string manipulation capabilities
for line in open("file"):
line=line.strip()
if "<input name" in line and "value=" in line:
item=line.split()
for i in item:
if "value" in i:
print i
output
$ more file
<input name="authenticity_token" type="hidden" value="WTumSWohmrxcoiDtgpPRcxUMh/D9m7O7T6HOhWH+Yw4=" />
$ python script.py
value="WTumSWohmrxcoiDtgpPRcxUMh/D9m7O7T6HOhWH+Yw4="
As to why you shouldn't use regular expressions to search HTML, there are two main reasons.
The first is that HTML is defined recursively, and regular expressions, which compile into stackless state machines, don't do recursion. You can't write a regular expression that can tell, when it encounters an end tag, what start tag it encountered on its way to that tag it belongs to; there's nowhere to save that information.
The second is that parsing HTML (which BeautifulSoup does) normalizes all kinds of things that are allowable in HTML and that you're probably not going to ever consider in your regular expressions. To pick a trivial example, what you're trying to parse:
<input name="authenticity_token" type="hidden" value="xxx"/>
could just as easily be:
<input name='authenticity_token' type="hidden" value="xxx"/>
or
<input type = "hidden" value = "xxx" name = 'authenticity_token' />
or any one of a hundred other permutations that I'm not thinking about right now.