Markedstyle: Managing Styles for Marked With Laravel

What are Markdown and Marked? See the footer.

TL;DR: I just wrote an app called Markedstyle, a centralized repository for custom Marked CSS styles.

I’ve been looking for an opportunity to rapidly prototype a simple web app using Laravel 4 (the best way to learn is to practice), so when I saw Brett Terpstra’s recent post on collecting styles for his app Marked I knew I had a perfect opportunity.

I wish I had run an actual timer, but within a few very brief evening programming sessions I had a basic REST-ish app allowing me to create, view, and edit styles and users. I used Bootstrap for rapid front-end prototyping, but followed the tips at Bootstrap without all the debt to save myself the pain of Bootstrap classes in the HTML.

The ease with which I can prototype an app in Laravel is incredible, but even more important is that I feel confident that I wouldn’t have to change much to consider this a production app. Getting the basic resource routes and controllers up and running, as well as database schemas and seeds prepared, was incredibly simple. Suffice it to say that I’m in love with Laravel.

I filled the app with the styles currently available at Brett’s Git repo, and made a wishlist for features I hope to develop soon–voting, Sass/SCSS style upload, etc. I then deployed it to ArcusTech, so it should be incredibly fast.

The site is online at markedstyle.com and the source code is publicly viewable at github.com/mattstauffer/markedstyle. I know this isn’t a login-every-day kind of thing, and that it’ll be a lot more useful once I’m tracking clicks and votes, but I figured I’d put it out there for now and see what the Marked and Laravel communities think of it.

Thanks! Please share any thoughts (or if I totally screwed up the code, pull requests).

What are Markdown and Marked?

Markdown is an incredible tool for formatting text in a light, clean manner; text written in Markdown is legible before it’s processed, easily written by hand, and easily processed by computer.

Marked is a simple, clean program that monitors local Markdown files and actively updates a preview window every time they change.

Download Latest SQL Backup Every Day

Today I got a question from a Karani user about our backups. We maintain backups on-server, and our host maintains backups off-site, so I felt comfortable assuring the user of the security of our backups. But inside I thought–wouldn’t it be great if I could get a daily copy of the latest SQL backup? Turns out it’s extremely easy.

StackOverflow to the rescue. This is focused around Unix-based systems, so, sorry Windows folks.

Step 1: Create a shell script to use scp to download your latest backup

Here’s mine:

remote_dir=/www/remote-user-name/sql_backup_directory
target_dir=~/local_sql_directory
destination=ssh_username@hostname
scp $destination:`ssh $destination ls -1td $remote_dir/\* | head -1` $target_dir

Of course, you’ll want to update remote_dir, target_dir, and destination’s values to be appropriate for your system.

Note: When I first created this file, the permissions weren’t correct for cron to run it. I chmod’ed the file to 777 to test to make sure it works, but I still need to figure out what the absolute best chmod value would be.

Note 2: You’ll need to have ssh key authorization already set up for this domain so scp & ssh can access it properly.

Step 2: Use cron to run that shell script every day

First, edit your crontab:

$ crontab -e

Then, paste your line:

0 2 * * * /path/to/shell/script.sh

The above line runs the script at 2am every day, but you can adjust the timing–learn more about cron timing.

Step 3: Profit

That’s it! You now have a local cron job running once a day, copying your remotely generated files to your local computer.

Tips:

Using the crontab editor

By default, the crontab editor uses vim. Trust me when I tell you that learning vim is absolutely worth it–check out the intro to vim that ships with vim by running $ vimtutor to learn the basics in 5-10 minutes. But if you need to just get by for now:

Type i to enter Insert mode, and now you can type. To save, type esc and then type :x and hit return. That’s it!

Cron job for running SQL backup

Are you looking for a server-side cron job to set up daily SQL dumps? Here’s mine:

0 1 * * * /usr/bin/mysqldump -h host-name-here -uusername-here -ppassword-here --databases database-name-here > /path/to/sql/dumps/$( date + \%Y-\%m-\%d).sql

