March 24, 2019

TextMate Vertical Selction

textmate tips
March 23, 2019

TextMate Multiple Cursors

textmate tips tuturials
January 22, 2018

SunSed Program to Find the Fibonacci Series Using Recursion

Note: this is the older SunSed markup language. I have since written it from scratch and made it look like C.

To be honest, I was surprised that it was possible, the following tag (function) takes the number of terms and determines the fibonacci series using recursion upto that term.

SunSed language is not very fast at calculating, looping and recursion, something I should work on in the future.

<s:tag name="fib">
    <s:case name="@case less">
        <s:if true="  2" return />
    </s:case>
    <s:if true="">
        
    </s:if>
    <s:if false="">
        <s:calc>
            <fib>
                <s:calc>
                      1
                </s:calc>
            </fib>
            
            <fib>
                <s:calc>
                      2
                </s:calc>
            </fib>
        </s:calc>
    </s:if>
</s:tag>

Now lets use our <fib> tag:

<fib>10</fib>

Result:

55    

For now, the maximum fib that you can calcualate using SunSed is fib of 18. That’s because recursion is very slow in SunSed language and SunSed platform has a 1 second response limit — forces all apps to respond in less than 1 second, otherwise you could calculate higher numbers of fibs.

fibonacci programming SunSed
December 25, 2017

Introducing Shortform HTML Tags

Discuss this on Hacker News

Update: I have decided to rename this project from SunDown to Shortform HTML Tags, because it better describes the project, and more importantly many people including the author of Sundown (one of the most popular Markdown engines) asked me to not use this name to avoid confusion between projects.

We have since changed SunSed into a full C like programming language

My goal with is to create a platform that makes it super easy for developers to create awesome websites without forcing them to learn new stuff (language, syntax, platform, framework and more). To achieve that I had to re-invent couple of wheels along the way. One of those wheels is Markdown.

You see, SunSed platfrom has its own programming language and its syntax is exactly like HTML (except you have tags like if and set and more that makes it a programming language). At first, I used Markdown as a preprocessor for SunSed language to make it easier for users to write commonly used tags like H1, P, IMG and more.

In this article I explain what was wrong with Markdown and why I finally had to create my own Markdown alternative.

Problem[0] = Too many implementations of Markdown”

Markdown is not standardized, there are too many variations. And I feel like Jeff Atwood and his friends abandoned the Common Mark project that tried to create a universal specification of Markdown. Even if they did, I don’t think that Github, Stackoverflow, Reddit and other major Markdown users would ever convert.

Problem[1] = Markdown is limited to a few HTML tags”

Markdown is limited to a few HTML tags! You cannot extend it to support new tags without adding more rules to its syntax and making it even more difficult to learn.

At SunSed, users can create their own HTML tags and it made a lot of sense to be able to abstrarct those tags just like HTML. After all, HTML is not very fun to write (it’s easy but not fun).

Problem[2] = Cannot safely mix Markdown with HTML

What do you think about the following code? Do you think that the nested Markdown works?

    <div>
        # Header 1
        <p>This is **bold**</p>
    </div>

The answer depends on the Markdown flavor and even if it supports it, you cannot always be sure that it works! Sometimes there are things that can be nested but also situations where it’s next to impossible for the compiler to detect edge cases that’s why in CommonMark they decided to completely remove this feature.

The most problematic syntax rule of Markdown that dose not allow you to easily mix it with HTML is that newlines create new paragraphs.

Remember, at SunSed I wanted users to be able to nest HTML, SunSed and Markdown in anyway they like.

Problem[3] = Markdown is slow!”

I could not believe it. A language with such a simple syntax was as slow as hell. Specially if you wanted the feature that allows you to mix HTML and Markdown. I’m talking about compiling 1,000 pages in less than 1 second fast.

