benfoster / MigraDoc.Extensions

Extensions for MigraDoc/PDFSharp
MIT License
28 stars 18 forks source link

Block elements inside list items throws exception #3

Open benfoster opened 11 years ago

BenLewies commented 10 years ago

Good day

I have a similar issue which I think relates to this item. The following markdown causes an exception:

1. Numbered item
<br/><br/>
1. Another numbered item:<br/>
#Case number# Crim123. Occurred on 2014/04/17. Dope.
<br/>
1. Yet another numbered item

Exception details as follows: System.InvalidCastException was caught _HResult=-2147467262 _message=Unable to cast object of type 'MigraDoc.DocumentObjectModel.Paragraph' to type 'MigraDoc.DocumentObjectModel.Section'. HResult=-2147467262 IsTransient=false Message=Unable to cast object of type 'MigraDoc.DocumentObjectModel.Paragraph' to type 'MigraDoc.DocumentObjectModel.Section'. Source=MigraDoc.Extensions.Html StackTrace: at MigraDoc.Extensions.Html.HtmlConverter.bb(HtmlNode node, DocumentObject parent) in c:\Users\MyName\Dropbox\MyCompany\Active Projects\MySiteOnline\MySiteOnline\MigraDoc.Extensions\src\MigraDoc.Extensions.Html\HtmlConverter.cs:line 122 at MigraDoc.Extensions.Html.HtmlConverter.ConvertHtmlNodes(HtmlNodeCollection nodes, DocumentObject section, DocumentObject current) in c:\Users\MyName\Dropbox\MyCompany\Active Projects\MySiteOnline\MySiteOnline\MigraDoc.Extensions\src\MigraDoc.Extensions.Html\HtmlConverter.cs:line 58 at MigraDoc.Extensions.Html.HtmlConverter.ConvertHtmlNodes(HtmlNodeCollection nodes, DocumentObject section, DocumentObject current) in c:\Users\MyName\Dropbox\MyCompany\Active Projects\MySiteOnline\MySiteOnline\MigraDoc.Extensions\src\MigraDoc.Extensions.Html\HtmlConverter.cs:line 69 at MigraDoc.Extensions.Html.HtmlConverter.ConvertHtmlNodes(HtmlNodeCollection nodes, DocumentObject section, DocumentObject current) in c:\Users\MyName\Dropbox\MyCompany\Active Projects\MySiteOnline\MySiteOnline\MigraDoc.Extensions\src\MigraDoc.Extensions.Html\HtmlConverter.cs:line 62 at MigraDoc.Extensions.Html.HtmlConverter.ConvertHtmlNodes(HtmlNodeCollection nodes, DocumentObject section, DocumentObject current) in c:\Users\MyName\Dropbox\MyCompany\Active Projects\MySiteOnline\MySiteOnline\MigraDoc.Extensions\src\MigraDoc.Extensions.Html\HtmlConverter.cs:line 69 at MigraDoc.Extensions.Html.HtmlConverter.ConvertHtml(String html, Section section) in c:\Users\MyName\Dropbox\MyCompany\Active Projects\MySiteOnline\MySiteOnline\MigraDoc.Extensions\src\MigraDoc.Extensions.Html\HtmlConverter.cs:line 47 at MigraDoc.Extensions.Html.HtmlConverter.<>cDisplayClass1.b__0(Section section) in c:\Users\MyName\Dropbox\MyCompany\Active Projects\MySiteOnline\MySiteOnline\MigraDoc.Extensions\src\MigraDoc.Extensions.Html\HtmlConverter.cs:line 30 at MigraDoc.Extensions.SectionExtensions.Add(Section section, String contents, IConverter converter) in c:\Users\MyName\Dropbox\MyCompany\Active Projects\MySiteOnline\MySiteOnline\MigraDoc.Extensions\src\MigraDoc.Extensions\SectionExtensions.cs:line 20 at MigraDoc.Extensions.Markdown.SectionExtensions.AddMarkdown(Section section, String markdown) in c:\Users\MyName\Dropbox\MyCompany\Active Projects\MySiteOnline\MySiteOnline\MigraDoc.Extensions\src\MigraDoc.Extensions.Markdown\SectionExtensions.cs:line 11 at MySiteOnline.Reporting.MotivationHelper.DefineSummary(Document document, Client client, Motivation motivation) in c:\Users\MyName\Desktop\MySiteOnline\MySiteOnline\MySiteOnline_Reporting\MotivationHelper.cs:line 496 at MySiteOnline.Reporting.MotivationHelper.CreateDocument(Client client, Motivation motivation) in c:\Users\MyName\Desktop\MySiteOnline\MySiteOnline\MySiteOnline_Reporting\MotivationHelper.cs:line 147 at MySiteOnline.Reporting.MotivationHelper.CreateMotivationDocument(Client client, Motivation motivation, String path) in c:\Users\MyName\Desktop\MySiteOnline\MySiteOnline\MySiteOnline_Reporting\MotivationHelper.cs:line 52 InnerException:

BenLewies commented 10 years ago

@benfoster Just want to find out if there will be a fix for this soon. I have been holding out for a fix on this issue as I like really this library - this is really good stuff! However, I can't wait much longer (this is holding back delivery of a project on my side).

If not, it's 100% OK (I understand we are all busy and this work is basically charity)... But still I thought it best to ask before implementing an alternative solution.

BenLewies commented 10 years ago

@benfoster Hi Ben

Going over the stacktrace, I suspect the root cause of the null reference exception might be found at Migradoc.Extensions.Html.HtmlConverter in the ConvertHtml method. I am not sure how to fix it, though.

Regards, Ben

benfoster commented 10 years ago

Hi Ben,

The issue is not actually caused by line breaks as I'd originally thought. The way in which we convert <hX>,

and <li> tags to to the Migradoc object model is to render a paragraph with a specific style. This relies on the parent node in the model being a Section. Unfortunately when a list item contains other paragraphs or headings, the converter attempts to add another paragraph to the list item paragraph. Since nested paragraphs are not supported by Migradoc we get an exception.

To fix I think we'll need to render any container elements like list items as sections but this requires further thought. I'll hopefully get time to look into this in the next few weeks.

BenLewies commented 10 years ago

Thanks for the feedback. I'll give it some thought too.

BenLewies commented 10 years ago

Hi Ben

So, I see there is actually a pending pull request which partly solves / provides a workaround for this problem. I downloaded and compiled it, and it seems to work, at least for nested

  • items. In my own solution I just suppress any header elements which might appear within a list.

    https://github.com/benfoster/MigraDoc.Extensions/pull/5

    I hope this can be of some help to you.

    Cheers

    [UPDATE] Nested lists does not work 100% as expected since bulleted list items spanning multiple lines are not properly indented - the text of the second and third lines etc continues right underneath the bullet.