# Translating Figure markup


Let's deal with figure references first
# when we encounter a reference to a figure inside the html
# we replace it with a ConTeXt reference

(@article/"a").each do |a|
a.swap("\\in[#{a.inner_html}]")
end



I have used the "alt" attribute inside the HTML element "img" to carry some ConTeXt figure setup directives such as width and position. To retrieve these I pass on the different string elements within "alt" into an array. The "alt" attribute is also used to carry an optional "keyword" for the image. This is useful for referencing figures.


# replace <p><img> by equivalent command in context
(@article/"p/img").each do |img|

img_attrs=img.attributes['alt'].split(",")




# ConTeXt can figure out the best image format. So we remove the file extension for images
# I have to take care of file names that have a "." embedded in them, so I reverse
# the order of the string before operating on it. After I filter, I reverse it again.

img_src=img.attributes['src'].reverse.sub(/\w+\./,"").reverse



# see if position of figure is indicated
img_pos="force"
img_attrs.each do |arr|
img_pos=arr.gsub("position=","") if arr.match("position=")
end
img_attrs.delete("position=#{img_pos}") unless img_pos=="force"



# see if the array img_attrs contains an referral key word
if img_attrs.first.match(/\w+[=]\w+/)
img_id=" "
else
img_id=img_attrs.shift
end



if img_pos=="force"
if img.attributes['title']
img.swap("
\\placefigure\n
[#{img_pos}][#{img_id}] \n
{#{img.attributes['title']}} \n
{\\externalfigure[#{img_src}][#{img_attrs.join(",")}]}  \n
")
else
img.swap("
\\placefigure\n
[#{img_pos}] \n
{none} \n
{\\externalfigure[#{img_src}][#{img_attrs.join(",")}]}
")
end
end

end # end of converting inside (@article/"p/img")