James Fishwick

  •  Minimal
  •  CSS Tomfoolery

Automating swfdump to bulk generate flash dimensions or other metadata

Code Snippet, linux, Note to Self, Production Automation

No Comments


Share this post

Using a MarkLogic application at work to manage and deploy content. We have almost no available metadata nearly any of our thousands and thousands of video and flash content. We’re generating it by hook or by crook, often repurposing editorial transmittal grids into csv for import. Needless to say, stuff like native dimensions or the like have been lost in the sands of time – existing html embeds are done in a multitude of slapdash ways, with height and width varying greatly per instance.

I should also say that this is all legacy content being shoehorned into “responsive” courseware. So there are width restrictions now, when before these activities were just popped up in new windows with “100%” widths.

Anyway, when trying to place said swfs into the product, the embed codes for an entire disciple were completely wrong. Things squished, white spaced abounded. Turns out the person who loaded the original metadata set all the widths to the max width for the product (595 px) and then grabbed heights from the questionable html wrappers. But they didn’t in turn adjust this grabbed height in light of a new width. Oooooof. A complete mess!

I needed to start from scratch, but I was looking at several hundred swfs. Grepping the dimensions from the html was out, as they were suspect. As was opening each in Flash, I refuse to do manual labor! I needed a csv with all the filenames and native dimensions. If the width is over 595px, I needed an aspect ratio calculation done.

So swfdump and bash to the rescue!

Read more

Can regex even match a null character?

CLI, Code Snippet, Note to Self

No Comments


Share this post

Yes.
\x00
That is a null char and you can match it with any PCRE engine. Note that most visual tools, like Dreamweaver etc, won’t handle this well, and won’t even display it. In fact, most will stop processing the file when they hit the null character.

The exception being the mighty Notepad++. So use that if you’re afraid of the CLI.

Read more

Converting Strings into Numbers

Code Snippet, Javascript, Note to Self

No Comments


Share this post

You need to convert a string to a number.  Several ways, with varying purposefulness:

Number constructor called as a function simply performs type conversion:

// type conversion
Number("911");        // 911
Number("20px");       // NaN
Number("2e1");        // 20, exponential notation
Number("010");        // 10 (The Number constructor doesn't detect octals)
Number("8,569")       // Nan
Number(8,569)         // 8
Number("8.569")       // 8.569
Number("0xF");        // 15 (But it can handle numbers in hexadecimal notation)
Number("false");      // NaN
Number(true)          // 1

parseInt() performs parsing into an integer*:

// parsing:
parseInt("999");        // 999
parseInt("20px");       // 20 (will stop parsing and drop any part of the string it can't figure out)
parseInt("fi5e")        // NaN (string has to start making some sense right away)
parseInt("10100", 2);   // 20
parseInt("2e1");        // 2
parseInt("010");        // 8 (if parseInt detects a leading zero on the string, it will parse the number in octal base)
parseInt("010", 10);    // 10 (decimal radix used)
parseInt("0xF");        // 15 (can also handle numbers in hexadecimal notation
parseInt("22.5")        // 22 (because the decimal point is an invalid character for an integer)
Number("false");        // NaN
Number(true)            // NaN

*It’s always a good idea to supply a radix to parseInt(value, radix) that way you don’t have accidental octal mode conversions.

The parseFloat() method works in a similar way to parseInt() however the string must represent a floating-point number in decimal form, not octal or hexadecimal.

parseFloat("1234blue"); 	// 1234.0
parseFloat("0xA"); 	// NaN
parseFloat("22.5"); 	// 22.5
parseFloat("22.34.5"); 	// 22.34
parseFloat("0908"); 	// 908
parseFloat("James1980"); 	// NaN

The Unary Plus method (+num) is basically a short cut for casting to a number via Number(). Just a nice shortcut really.

+"911"; // 911
+"20px";       // NaN
//etc etc
num = 1 + +"2"; // num is assigned the value 3
+true; // 1 (just saying...)

Finally, you can attempt some simple math on your string, does the same as the above.

"123" / 1; // 123
"123.987" * 1; // 123.987

You can use num / 1;, num * 1;, 0 + num; or 1 * num;.

If you’re interested in a speed comparison, see this test case (this would only matter for huge numbers of conversions. HUGE.)

Read more

total directory size hold the extra foo

CLI, Code Snippet

No Comments


Share this post
du -h | tail -n 1

Read more

bash find and boolean operators

CLI, Code Snippet, Note to Self

No Comments


Share this post

Just because it took me way too long this morning to figure out how Boolean operators work with find.

Suppose I want to find the files with .png and .jpg extensions.

Its not

$ find /path/ -name '*.png' -and -name '*.jpg'

but

$ find /path/ -name '*.png' -or -name '*.jpg'

The “-and” refers to one set of file names where both conditions are met (as if we didn’t use the boolean at all). The “-or” says I’m looking for either/both of two sets.

So to explain further:

Read more

Download a list of HTML pages with dependencies

CLI, Code Snippet

No Comments


Share this post
wget --page-requisites -i list.txt

Where ‘list.txt’ is a list of URLs separated by line breaks.

If you aren’t interested in certain files, say images, there is a further flag to reject certain file types:

--reject=gif,jpg,png

I love wget!

 

Read more

Conditional Tags for Custom Post Types & Taxonomies

Code Snippet, Note to Self, WordPress

No Comments


Share this post

Most WordPress instances used to power a full site or shop will hopefully take advantage of the power combination of Custom Taxonomies and Custom Posts. Often you want to style a list of your Custom Posts by Custom Taxonomy and have the single view styled in the same way, or perhaps have the same sidebar. You can do this with by leveraging the Template Hierarchy and registering tons of sidebars. However, before long, you can find your theme folder bloated with extra templates. I don’t like having tons of templates in my themes. Too much code duplication usually. Rather, I like to use Conditional Tags and get_template_part. When dealing with Custom Taxonomies & Post Types it may not be clear at first how one can best use Conditional Tags to good use. Lets take a look.

Read more

check if checkbox/radio is checked using jQuery

Code Snippet, jQuery, Note to Self

No Comments


Share this post
//returns true or false
$('"input[type=radio]').is(':checked');

$("input[type=checkbox]:checked").each(
    function() {
       // do something at each checked radio or checkbox
    }
);

Read more

Modernizr and jQuery and wp_enqueue_script oh my

Code Snippet, Javascript, Note to Self, WordPress

No Comments


Share this post

Some questions on the best way to use Modernizr with jQuery in the context of WordPress…

I want to be able to use jQuery in my complete callbacks. I, of course, want to be able to use wp_enqueue_script.

Read more

Capitalizing strings in PHP

Code Snippet, Note to Self, PHP

No Comments


Share this post
string ucfirst ( string $str )

Returns a string with the first character of str capitalized, if that character is alphabetic.

Therefore, you can do:

Read more