django-markdown-deux icon indicating copy to clipboard operation
django-markdown-deux copied to clipboard

Styles not working

Open erikcw opened this issue 12 years ago • 7 comments

I'm trying to use styles with the markdown templatetag. I've setup settings.MARKDOWN_DEUX_STYLES to match the 3 styles in the README -- but regardless of which I call (trusted or recipe), the "default" style always gets used.

In [14]: from markdown_deux.conf import settings

In [15]: settings.MARKDOWN_DEUX_STYLES
Out[15]: {'default': {'extras': {'code-friendly': None}, 'safe_mode': 'escape'}}

In [16]: settings.settings.MARKDOWN_DEUX_STYLES                                                                                                                         
Out[16]: 
{'default': {'extras': {'code-friendly': None}, 'safe_mode': 'escape'},
 'recipe': {'extras': {'code-friendly': None,
   'cuddled-lists': None,
   'demote-headers': 3,
   'footnotes': None,
   'header-ids': None,
   'html-classes': {'pre': 'prettyprint'},
   'link-patterns': None,
   'pyshell': None},
  'link_patterns': [(re.compile(r'recipe\s+#?(\d+)\b', re.IGNORECASE),
    'http://code.activestate.com/recipes/\\1/')],
  'safe_mode': 'escape'},
 'trusted': {'extras': {'code-friendly': None}, 'safe_mode': False}}

# notice how it returns "default" even though "trusted" is being requested
In [18]: markdown_deux.get_style('trusted')
Out[18]: {'extras': {'code-friendly': None}, 'safe_mode': 'escape'}


It looks like the get_style function is only looking at the default settings, and not the updated settings.

erikcw avatar Dec 17 '12 20:12 erikcw

@erikcw Did you ever get this worked out?

ajt avatar Mar 15 '13 16:03 ajt

@ajt No, never did get it working...

erikcw avatar Mar 15 '13 23:03 erikcw

Don't import the default style before setting the custom styles. This causes the settings to be loaded before they are defined and you end up with only the default style. Put this (with no imports from markdown_deux) into your settings.py and it should work:

    MARKDOWN_DEUX_STYLES = {
        "default": {
            "extras": {
                "code-friendly": None,
            },
            "safe_mode": "escape",
        },
        "trusted": {
            "extras": {
                "code-friendly": None,
            },
            "safe_mode": False,
        }
    }

mitchelljkotler avatar Apr 17 '13 02:04 mitchelljkotler

Hi all. Sorry on the non-response here. I haven't been working on a Django site for a while so I'm a little clueless on Django settings import mechanics.

I'd welcome a cleaner way to have this module pick up on MARKDOWN_DEUX_* settings from the site's settings.py file. Do you know of other django apps that have a better way here?

It looks like the README.md might be showing usage that will cause this problem:

Here is how you might add styles of your own, and preserve the default style:

    # settings.py
    from markdown_deux.conf.settings import MARKDOWN_DEUX_DEFAULT_STYLE

    MARKDOWN_DEUX_STYLES = {
        "default": MARKDOWN_DEUX_DEFAULT_STYLE,
        "trusted": {
    ...

@mitchelljkotler Does that look like the README is wrong here.

trentm avatar Apr 17 '13 04:04 trentm

The README is wrong.

The problem is that when you import MARKDOWN_DEUX_DEFAULT_STYLE, markdown_deux's settings.py is run, and since the user's MARKDOWN_DEUX_STYLES isn't defined yet, markdown duex's MARKDOWN_DEUX_STYLES is set to the default.

If you do not imoprt MARKDOWN_DEUX_DEFAULT_STYLE and just hard code it, as in my example, all works fine. By the time markdown deux's settings.py is imported, the user's MARKDOWN_DEUX_STYLES is defined and markdown deux's MARKDOWN_DEUX_STYLES picks it up correctly.

I can't think of a good way to allow this to work how you have it now. I think a decent compromise might be to always add the default style to the user's MARKDOWN_DEUX_STYLES.

So if the user wants to add a trusted style, they can do:

    MARKDOWN_DEUX_STYLES = {
        "trusted": {
            "extras": {
                "code-friendly": None,
            },
            "safe_mode": False,
        }
    }

And in markdown_deux.conf.settings.py:

MARKDOWN_DEUX_STYLES = getattr(settings, "MARKDOWN_DEUX_STYLES", {})
MARKDOWN_DEUX_STYLES.update({'default': MARKDOWN_DEUX_DEFAULT_STYLE})

This way, the default style is always accessible without the user having to put it in. Just one idea though.

mitchelljkotler avatar Apr 17 '13 14:04 mitchelljkotler

Sorry to revive such an old thread here, but another potential solution would be to define MARKDOWN_DEUX_DEFAULT_STYLE in a different module (say conf/styles.py) and then import it into settings.py.

Then in the README you would instruct the user to import MARKDOWN_DEUX_DEFAULT_STYLE from styles.py instead.

RevolutionTech avatar Sep 19 '16 07:09 RevolutionTech

First off, thanks to the contributors for this nice project.

Having said that, in its innocence, this issue is a very serious one. I am sure many people have wasted time trying to debug both their and this code before they found out that this is a mere bad documentation issue existing for 5 years already.

I will create a merge request to apply the correct recommendation by @mitchelljkotler .

Wtower avatar Jun 10 '17 15:06 Wtower