0

I have a csv file which I need to import on a php-based system - Magento to be specific. There is a field which contains the description of a product and is whitespace formatted for indentation as well as dashes (-) used for list items. I could use a <pre> tag and display it as it is, but when I got the csv file, somehow the lines which could fit in a single line had already moved to another line. So there is no real success trying to replace \r\n with <br/> (or using the nl2br function) because I do not want something that can fit in one line to be displayed in two lines.

I am not sure if this can be achieved with php, VBA routine in Excel or some Magento extension (I am using MAGMI at the moment and it does not have such facilities). So I ask this question with an open option of offering a solution with either of those.

Anyone has a tip for this please?

EDIT::

Should look like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

  • Praesent cursus eu eros quis laoreet.
  • In tincidunt massa sed dui aliquam placerat.

Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam a nulla id dui semper tristique. Vivamus rutrum auctor neque, eu tincidunt magna dapibus vitae. Integer felis mi, luctus ut mollis at, mollis nec lacus. Vestibulum et dictum turpis. Praesent in neque sed mauris semper hendrerit.

  • Vivamus rhoncus magna ipsum
  • sit amet ullamcorper lectus eleifend ut. Sed semper dui quis accumsan suscipit.
  • Donec eu lacus sed dolor fermentum fermentum.
  • Curabitur iaculis molestie ante a bibendum.

Which means:

  • Text should not contain a line break unless it is intended to have one.
  • Width of the text should depend on the container rather than whitespace formatting (somehow the text is narrow and does not extend even if I adjust the column width on csv)#
  • If possible, dashes should be replaced with a list item enclosure, but that is not the biggest problem at this moment.

Text currently looks like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. - Praesent cursus eu eros quis laoreet. - In tincidunt assa sed dui aliquam placerat. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam a nulla id dui semper tristique. Vivamus rutrum auctor neque, eu tincidunt magna dapibus vitae. Integer felis mi, luctus ut mollis at, mollis nec lacus. Vestibulum et dictum turpis. Praesent in neque sed mauris semper hendrerit. - Vivamus rhoncus magna ipsum - sit amet ullamcorper lectus eleifend ut. Sed semper dui quis accumsan suscipit. - Donec eu lacus sed dolor fermentum fermentum. - Curabitur iaculis molestie ante a bibendum.

*And if I use <pre> tag, it looks like this *

Lorem ipsum dolor sit amet, 
consectetur adipiscing elit. 
- Praesent cursus eu eros 
quis laoreet. 
- In tincidunt massa sed dui 
aliquam placerat. 

Interdum et malesuada fames 
ac ante ipsum primis in faucibus. 
Aliquam a nulla id dui semper 
tristique. Vivamus rutrum auctor 
neque, eu tincidunt magna 
dapibus vitae. Integer felis mi, 
luctus ut mollis at, mollis nec 
lacus. Vestibulum et dictum turpis. 
Praesent in neque sed mauris 
semper hendrerit. 

- Vivamus rhoncus magna ipsum 
- sit amet ullamcorper lectus 
eleifend ut. Sed semper dui 
quis accumsan suscipit. 
- Donec eu lacus sed dolor 
fermentum fermentum. 
- Curabitur iaculis molestie ante 
a bibendum.

So my product description looks either as a single unformatted block (which I understand because there is no html tags in there) or if I try preserving the whitespace formatting, it looks too narrow (does not cover the whole width of the container).

Hope this is detailed enough.

P.S.::

Its just like how a ckeditor would allow users to type in text, press enter and at the back of it, ckeditor would replace them with paragraph tags etc. May be this operates on the back of a regex that can be referred to? Anyone know about that?

Community
  • 1
  • 1
zarun
  • 910
  • 9
  • 26
  • Post an example of the data you are importing, and also an exact example of how you want it to appear in Magento. Including a sample CSV file will also be beneficial. – Axel Mar 12 '14 at 17:03
  • @Axel I have added some details. Hope its clear enough now. – zarun Mar 13 '14 at 10:40
  • Why not just format your CSV descriptions using HTML? – Axel Mar 13 '14 at 20:00
  • I have 3000 products with an average of about 500 words of description per product. So that's out of the question. – zarun Mar 14 '14 at 02:36
  • Plus unfortunately, that is what I am asking if it is possible to format the existing whitespace formatted description using html tags instead. Just that I don't want to do it manually and be old and still not finish it :D – zarun Mar 14 '14 at 02:48

2 Answers2

1

What you're looking for is a Markdown to HTML converter. Magmi doesn't have a plugin that can do this, however you could easily write one yourself.

  1. Create a Magmi plugin folder and file.
  2. Include a Markdown to HTML PHP library, such as this: https://github.com/michelf/php-markdown
  3. Use the processItemBeforeId() function to update the descriptions on the fly in the plugin, like this:

    public function processItemBeforeId(&$item,$params=null)
    {
        //Use the Markdown class library to convert the description.
        $updated_description = Markdown::defaultTransform($item['description']);
    
        //Update the description of the item before it gets imported.
        $item['description'] = $updated_description;
    }
    
  4. Enable your custom Magmi plugin, and it should start converting the descriptions on the fly when you import.
Axel
  • 10,732
  • 2
  • 30
  • 43
  • I think this gets me a lot closer. I will wait for some other answers and if not, will mark this as correct. – zarun Mar 18 '14 at 12:53
  • Is there an easy way to figure out how to write extensions for MAGMI? I tried a bit, but now my MAGMI is stuck on the import page (no errors). – zarun Mar 18 '14 at 13:35
  • The easiest way is to copy an existing Magmi plugin and taking it apart/see how it works. They're very basic PHP files that contain specific function which Magmi calls throughout the import process. – Axel Mar 18 '14 at 17:23
  • I have already a running plugin that just has all the required functions. But, when I use ´$updated_description = Markdown::defaultTransform($item['description']);´, I get no error, no progress and am just stuck. Is this a wrong way to call the function inside a class? I also tried creating an object of Markdown class - but no success. – zarun Mar 19 '14 at 12:01
  • 1
    You can use the function `$this->log('text or variable to output, 'info');` which will output log information to the log window in Magmi. Try outputting your results into the log function. – Axel Mar 19 '14 at 14:57
  • Since the code was written for a defined namespace, I had to use Michelf\Markdown::defaultTransform($item['description']); to actually use the function. Thank you for your tips Axel. – zarun Mar 20 '14 at 13:00
  • Glad you figured it out. Happy to help :) – Axel Mar 20 '14 at 16:15
0

Have you try to add html tags using basic regular expression replacement ? (Using preg_replace to catch and replace list elements by their appropriate tags for example)

Saltan
  • 11
  • 4
  • Yes. I have tried that. Anything that's ending at two \r\n is the end of a paragraph. That way, I can get it to behave a little. Anything between `-` and `\r\n` could be a list item too. But still, it is not really effective because every description has some new whitespace formatting. – zarun Mar 14 '14 at 02:43