Skip to content

Commit

Permalink
Updated french char handling, lang in filename
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasuhiro Asaka committed Jul 12, 2012
1 parent 898a31e commit 12412d6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
12 changes: 8 additions & 4 deletions lib/ydocx/parser.rb
Expand Up @@ -55,7 +55,7 @@ def apply_fonts(rpr, text)
if symbol
_text = ''
text.unpack('U*').each do |char|
_text << optional_replace(char.to_s(16))
_text << character_replace(char.to_s(16))
end
text = _text
end
Expand All @@ -76,14 +76,14 @@ def apply_align(rpr, text)
end
text
end
def optional_escape(text)
def character_encode(text)
text.force_encoding('utf-8')
# NOTE
# :named only for escape at Builder
text = @coder.encode(text, :named)
text
end
def optional_replace(code)
def character_replace(code)
code = '0x' + code
# NOTE
# replace with rsemble html character ref
Expand Down Expand Up @@ -143,6 +143,9 @@ def optional_replace(code)
#p "char : " + @coder.decode("&#%s;" % code.hex.to_s)
end
end
def optional_escape(text)
text
end
def parse_block(node)
nil # default no block element
end
Expand Down Expand Up @@ -230,7 +233,7 @@ def parse_paragraph(node)
end
unless r.xpath('w:sym').empty?
code = r.xpath('w:sym').first['char'].downcase # w:char
content << optional_replace(code)
content << character_replace(code)
pos += 1
end
if !r.xpath('w:pict').empty? or !r.xpath('w:drawing').empty?
Expand Down Expand Up @@ -276,6 +279,7 @@ def parse_table(node)
end
def parse_text(r, lstrip=false)
text = r.xpath('w:t').map(&:text).join('')
text = character_encode(text)
text = optional_escape(text)
text = text.lstrip if lstrip
if rpr = r.xpath('w:rPr')
Expand Down
11 changes: 6 additions & 5 deletions lib/ydocx/templates/fachinfo.rb
Expand Up @@ -6,6 +6,7 @@
module YDocx
class Parser
attr_accessor :code, :lang
@@figure_pattern = /&lsquo;|&rsquo;|&apos;|&acute;/
def init
@image_path = 'image'
@code = nil
Expand Down Expand Up @@ -93,7 +94,7 @@ def escape_id(text)
gsub(/\s*\/\s*|\s+|\/|\-/, '_').gsub(/\./, '').downcase)
end
def parse_code(text) # swissmedic number
if text.gsub(/&lsquo;|&rsquo;|&apos;|&acute;/, '') =~
if text.gsub(@@figure_pattern, '') =~
/^\s*(\d{5})(.*|\s*)\s*\(\s*Swiss\s*medic\s*\)(\s*|.)$/iu
@code = "%5d" % $1
else
Expand All @@ -116,21 +117,20 @@ def parse_title(node, text)
end
def parse_block(node)
text = node.inner_text.strip
text = optional_escape text
text = character_encode(text)
chapters.each_pair do |chapter, regexp|
if text =~ regexp
# allow without line break
# next if !node.previous.inner_text.empty? and !node.next.inner_text.empty?
id = escape_id(chapter)
@indecies << {:text => chapter, :id => id}
return parse_heading(text, id)
elsif parse_code(text)
return nil
end
end
if title = parse_title(node, text)
return title
end
parse_code(text)
return nil
end
end
Expand Down Expand Up @@ -256,9 +256,10 @@ def output_directory
@files
end
def output_file(ext) # html
lang = (@parser.lang.downcase == 'fr' ? 'fr' : 'de')
if @parser.code
filename = @parser.code
output_directory.join "#{filename}.#{ext.to_s}"
output_directory.join "#{lang}_#{filename}.#{ext.to_s}"
else # default
@path.sub_ext(".#{ext.to_s}")
end
Expand Down

0 comments on commit 12412d6

Please sign in to comment.