old_contents: """
  Image Tag
  ---------
  This implements a Liquid-style image tag for Pelican,
  based on the octopress image tag [1]_
  
  Syntax
  ------
  {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | "title text" ["alt text"]] %}
  
  Examples
  --------
  {% img /images/ninja.png Ninja Attack! %}
  {% img left half http://site.com/images/ninja.png Ninja Attack! %}
  {% img left half http://site.com/images/ninja.png 150 150 "Ninja Attack!" "Ninja in attack posture" %}
  
  Output
  ------
  <img src="/images/ninja.png">
  <img class="left half" src="http://site.com/images/ninja.png" title="Ninja Attack!" alt="Ninja Attack!">
  <img class="left half" src="http://site.com/images/ninja.png" width="150" height="150" title="Ninja Attack!" alt="Ninja in attack posture">
  
  [1] https://github.com/imathis/octopress/blob/master/plugins/image_tag.rb
  """
  import re
  from mdx_liquid_tags import LiquidTags
  
  SYNTAX = '{% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | "title text" ["alt text"]] %}'
  
  # Regular expression to match the entire syntax
  ReImg = re.compile("""(?P<class>\S.*\s+)?(?P<src>(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?P<width>\d+))?(?:\s+(?P<height>\d+))?(?P<title>\s+.+)?""")
  
  # Regular expression to split the title and alt text
  ReTitleAlt = re.compile("""(?:"|')(?P<title>[^"']+)?(?:"|')\s+(?:"|')(?P<alt>[^"']+)?(?:"|')""")
  
  
  @LiquidTags.register('img')
  def img(preprocessor, tag, markup):
      attrs = None
  
      # Parse the markup string
      match = ReImg.search(markup)
      if match:
          attrs = dict([(key, val.strip())
                        for (key, val) in match.groupdict().iteritems() if val])
      else:
          raise ValueError('Error processing input. '
                           'Expected syntax: {0}'.format(SYNTAX))
  
      # Check if alt text is present -- if so, split it from title
      if 'title' in attrs:
          match = ReTitleAlt.search(attrs['title'])
          if match:
              attrs.update(match.groupdict())
          if not attrs.get('alt'):
              attrs['alt'] = attrs['title']
  
      # Return the formatted text
      return '<a href="{1}" href="_new"><img {0}></a>'.format(' '.join('{0}="{1}"'.format(key, val)
                                         for (key, val) in attrs.iteritems()), attrs['src'])
  
  #----------------------------------------------------------------------
  # This import allows image tag to be a Pelican plugin
  from liquid_tags import register
  
new_contents: """
  Image Tag
  ---------
  This implements a Liquid-style image tag for Pelican,
  based on the octopress image tag [1]_
  
  Syntax
  ------
  {% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | "title text" ["alt text"]] %}
  
  Examples
  --------
  {% img /images/ninja.png Ninja Attack! %}
  {% img left half http://site.com/images/ninja.png Ninja Attack! %}
  {% img left half http://site.com/images/ninja.png 150 150 "Ninja Attack!" "Ninja in attack posture" %}
  
  Output
  ------
  <img src="/images/ninja.png">
  <img class="left half" src="http://site.com/images/ninja.png" title="Ninja Attack!" alt="Ninja Attack!">
  <img class="left half" src="http://site.com/images/ninja.png" width="150" height="150" title="Ninja Attack!" alt="Ninja in attack posture">
  
  [1] https://github.com/imathis/octopress/blob/master/plugins/image_tag.rb
  """
  import re
  from mdx_liquid_tags import LiquidTags
  
  SYNTAX = '{% img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | "title text" ["alt text"]] %}'
  
  # Regular expression to match the entire syntax
  ReImg = re.compile("""(?P<class>\S.*\s+)?(?P<src>(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?P<width>\d+))?(?:\s+(?P<height>\d+))?(?P<title>\s+.+)?""")
  
  # Regular expression to split the title and alt text
  ReTitleAlt = re.compile("""(?:"|')(?P<title>[^"']+)?(?:"|')\s+(?:"|')(?P<alt>[^"']+)?(?:"|')""")
  
  
  @LiquidTags.register('img')
  def img(preprocessor, tag, markup):
      attrs = None
  
      # Parse the markup string
      match = ReImg.search(markup)
      if match:
          attrs = dict([(key, val.strip())
                        for (key, val) in match.groupdict().iteritems() if val])
      else:
          raise ValueError('Error processing input. '
                           'Expected syntax: {0}'.format(SYNTAX))
  
      settings = preprocessor.configs.config['settings']
      siteurl =  settings.get('SITEURL', '')
      # TODO fix the logic here. Assume's we're using an image in /content/
      attrs['src'] = siteurl + attrs['src']
      # Check if alt text is present -- if so, split it from title
      if 'title' in attrs:
          match = ReTitleAlt.search(attrs['title'])
          if match:
              attrs.update(match.groupdict())
          if not attrs.get('alt'):
              attrs['alt'] = attrs['title']
  
      # Return the formatted text
      return '<a href="{1}" href="_new"><img {0}></a>'.format(' '.join('{0}="{1}"'.format(key, val)
                                         for (key, val) in attrs.iteritems()), attrs['src'])
  
  #----------------------------------------------------------------------
  # This import allows image tag to be a Pelican plugin
  from liquid_tags import register
  
subject: fix for when images on prod are under a sub directory i.e. /blog