0

I am using Ben Foster's Migradoc extensions to format a PDF document using Markdown syntax.

I am running into an issue when using headers or sub-lists (<hx> or <li> elements) within a list (a null reference exception is thrown). The issue is detailed here.

The root cause of the problem is that Migradoc does not support nested paragraphs.

Are there any possible workarounds to this issue?

CShark
  • 2,183
  • 1
  • 24
  • 42
  • @Okuma.Scott Unfortunately, this is based on user input & I can not limit users in this regard - they expect "normal" markdown capabilities to work as expected. If I knew how to fix the issue, I would be happy to contribute a fix to the project. Unfortunately, I don't. And as I have a deadline to hit, I have to find a temporary workaround to the issue. – CShark Aug 11 '14 at 11:54

2 Answers2

0

You ask "Are there any possible workarounds to this issue?"

MigraDoc is able to create PDF and RTF. Does RTF (Word) support nested paragraphs?
Probably not. I think this is not a MigraDoc issue.

Nested lists are possible in MigraDoc, but may require changes in the extensions. IIRC there are limitations with respect to nesting when numbered lists are involved.

IMHO nested paragraphs do not make sense. MigraDoc supports AddFormattedText that allows to use different formats in a single paragraph. This may require changes to the extensions and/or the input given to the extensions.

0

Hey I've been using Ben Foster's Migradoc extensions as well and had this same problem. This may not be perfect, but it worked well enough for me... Modify your HtmlConverter.cs and do the following:

First, add a global variable:

private int _nestedListLevel;

Next, add 2 new node handlers to the AddDefaultNodeHandlers() method:

nodeHandlers.Add("ul", (node, parent) =>
{
    if (parent is Paragraph)
    {
        _nestedListLevel++;
        return parent.Section;
    }

    _nestedListLevel = 0;
    return parent;
});

nodeHandlers.Add("ol", (node, parent) =>
{
    if (parent is Paragraph)
    {
        _nestedListLevel++;
        return parent.Section;
    }

    _nestedListLevel = 0;
    return parent;
});

Finally, change the "li" node handler to the following... NOTE, this removes some of the styling work that he did, but it made things less complicated for me and works just fine.. you can re-add that stuff if you want.

nodeHandlers.Add("li", (node, parent) =>
{
    var listStyle = node.ParentNode.Name == "ul"
        ? "UnorderedList"
        : "OrderedList";

    var section = (Section)parent;
    var isFirst = node.ParentNode.Elements("li").First() == node;
    var isLast = node.ParentNode.Elements("li").Last() == node;

    var listItem = section.AddParagraph().SetStyle(listStyle);

    if (listStyle == "UnorderedList")
    {
        listItem.Format.ListInfo.ListType = _nestedListLevel%2 == 1 ? ListType.BulletList2 : ListType.BulletList1;
    }
    else
    {
        listItem.Format.ListInfo.ListType = _nestedListLevel % 2 == 1 ? ListType.NumberList2 : ListType.NumberList1;
    }

    if (_nestedListLevel > 0)
    {
        listItem.Format.LeftIndent = String.Format(CultureInfo.InvariantCulture, "{0}in", _nestedListLevel*.75);
    }

    // disable continuation if this is the first list item
    listItem.Format.ListInfo.ContinuePreviousList = !isFirst;

    if (isLast)
        _nestedListLevel--;

    return listItem;
});
Donuts
  • 2,185
  • 3
  • 20
  • 31