Instantly share code, notes, and snippets.

Embed
What would you like to do?
SearchLink creates Markdown links from automatic searches based on special syntax.
@n8henrie

This comment has been minimized.

n8henrie commented Aug 9, 2013

Just tweeted, but I supposed GitHub is a more appropriate place:
Any way to make the link 163 input.gsub!(/\[(.*?)\]\((\!.+?)\)/) do |match| behave properly in the following scenario:

[link](http://notsearchlinked.com) followed by [searchlinkme](!g)

Currently it hits the first link's opening [ and the .*? matches all the way until the second link's ](!g).

@n8henrie

This comment has been minimized.

n8henrie commented Aug 9, 2013

Fixed it. The ! in the regex was breaking the ability to non-greedy match non-searchlink Markdown urls. By taking the ! out of the regex you match all urls, then only process ones where the first character of the link is !.

My suggested fix for lines 163-232:

input.gsub!(/\[(.*?)\]\((.+?)\)/) do |match|
    link_text = $1
    link_info = $2
    search_type = ''
    search_terms = ''
    if link_info[0,1] == '!'
            if link_info =~ /\!(.+) "(.*?)"$/
                search_type = $1
                search_terms = $2
            else
                search_word = link_info.match(/\!(.+)/)
                search_type = search_word[1] unless search_word.nil?
                search_terms = link_text
            end

            url = false
            title = false

            case search_type
            when /^a$/
                az_url, title = google("site:amazon.com #{search_terms}", false)
                url, title = amazon_affiliatize(az_url, amazon_partner)

            when /^g$/ # google lucky search
                url, title = google(search_terms)

            when /^wiki$/
                url, title = wiki(search_terms)

            when /^def$/ # wikipedia/dictionary search
                # title, definition, definition_link, wiki_link = zero_click(search_terms)
                # if search_type == 'def' && definition_link != ''
                #   url = definition_link
                #   title = definition.gsub(/'+/,"'")
                # elsif wiki_link != ''
                #   url = wiki_link
                #   title = "Wikipedia: #{title}"
                # end
                url, title = google("define " + search_terms, true)

            when /^masd?$/ # Mac App Store search (mas = itunes link, masd = developer link)
                dev = search_type =~ /d$/
                url, title = itunes('macSoftware',search_terms, dev, itunes_affiliate)

            when /^itud?$/ # iTunes app search
                dev = search_type =~ /d$/
                url, title = itunes('iPadSoftware',search_terms, dev, itunes_affiliate)

            when /^s$/ # software search (google)
                url, title = google("(software OR app OR mac) #{search_terms}")
                link_text = title if link_text == ''

            when /^isong$/ # iTunes Song Search
                url, title = itunes('song', search_terms, false)

            when /^iart$/ # iTunes Artist Search
                url, title = itunes('musicArtist', search_terms, false)

            when /^ialb$/ # iTunes Album Search
                url, title = itunes('album', search_terms, false)

            when /^lsong$/ # Last.fm Song Search
                url, title = lastfm('track', search_terms)

            when /^lart$/ # Last.fm Artist Search
                url, title = lastfm('artist', search_terms)
            else
                if search_type =~ /.+?\.\w{2,4}$/
                    url, title = google("site:#{search_type} #{search_terms}")
                end
            end
        end
@jdkram

This comment has been minimized.

jdkram commented Jan 17, 2014

Thanks for this.

I was looking up Amazon affiliate links and different sources claim different information is required in the affiliate URL. This article makes a claim for much shorter links which only include the affiliate tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment