From 936380e660ec0a4b94f3e2e0abc2e2dc982f2c23 Mon Sep 17 00:00:00 2001 From: Aidin Gharibnavaz Date: Fri, 19 Feb 2016 08:42:13 +0330 Subject: [PATCH] HTML Dumper. --- dumpers/html/dumper.rb | 113 +++++++++++++++++++++++++++ dumpers/html/template | 168 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 281 insertions(+) create mode 100644 dumpers/html/dumper.rb create mode 100644 dumpers/html/template diff --git a/dumpers/html/dumper.rb b/dumpers/html/dumper.rb new file mode 100644 index 0000000..92103ed --- /dev/null +++ b/dumpers/html/dumper.rb @@ -0,0 +1,113 @@ +require "pathname" +require_relative '../dumper_interface' + +MAIN_TEMPLATE = File.read(File.join(File.dirname(__FILE__), 'template')) + +LEFT_BUBBLE_TEMPLATE = +''' +
+ %s %s
+ %s +
+''' + +RIGHT_BUBBLE_TEMPLATE = +''' +
+ %s %s
+ %s +
+''' + +SERVICE_TEMPLATE = '

%s

' + +IMAGE_TEMPLATE = '

%s' + +RTL_TEMPLATE = '

%s

' + +# Characters of RTL languages. +RTL_CHARS = ['ا', 'آ', 'ب', 'پ', 'ت', 'ث', 'ج', 'چ', 'ح', 'خ', 'د', 'ذ', 'ر', 'ز', 'ژ', 'س', 'ش', 'ص', 'ض', 'ط', 'ظ', 'ع', 'غ', 'ف', 'ق', 'ک', 'گ', 'ل', 'م', 'ن', 'و', 'ه', 'ی'] + +class Dumper < DumperInterface + + def start_dialog(dialog) + @content = "" + @previous_name = "" + @use_right_bubble = false + end + + def dump_msg(dialog, msg) + if msg['event'] == 'message' + + if msg['service'] == false + # Getting sent time. + message_time = msg['from']['when'] + if not message_time + message_time = msg['to']['when'] + end + + # Alternating left/right bubbles. + if msg['from']['print_name'] != @previous_name + @use_right_bubble = !@use_right_bubble + end + @previous_name = msg['from']['print_name'] + + # Dumping text message. + if msg['text'] + + message = msg['text'].gsub(">", ">").gsub("<", "<").gsub("\n", "
") + if message.start_with?(*RTL_CHARS) + message = sprintf(RTL_TEMPLATE, message) + end + add_text(message_time, msg['from']['print_name'], message, @use_right_bubble) + end + + # Dumping medias. + if msg['media'] + if msg['media']['type'] == 'photo' + add_image(message_time, msg['from']['print_name'], msg['media']['caption'], msg['media']['file'], @use_right_bubble) + end + end + + end + end + end + + def end_dialog(dialog) + output_file = File.join(get_backup_dir, get_safe_name(dialog['print_name']) + '.html') + + File.open(output_file, 'w') {|f| f.write(sprintf(MAIN_TEMPLATE, dialog['print_name'], @content))} + + @content = '' + end + + def add_text(time, title, text, use_right_bubble) + # Messages will be passed to the dumper, from newest to oldest. + # I wanted them in the reverse order: oldest to newest. + # That's why I inserted it at the beginning. + + if use_right_bubble + template = RIGHT_BUBBLE_TEMPLATE + else + template = LEFT_BUBBLE_TEMPLATE + end + + @content = sprintf(template, time, title, text) + @content + end + + def add_image(time, title, text, file_path, use_right_bubble) + if use_right_bubble + template = RIGHT_BUBBLE_TEMPLATE + else + template = LEFT_BUBBLE_TEMPLATE + end + + # Getting path of the image file, relative to the output_dir. + relative_path = Pathname.new(file_path).relative_path_from(Pathname.new(get_backup_dir)).to_s + inner_tag = sprintf(IMAGE_TEMPLATE, relative_path, text) + + @content = sprintf(template, time, title, inner_tag) + @content + end + +end + diff --git a/dumpers/html/template b/dumpers/html/template new file mode 100644 index 0000000..2c6a51c --- /dev/null +++ b/dumpers/html/template @@ -0,0 +1,168 @@ + + + + + + %s + + + + +
+ %s +
+ +