Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Publicaciones sobre python (publicaciones antiguas, página 89)

Nikola Internals Doc

Ya que Niko­la, mi ge­ne­ra­dor de si­tio­s/­blogs es­tá­ti­cos re­ci­be bas­tan­te có­di­go de otro­s, me pa­re­ció que ya era ho­ra de em­pe­zar a do­cu­men­tar co­mo fun­cio­na por aden­tro. Así que acá es­tá Niko­la in­ter­nals aun­que to­da­vía le fal­ta un her­vo­r.

Migrating from Wordpress to Nikola

Se­ve­ral peo­ple ha­ve mi­gra­ted from Wor­dpress in­to Niko­la, and he­re are so­me of their des­crip­tions of the pro­ce­ss:

In ge­ne­ra­l, it see­ms to be wo­rkin­g, but the­re's so­me wo­rk sti­ll to be do­ne. Wor­dpress su­ppor­ts many di­ffe­rent plu­gins and ex­ten­sions whi­ch react to ma­rkup in their pa­ges, and su­ppor­ting tha­t's al­most an in­fi­ni­te ta­sk. Cu­rren­tly Niko­la's im­por­ter hand­les a few of the mo­re co­m­mo­n. But if you try to im­port your blog and get le­ss than ideal re­sul­ts, plea­se fi­le a bug and I'­ll do my best to fix it.

Usua­lly the fixes are ra­ther sim­ple, it's just that I ha­ve ne­ver seen that spe­ci­fic thing ;-)

Ha­ve fun!

Standalone Search in Nikola

This has been in the mas­ter bran­ch of Niko­la for a whi­le but on­ly now ha­ve I tried to fu­lly in­te­gra­te it, and po­lish all (mos­t) of the rou­gh edges.

By de­faul­t, Niko­la co­mes wi­th su­pport for sear­ch for­ms using Google and Du­ck­du­ck­go. So­me peo­ple di­sappro­ve of them for di­ffe­rent rea­sons [1] so the­re was a re­quest about su­ppor­ting a stan­da­lo­ne sear­ch en­gi­ne.

The best of breed of tho­se things see­ms to be Ti­pue so tha­t's what I su­pporte­d.

To use this, you need to ena­ble a plu­gi­n, and do so­me con­fi­gu­ra­tion chan­ges.

The plugin is called task_localsearch and you can find it in the Nikola source tree

Suppose your site is in a folder called mysite then to enable this plugin you need to create mysite/plugins and then copy task_localsearch.plugin and task_localsearch in there.

Then, in your site's conf.py find these options and change them accordingly:

SEARCH_FORM = """
<span class="navbar-form pull-left">
<input type="text" id="tipue_search_input">
</span>"""

ANALYTICS = """
<script type="text/javascript" src="/assets/js/tipuesearch_set.js"></script>
<script type="text/javascript" src="/assets/js/tipuesearch.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('#tipue_search_input').tipuesearch({
        'mode': 'json',
        'contentLocation': '/assets/js/tipuesearch_content.json',
        'showUrl': false
    });
});
</script>
"""

EXTRA_HEAD_DATA = """
<link rel="stylesheet" type="text/css" href="/assets/css/tipuesearch.css">
<div id="tipue_search_content" style="margin-left: auto; margin-right: auto; padding: 20px;"></div>
"""

How does it wo­rk? He­re's a de­mo si­te for you to tr­y!

I would not re­co­m­mend doing this for a big si­te, sin­ce it may load a mul­ti-­me­ga­b­y­te ja­vas­cript fi­le when you sear­ch, but for sma­ll to me­dium si­tes, it may be ok.

Te hago la tarea, pero con estilo

Pa­sa siem­pre, co­mo en to­das las lis­tas de pro­gra­ma­ció­n, que de vez en cuan­do al­guien pos­tea en la lis­ta de Py­thon Ar­gen­ti­na al­go que es ob­via­men­te su ta­rea pa­ra el ho­ga­r. Pa­ra ma­ne­jar­lo hay dos es­cue­las de pen­sa­mien­to:

  1. Mo­s­­tra­r­­le co­­­mo se ha­­ce es de­­ci­r­­le co­­­mo ha­­cer tra­m­­pa.

  2. Mo­s­­tra­r­­le co­­­mo se ha­­ce es en­se­­ña­r­­le.

Yo me in­clino más por 1) pe­ro creo ha­ber en­contra­do un ca­mino in­ter­me­dio:

1.5) Dar­le una so­lu­ción más com­pli­ca­da que el pro­ble­ma.

De esa ma­ne­ra, si en­tien­de la so­lu­ció­n, ya hi­zo el tra­ba­jo. Y si no la en­tien­de, va a ser tan ob­vio que no la en­tien­de, que el pro­fe­sor nun­ca le va a acep­tar la res­pues­ta.

Co­mo ejem­plo, es­te es el pro­ble­ma que pre­sen­ta­ron en PyA­r:

La lis­ta vie­ne de­sor­de­na­da y con dos ca­rac­te­res (uno en mi­nús­cu­la y otro en ma­yús­cu­la), por ejem­plo:

['eD', 'fC', 'hC', 'iC', 'jD', 'bD', 'fH', 'mS', 'aS', 'mD']

Pa­ra or­de­nar­la de­bo se­guir es­tas pau­ta­s:

  1. Ar­mar su­b­con­jun­tos de acuer­do a la ma­yús­cu­la y lue­go or­de­nar esos su­b­con­jun­tos por la canti­dad de miem­bros (en or­den as­cen­den­te), la lis­ta que­da de la si­guien­te for­ma:

    ['fH', 'mS', 'aS', 'fC', 'hC', 'iC', 'jD', 'bD', 'eD', 'mD']
  2. Aho­ra ca­da su­b­con­jun­to de­be es­tar or­de­na­do de ma­ne­ra as­cen­den­te y de acuer­do a la mi­nús­cu­la, con lo que la lis­ta que­da or­de­na­da así:

    ['fH', 'aS', 'mS', 'fC', 'hC', 'iC', 'bD', 'eD', 'jD', 'mD']

Ig­no­ran­do que el plan­teo es am­bi­guo (hay por lo me­nos dos ma­ne­ras de leer­lo, ca­paz que má­s), yo pro­pu­se es­ta so­lu­ció­n, que re­quie­re py­thon 3:

from collections import defaultdict
d1 = defaultdict(list)
[d1[i[1]].append(i) for i in  ['eD', 'fC', 'hC', 'iC', 'jD', 'bD', 'fH', 'mS', 'aS', 'mD']]
{i: d1[i].sort() for i in d1}
d2 = {len(d1[i]): d1[i] for i in d1}
print([item for sublist in [d2[i] for i in sorted(d2.keys())] for item in sublist])

Produce el resultado deseado: ['fH', 'aS', 'mS', 'fC', 'hC', 'iC', 'bD', 'eD', 'jD', 'mD'] pero de una manera que para entenderlo, el alumno necesita saber mas de lo que requiere el problema original, incluyendo tres o cuatro cosas que no creo que le hayan enseñado.


Contents © 2000-2023 Roberto Alsina