I even tried the :s:a[https://github.com/vmg/sundown fastest implementation of Markdown] and it was still slower than SunSed language compiler and used more CPU and RAM than I liked.

Solution[0] = A good name”

First, I came up with SML (SunSed Markup Language) but eventually decided to go with Sundown” (SunSed Markdown) because it’s a cooler name that almost describes itself.

Update: But, after the feedback I got from HN discussions I decided to name this project: "Shortform HTML Tags".

Solution[1] = Shortform Tags should scale”

Unlike Markdown, I wanted the Shortform Tags to support all HTML tags (SunSed tags + Users custom HTML tags + HTMLs built-in tags).

Solution[2] = Easy to learn”

It must have very simple syntax (with just a few rules). My goal here was to be able to teach someone the entire syntax in one paragraph.

Solution[3] = It must be fast”

I believe that faster programs almost always have less code! How about a syntax that could be parsed with just a few Regular Expression search and replace? That would be awesome — no more complicated compiler.

Solution[4] = You should be able to mix HTML tags and the Shortform Tags”

Users should be able to nest HTML tags inside the Shortforms (and vice versa).

Result = Shortform HTML Tags v1”

I’m actually writing using Shortform Tags right now. I have been testing it for while and think that it’s super awesome!

Here is a picture of SunSed IDE right now:

SunSed IDE writing in Shortform HTML Tags

Syntax

foo and bar could be any HTML or SunSed tag:

HTML

<foo>
    Content <bar>inline<bar>.
</foo>

Shortfrom Tags

:foo Content :bar[inline].

That’s it, now you know the entire Shortfrom Tags syntax.

Examples

Any line that starts with a : is special.

Header 1

:h1 This is also header 1

Paragraph

:p This is also a paragraph.
: You can skip the P and still create a new paragraph.

Embedding YouTube video

Here we are using a SunSed s:youtube tag.

:s:youtube https://www.youtube.com/watch?v=dGSOaCY6D6s

You can easily create your own tags using SunSed language! https://www.sunsed.com/manual/creating-tag

My goal is to create thousadns of useful tags that abstract HTML+CSS+JavaScript and make front-end developers life easier.

markdown sundown sunsed short-form HTML tags
November 23, 2017

JavaScript Hoisting Explained

Hoisting in JavaScript means that variables in the current scope are defined ahead of time by compiler.

Lets work with this following code:

console.log(x); // x is undefined  (VALUE)
console.log(y); // y is undeclared (ERROR! Your program halts!)
var x = 3;      // x is now 3!

Programmers don’t have any problem with the concept of having to define a variable before trying to use it. But, why does JS Engine treat x and y differntly?

Undefined vs Undeclared

In most programming languages we would expect both console.log(x) and console.log(y) return an error saying these two variables are not defined. But, that’s not the case for the JavaScript.

To the JS Engine, undeclared is what it sounds like: something (in this case a variable) that is not declared in your code — something that does not exist in the world — and of course, it has no value. We don’t disagree with JS on why y should be undeclared. You would probably look at line #1 and think, good JS Engine, x is also not declared, YET. And the key of understanding hoisting is in that bold yet”.

JS is Compiled

OK, before you bite my words, I agree, we don’t compile JS to machine code like we do with something like Go, Chicken, Rust, C++ or our good old C. But, trust me, it really helps to know that JS Engine does not go over your code one line at a time! It goes through your entire source code several times (mostly to optimize your code) before it starts executing it line by line.

Now, I’m sure you can link the bold YET to this. On line #1 JS already has x declared. But wait a minute, if it exists, what is its value? undefined. undefined is not an error! It is a real value, like the number 1 or the string hello world. You can even set a variable to the value of undefined yourself, just like we are used to set variables to null in other programming languages.

Null vs Undefined

Null and Undefined are different values in JS but used for the same purpose — to declare a variable has no useful value! My guess is that the designer of JS first made Undefined and then decided to also add Null to make JS look more like Java.

Who Set our x to undefined

The JS engine, at the compile time! Why? It just want to allocate memory for variables. At one of the compile phases JS Engine goes through our entire source code and allocate memory for all of our declared variables and set their value to undefined. Remember, these values will be set in their scope. For our little example, everything is in the global scope.

Why not set it to 3?

I hear you, if it’s trying to be smart why not go the extra mile and set the variable to its real value?

Because although it might be able to figure out primitive values for some variables it cannot figure out all of them without executing your code. In the following example x value should be the return value from the function foo() and who knows what needs to execute inside that function in order to get its value!

    var y = 3;
    var x = foo();

What about function statements?

Well, we know that in JS functions are first class (just like variables you can pass them around) that’s why the idea of hoisting applies to them as well. Here is a simple example of how hoisting of functions work in JS, where we use a function before we define it.

foo(); // returns 3
function foo(){
    return 3;
}

Unlike variables, the compiler does not set the value of foo to undefined instead it points to the location where the function was defined. And a reference to the location of the function definition is all it takes for the runtime (when we execute the code line by line) to find and execute the function!

What about function expressions?

Well, unlike function statements, function expressions return some value and it is next to impossible for the compiler to cover all the edge cases of guessing if an expression is going to return a function or a primitive value. So, it just sets it to undefined like any other variable.

foo(); // Error: undefined is not a function!
var foo = function(){
    return 3;
};
JavaScript JS Hoisting Variables
November 7, 2016

Convert SRW to JPEG in Linux

SRW is Samsung raw format. You could setup your camera to save in this format.

In this post I show you how to convert it back to JPG or some other more popular format.

As it turned out ImageMagick convert does not recognize SRW. Maybe it can convert other Raw formats but not Samsungs you’ll get a similar error if you try ImageMagick:

convert: Invalid TIFF directory; tags are not sorted in ascending order. `TIFFReadDirectoryCheckOrder' @ warning/tiff.c/TIFFWarnings/861.
convert: TIFF directory is missing required "ImageLength" field. `MissingRequired' @ error/tiff.c/TIFFErrors/561.
convert: no images defined `29.jpg' @ error/convert.c/ConvertImageCommand/3210.

Install ufraw

Ufraw is a program that was designed to handle raw formats.

Install it on your Linux computer with your favorite package manager, I guess it’s old enough to be found in most distros repositories.

I just did man ufraw and was amazed at what it can do! It’s not just a convertor.

It can do much more than that but to simply convert your image to jpg you can open the image in ufraw and then save it use the GUI (yes it has a graphical user interface too).

You can also run the following command to save the file using command line, this is my preferred method for converting all my images at once.

But remember that you should also limit the output size or the size your jpg will be huge. Mine is about 60MB.

Here is the command:

ufraw-batch SAM_0029.SRW --shrink=4 --out-type=jpeg --output output.jpg

By using the above command my 60MB image output became 100KB (0.1MB) which is even web ready.

Anyway, now it’s time to convert all images at once. I have 100+ images in one directory. I don’t want to convert each one by one.

for n in *.SRW; do ufraw-batch $n  --shrink=4 --out-type=jpeg --output $n.jpg; done

CD (change directrory) to the folder that contains all the SRW images and then run the above command. It will take care of converting all images.

Also checkout man ufraw for a list of options and effects that it can apply to all images.

Linux Command Line