|
| 1 | +module Orgmode |
| 2 | + module Elements |
| 3 | + class Link |
| 4 | + attr_reader :url, :description, :document |
| 5 | + |
| 6 | + def initialize(document, url, description = nil) |
| 7 | + @document = document |
| 8 | + @url = expand(url, document.link_abbreviations) |
| 9 | + @description = description |
| 10 | + end |
| 11 | + |
| 12 | + def html_tag |
| 13 | + return image_tag if target_image? |
| 14 | + return target_tag unless document.targets.empty? |
| 15 | + |
| 16 | + "<a href=\"#{url}\">#{description || url}</a>" |
| 17 | + end |
| 18 | + |
| 19 | + private |
| 20 | + |
| 21 | + def expand(url, abbreviations) |
| 22 | + return url if abbreviations.nil? |
| 23 | + return url unless abbreviations.has_key?(url) |
| 24 | + |
| 25 | + abbreviations[url] |
| 26 | + end |
| 27 | + |
| 28 | + def description_img_tag |
| 29 | + "<img src=\"#{description}\" alt=\"#{description}\" />" |
| 30 | + end |
| 31 | + |
| 32 | + def find_target(targets = []) |
| 33 | + targets.find do |target| |
| 34 | + target[:content] == description || target[:content] == url |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + def image_file?(file) |
| 39 | + RegexpHelper.image_file.match(file) |
| 40 | + end |
| 41 | + |
| 42 | + def image_tag |
| 43 | + if !image_file?(url) |
| 44 | + "<a href=\"#{url}\">#{description_img_tag}</a>" |
| 45 | + elsif image_file? description |
| 46 | + description_img_tag |
| 47 | + elsif description.nil? |
| 48 | + "<img src=\"#{url}\" alt=\"#{url}\" />" |
| 49 | + else |
| 50 | + "<img src=\"#{url}\" alt=\"#{description}\" />" |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + def target_image? |
| 55 | + return @target_image unless @target_image.nil? |
| 56 | + |
| 57 | + @target_image = image_file?(url) || image_file?(description) || false |
| 58 | + end |
| 59 | + |
| 60 | + def target_tag |
| 61 | + target = find_target(document.targets) |
| 62 | + link = target ? "#tg.#{target[:index]}" : url |
| 63 | + |
| 64 | + "<a href=\"#{link}\">#{description || url}</a>" |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | +end |
0 commit comments