-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ANSI to UTF8 conversion #2
base: master
Are you sure you want to change the base?
Changes from all commits
6643c70
e24c638
14e7378
9653978
c46c592
d1190df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
require 'fastercsv' | ||
require 'tempfile' | ||
require 'iconv' | ||
|
||
class MultipleIssuesForUniqueValue < Exception | ||
end | ||
|
@@ -42,6 +43,15 @@ def match | |
i = 0 | ||
@samples = [] | ||
|
||
# transcode the ANSI format to UTF8 using iconv | ||
if (iip.encoding == "A" ) | ||
ansi2utf8!(iip) | ||
end | ||
|
||
# preparing the import | ||
chomp_separators!(iip) | ||
sub_blanks!(iip) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here is the call to the function removing the nasty blanks in the csv_data |
||
|
||
FasterCSV.new(iip.csv_data, {:headers=>true, | ||
:encoding=>iip.encoding, :quote_char=>iip.quote_char, :col_sep=>iip.col_sep}).each do |row| | ||
@samples[i] = row | ||
|
@@ -124,6 +134,8 @@ def result | |
return | ||
end | ||
|
||
|
||
|
||
default_tracker = params[:default_tracker] | ||
update_issue = params[:update_issue] | ||
unique_field = params[:unique_field].empty? ? nil : params[:unique_field] | ||
|
@@ -158,6 +170,15 @@ def result | |
flash[:error] = unique_error | ||
return | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Below you'll find the same code as before, seems like csv_data needed to be reprocessed here. Haven't investigated why, but maybe the processsing can be made only one time. |
||
# transcode the ANSI format to UTF8 using iconv | ||
if (iip.encoding == "A" ) | ||
ansi2utf8!(iip) | ||
end | ||
|
||
# preparing the import | ||
chomp_separators!(iip) | ||
sub_blanks!(iip) | ||
|
||
FasterCSV.new(iip.csv_data, {:headers=>true, :encoding=>iip.encoding, | ||
:quote_char=>iip.quote_char, :col_sep=>iip.col_sep}).each do |row| | ||
|
@@ -362,6 +383,44 @@ def result | |
end | ||
|
||
private | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Below this line you'll find the code of the methods called previously |
||
# converts the ImportInProgrsse csv data from ansi to utf8 | ||
# @param ImportInProgress importip, the ImportInProgress to be transcoded from ANSI to UTF8 | ||
def ansi2utf8!(importip) | ||
converter = Iconv.new('UTF-8','CP1252') | ||
importip.csv_data = converter.iconv(importip.csv_data) | ||
#changing the encoding tag : U for UTF8 | ||
importip.encoding = "U" | ||
end | ||
|
||
|
||
# deletes a repetition of CSV separators at the end of the file (eg. "last_field,,,\n" becomes "last_field\n") | ||
# @param ImportInProgress importip whose csv_data will be processed | ||
def chomp_separators!(importip) | ||
separator=importip.col_sep | ||
csv_data_chomped="" | ||
importip.csv_data.each_line{ |line| | ||
line.chomp! | ||
line=~/^ *(.*?)((#{separator})*)$/ | ||
csv_data_chomped+=$1+"\n" | ||
} | ||
importip.csv_data=csv_data_chomped | ||
end | ||
|
||
#deletes spaces around separators and in the start of line in csv_data (prevents parsing failure) | ||
# @param ImportInProgress importip whose csv_data will be cleaned from unwanted spaces | ||
def sub_blanks!(importip) | ||
separator=importip.col_sep | ||
csv_data_subed="" | ||
importip.csv_data.each_line{ |line| | ||
subed_line=line.gsub(/ *#{separator} */,separator) | ||
csv_data_subed += subed_line | ||
} | ||
importip.csv_data=csv_data_subed | ||
|
||
end | ||
|
||
|
||
|
||
def find_project | ||
@project = Project.find(params[:project_id]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Subject;Description;Assigned to | ||
t�che 1;cr�er objet;admin |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Subject;Description;Assigned to | ||
tache 1;creer objet;admin |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Subject;Description;Assigned to | ||
tâche 1;créer objet;admin |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Subject;Description;Assigned to | ||
tache 1;creer objet;admin |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Subject;Description;Assigned to | ||
tâche 1;créer objet;admin |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Subject;Description;Assigned to | ||
tache 1;creer objet;admin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the call to the function removing the irrelevant separators in the csv_data