That generates files with the date name as the file name, every night at 1am. You can adjust the timing, and be sure to put your own: route to mysqldump, MySQL host name, username, password, and database name. Notice that the user flags and password flags do not have spaces–so if your username is bob and your password is secret, those flags would be -ubob and -psecret .

Emailing the results

How do you debug problems? If you add the following line to the top of your cron job, it will email you any output from the script.

MAILTO=youremailaddress@hotmail.com

I haven’t yet tested to see if this works locally, though, so I’ll update this when I have.

Even Shorter Ternary Operators in PHP Using ?:

Today I discovered something about PHP’s ternary operator that gave my DRY soul a little bit of joy.

The PHP ternary operator is often a key player in the terseness vs. clarity argument. A brief refresher: The PHP ternary operator allows you to write single-line comparisons, replacing code like the following:

1
2
3
4
5
6
<?php
if (isset($value)) {
    $output = $value;
} else {
    $output = 'No value set.';
}

with this:

1
2
<?php
$output = isset($value) ? $value : 'No value set.';

The second code example is clearly simpler, and in many cases (although certainly not all), it still retains enough clarity to be a worthy tool. There’s plenty of debate about whether the ternary operator sacrifices clarity at the expense of conciseness; let’s just say it’s a tool, and like any tool, it can be used well or poorly.

The syntax for the regular ternary operator is (expression) ? value if truthy : value if falsy. The expression can also just be a single variable, which will test whether the variable is truthy or falsy:

1
2
<?php
$output = $value ? $value : 'No value set.';

The problem is, the above example is both common and annoyingly repetitive: Having to write $value twice like that just feels wrong.

Well, I discovered today that PHP 5.3 introduced an even terser syntax for this use of the ternary operator. You can learn more at the docs, but here’s how we could make the above example even more concise:

1
2
<?php
$output = $value ?: 'No value set.';

If this looks familiar, it’s because it is: this is exactly how PHP shortens other operators, like shortening this:

1
2
<?php
$value = $value . $other_value;

to this:

1
2
<?php
$value .= $other_value;

For the sake of clarity, just because we can shorten something doesn’t mean we should. But, when we can write terse code that is also appopriately clear, we should, and this feature allows us to DRY up the ternary operator in many cases.

Falling in Love With PHP–again

PHP is one of the most maligned languages today–even more hated than JavaScript, which says a lot.

The problem is that there are thousands of PHP sites, PHP apps (and PHP developers) that are low quality, untested, procedural, antiquated, poorly designed and poorly commented. Much PHP code is flaky, hackable, and ugly, and the language itself was clearly not designed with modern coding standards and conventions in mind–its naming is inconsistent, its OOP features are frustratingly-designed afterthoughts, and it suffers from myriad other painful little problems that sum up to a big pain in the rear.

I’ve developed in CodeIgniter, a PHP-based MVC framework, for years, and while the addition of a framework and of the MVC methodology have brought some much-needed structure to my PHP world, I’ve still longed for the beauty, simplicity, and expressiveness of Ruby on Rails. After working on a few Rails apps for work, I came close to fully changing over my loyalties.

And then came Laravel. Laravel is a light, flexible, expressive framework that capitalizes on the best parts of PHP and does its best to supplant the places where PHP is most lacking. Laravel is powerful, extensible, rewritable, and is in active development by some really brilliant people. The learning curve is low, the power is high, and I cannot begin to express the joy it’s given me to work in this framework.

Laravel has singlehandedly rekindled my love for PHP, and my hope that it may actually contend with Ruby (especially on Rails) as a legitimate language for web app development.

Taylor Otwell, I owe you a beer. And my PHP-coding life.

What Is an Accidental Bootstrap Founder?

LessConf Sand Sculpture

Before LessConf 2013, I didn’t see myself an entrepreneur, let alone a “startup founder.” I knew what an entrepreneur and a founder looked like, and I was neither.

I worked as a freelance web developer during college, but after graduation I left the tech world to work at a non-profit. My job included raising funds to support my salary, something I was ill equipped to do. One of the hardest parts of my job was the administrative work—tracking contacts, interactions, gifts, pledges.