cripsy-binary-api 0.3.3

API moderna multibroker para opciones binarias (Rust, sync y async)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
//! Generador de documentación navegable (single-file HTML, sin Node ni red).
//!
//! Lee `docs/**/*.<lang>.md` y produce `docs/site/<lang>/index.html`
//! con sidebar, índice de la página (TOC), routing por hash y búsqueda.
//! La fuente de verdad sigue siendo Markdown; el HTML es generado (nunca se
//! edita a mano).
//!
//! Uso: `cargo run --example build_docs -- --lang es`
//! PowerShell: `.\scripts\build_docs.ps1 -Lang es`

use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};

/// Proyecto publicado, usado en la cabecera y el pie del sitio.
const REPO: &str = "https://github.com/ionixcorp/cripsy_binary_API";
const VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Clone)]
struct Pagina {
    id: String,
    titulo: String,
    grupo: String,
    html: String,
    toc: String,
}

fn esc(t: &str) -> String {
    t.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

/// Convierte Markdown (subconjunto) a HTML.
fn md_a_html(md: &str) -> String {
    let mut out = String::new();
    let mut en_codigo = false;
    let mut codigo_lang = String::new();
    let mut codigo_buf = String::new();
    let mut lista_abierta: Option<&str> = None; // "ul" | "ol"
    let mut tabla_buf: Vec<Vec<String>> = Vec::new();

    macro_rules! cerrar_lista {
        () => {
            if let Some(t) = lista_abierta.take() {
                out.push_str(&format!("</{t}>"));
            }
        };
    }
    macro_rules! cerrar_tabla {
        () => {
            if !tabla_buf.is_empty() {
                let filas = std::mem::take(&mut tabla_buf);
                if !filas.is_empty() {
                    out.push_str("<table>");
                    for (i, fila) in filas.iter().enumerate() {
                        let etiqueta = if i == 0 { "th" } else { "td" };
                        out.push_str("<tr>");
                        for celda in fila {
                            out.push_str(&format!("<{etiqueta}>{}</{etiqueta}>", inline(celda)));
                        }
                        out.push_str("</tr>");
                    }
                    out.push_str("</table>");
                }
            }
        };
    }

    for linea in md.lines() {
        let l = linea.trim_end();
        if l.trim_start().starts_with("```") {
            if en_codigo {
                let clase = if codigo_lang.is_empty() {
                    "txt".to_string()
                } else {
                    codigo_lang.clone()
                };
                out.push_str(&format!(
                    "<pre class=\"code\"><code class=\"lang-{clase}\">{}</code></pre>",
                    esc(&codigo_buf),
                    clase = clase
                ));
                en_codigo = false;
                codigo_buf.clear();
            } else {
                cerrar_lista!();
                cerrar_tabla!();
                en_codigo = true;
                codigo_lang = l.trim_start().trim_start_matches("```").trim().to_string();
            }
            continue;
        }
        if en_codigo {
            codigo_buf.push_str(l);
            codigo_buf.push('\n');
            continue;
        }
        // Líneas de tabla
        if l.trim_start().starts_with('|') && l.trim_start().ends_with('|') {
            cerrar_lista!();
            let celdas: Vec<String> = l
                .trim()
                .trim_matches('|')
                .split('|')
                .map(|c| c.trim().to_string())
                .collect();
            // Separador |---|---|
            if celdas
                .iter()
                .all(|c| c.chars().all(|ch| ch == '-' || ch == ':'))
            {
                continue;
            }
            tabla_buf.push(celdas);
            continue;
        } else {
            cerrar_tabla!();
        }

        let t = l.trim();
        if t.is_empty() {
            cerrar_lista!();
            continue;
        }
        // Encabezados
        if let Some(rest) = t.strip_prefix("###### ") {
            cerrar_lista!();
            out.push_str(&format!("<h6>{}</h6>", inline(rest)));
            continue;
        }
        if let Some(rest) = t.strip_prefix("##### ") {
            cerrar_lista!();
            out.push_str(&format!("<h5>{}</h5>", inline(rest)));
            continue;
        }
        if let Some(rest) = t.strip_prefix("#### ") {
            cerrar_lista!();
            out.push_str(&format!("<h4>{}</h4>", inline(rest)));
            continue;
        }
        if let Some(rest) = t.strip_prefix("### ") {
            cerrar_lista!();
            out.push_str(&format!("<h3>{}</h3>", inline(rest)));
            continue;
        }
        if let Some(rest) = t.strip_prefix("## ") {
            cerrar_lista!();
            out.push_str(&format!("<h2>{}</h2>", inline(rest)));
            continue;
        }
        if let Some(rest) = t.strip_prefix("# ") {
            cerrar_lista!();
            out.push_str(&format!("<h1>{}</h1>", inline(rest)));
            continue;
        }
        // Lista
        if let Some(rest) = t.strip_prefix("- ") {
            if lista_abierta != Some("ul") {
                cerrar_lista!();
                out.push_str("<ul>");
                lista_abierta = Some("ul");
            }
            out.push_str(&format!("<li>{}</li>", inline(rest)));
            continue;
        }
        if t.len() > 2 && t.as_bytes()[0].is_ascii_digit() {
            if let Some(pos) = t.find(". ") {
                let resto = &t[pos + 2..];
                if lista_abierta != Some("ol") {
                    cerrar_lista!();
                    out.push_str("<ol>");
                    lista_abierta = Some("ol");
                }
                out.push_str(&format!("<li>{}</li>", inline(resto)));
                continue;
            }
        }
        // Cita
        if let Some(rest) = t.strip_prefix("> ") {
            cerrar_lista!();
            out.push_str(&format!("<blockquote>{}</blockquote>", inline(rest)));
            continue;
        }
        // Reglahorizontal
        if t == "---" || t == "***" {
            cerrar_lista!();
            out.push_str("<hr>");
            continue;
        }
        // Párrafo: acumula líneas contiguas
        cerrar_lista!();
        out.push_str(&format!("<p>{}</p>", inline(t)));
    }
    cerrar_lista!();
    cerrar_tabla!();
    out
}

/// Formato inline: **negrita**, `codigo`, [texto](url). Seguro para UTF-8
/// (recorrido por caracteres, sin cortes por byte).
fn inline(t: &str) -> String {
    let chars: Vec<char> = t.chars().collect();
    let mut out = String::new();
    let mut i = 0usize;
    let mut negrita = false;
    while i < chars.len() {
        // ** negrita **
        if chars[i] == '*' && i + 1 < chars.len() && chars[i + 1] == '*' {
            out.push_str(if negrita { "</strong>" } else { "<strong>" });
            negrita = !negrita;
            i += 2;
            continue;
        }
        // `codigo`
        if chars[i] == '`' {
            let mut j = i + 1;
            while j < chars.len() && chars[j] != '`' {
                j += 1;
            }
            if j < chars.len() {
                let contenido: String = chars[i + 1..j].iter().collect();
                out.push_str(&format!("<code>{}</code>", esc(&contenido)));
                i = j + 1;
                continue;
            }
        }
        // [texto](url)
        if chars[i] == '[' {
            let mut j = i + 1;
            while j + 1 < chars.len() && !(chars[j] == ']' && chars[j + 1] == '(') {
                j += 1;
            }
            if j + 1 < chars.len() && chars[j] == ']' && chars[j + 1] == '(' {
                let mut k = j + 2;
                while k < chars.len() && chars[k] != ')' {
                    k += 1;
                }
                if k < chars.len() {
                    let texto: String = chars[i + 1..j].iter().collect();
                    let url: String = chars[j + 2..k].iter().collect();
                    out.push_str(&format!("<a href=\"{}\">{}</a>", esc(&url), esc(&texto)));
                    i = k + 1;
                    continue;
                }
            }
        }
        match chars[i] {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            otro => out.push(otro),
        }
        i += 1;
    }
    if negrita {
        out.push_str("</strong>");
    }
    out
}

fn slug(path: &Path, lang: &str) -> String {
    let rel = path.to_string_lossy().replace('\\', "/");
    rel.trim_end_matches(&format!(".{lang}.md"))
        .replace("docs/", "")
        .replace(['/', ' '], "-")
        .to_lowercase()
}

/// Ancla estable a partir del texto visible de un encabezado.
fn slug_texto(t: &str) -> String {
    let mut s = String::new();
    let mut guion = false;
    for c in t.chars() {
        let c = c.to_lowercase().next().unwrap_or(c);
        if c.is_alphanumeric() {
            s.push(c);
            guion = false;
        } else if !guion && !s.is_empty() {
            s.push('-');
            guion = true;
        }
    }
    s.trim_matches('-').to_string()
}

/// Quita etiquetas y entidades para usar el texto como ancla.
fn sin_etiquetas(t: &str) -> String {
    let mut s = String::new();
    let mut dentro = false;
    for c in t.chars() {
        match c {
            '<' => dentro = true,
            '>' => dentro = false,
            c if !dentro => s.push(c),
            _ => {}
        }
    }
    s.replace("&amp;", "&")
        .replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&quot;", "\"")
}

/// Añade `id` a los `h2`/`h3` y devuelve el HTML con anclas y el índice de la
/// página (TOC) que se muestra en la columna derecha.
fn ids_y_toc(html: &str) -> (String, String) {
    let mut salida = String::with_capacity(html.len() + 512);
    let mut toc = String::new();
    let mut resto = html;
    let mut n = 0usize;
    loop {
        let encontrado = [("h2", "<h2>"), ("h3", "<h3>")]
            .iter()
            .filter_map(|(tag, marca)| resto.find(marca).map(|p| (p, *tag, *marca)))
            .min_by_key(|(p, _, _)| *p);
        let Some((pos, tag, marca)) = encontrado else {
            salida.push_str(resto);
            break;
        };
        let cierre = format!("</{tag}>");
        let inicio = pos + marca.len();
        let Some(fin_rel) = resto[inicio..].find(&cierre) else {
            salida.push_str(resto);
            break;
        };
        let fin = inicio + fin_rel;
        let texto = &resto[inicio..fin];
        n += 1;
        let id = format!("s{n}-{}", slug_texto(&sin_etiquetas(texto)));
        salida.push_str(&resto[..pos]);
        salida.push_str(&format!("<{tag} id=\"{id}\">{texto}</{tag}>"));
        let clase = format!("toc-{}", tag.trim_start_matches('h'));
        toc.push_str(&format!("<a class=\"{clase}\" href=\"#{id}\">{texto}</a>"));
        resto = &resto[fin + cierre.len()..];
    }
    (salida, toc)
}

fn main() {
    let args: Vec<String> = std::env::args().collect();
    let mut lang = "es".to_string();
    let mut root = ".".to_string();
    let mut out_dir = "docs/site".to_string();
    let mut i = 1;
    while i < args.len() {
        match args[i].as_str() {
            "--lang" => {
                i += 1;
                if let Some(v) = args.get(i) {
                    lang = v.clone();
                }
            }
            "--out" => {
                i += 1;
                if let Some(v) = args.get(i) {
                    out_dir = v.clone();
                }
            }
            _ => {
                root = args[i].clone();
            }
        }
        i += 1;
    }

    let base = Path::new(&root).join("docs");
    let sufijo = format!(".{lang}.md");
    let mut archivos: Vec<PathBuf> = Vec::new();
    recopilar(&base, &sufijo, &mut archivos);
    archivos.sort();

    let mut paginas: Vec<Pagina> = Vec::new();
    for ruta in &archivos {
        let md = match fs::read_to_string(ruta) {
            Ok(m) => m,
            Err(e) => {
                eprintln!("no se pudo leer {}: {e}", ruta.display());
                continue;
            }
        };
        let rel = ruta
            .strip_prefix(&base)
            .unwrap_or(ruta)
            .to_string_lossy()
            .replace('\\', "/");
        let grupo = if rel.contains('/') {
            rel.split('/').next().unwrap_or("general").to_string()
        } else {
            "general".to_string()
        };
        let nombre = rel
            .rsplit('/')
            .next()
            .unwrap_or(&rel)
            .trim_end_matches(&sufijo)
            .to_string();
        let id = slug(ruta, &lang);
        // Título: primer encabezado `# `, o el nombre del archivo
        let titulo = md
            .lines()
            .find_map(|l| l.strip_prefix("# ").map(str::to_string))
            .unwrap_or_else(|| {
                // Quita prefijos numéricos tipo "01_" y los guiones bajos.
                nombre
                    .split_once('_')
                    .map(|(_, resto)| resto)
                    .unwrap_or(&nombre)
                    .replace('_', " ")
            });
        let (html, toc) = ids_y_toc(&md_a_html(&md));
        paginas.push(Pagina {
            id,
            titulo,
            grupo,
            html,
            toc,
        });
    }

    let nav_grupos: BTreeMap<String, Vec<(String, String, String)>> = {
        let mut m: BTreeMap<String, Vec<(String, String, String)>> = BTreeMap::new();
        for p in &paginas {
            m.entry(p.grupo.clone()).or_default().push((
                p.id.clone(),
                p.grupo.clone(),
                p.titulo.clone(),
            ));
        }
        m
    };

    let mut nav_html = String::new();
    let mut grupos: Vec<_> = nav_grupos.iter().collect();
    grupos.sort_by_key(|(grupo, _)| orden_grupo(grupo));
    for (grupo, items) in grupos {
        nav_html.push_str(&format!(
            "<div class=\"nav-grupo\"><div class=\"nav-titulo\">{}</div>",
            esc(&nombre_grupo(grupo))
        ));
        for (id, _g, titulo) in items {
            nav_html.push_str(&format!(
                "<a class=\"nav-item\" href=\"#{id}\" data-id=\"{id}\">{}</a>",
                esc(titulo)
            ));
        }
        nav_html.push_str("</div>");
    }

    let mut contenido_html = String::new();
    for p in &paginas {
        contenido_html.push_str(&format!(
            "<section class=\"pagina\" id=\"{}\"><h1 class=\"titulo-pagina\">{}</h1>{}</section>",
            p.id,
            esc(&p.titulo),
            p.html
        ));
    }

    let busqueda_json: String = paginas
        .iter()
        .map(|p| {
            format!(
                "{{\"id\":\"{}\",\"t\":{},\"g\":{}}}",
                p.id,
                serde_json_string(&p.titulo),
                serde_json_string(&p.grupo)
            )
        })
        .collect::<Vec<_>>()
        .join(",");

    let tocs_json: String = paginas
        .iter()
        .map(|p| format!("\"{}\":{}", p.id, serde_json_string(&p.toc)))
        .collect::<Vec<_>>()
        .join(",");

    let html = format!(
        r##"<!DOCTYPE html>
<html lang="{lang}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>cripsy_binary_API — Documentación</title>
<meta name="description" content="Documentación de cripsy_binary_API: cliente Rust multibróker para IQ Option y Deriv.">
<style>
:root {{ --bg:#0b0e14; --panel:#11151f; --panel2:#151b28; --borde:#1f2736; --texto:#dde3ee; --suave:#8d97a8; --acento:#6aa8ff; --codigo:#0a0d13; }}
*, *::before, *::after {{ box-sizing:border-box; }}
html {{ scroll-behavior:smooth; }}
body {{ margin:0; background:var(--bg); color:var(--texto); font-family:"Segoe UI",system-ui,-apple-system,"Helvetica Neue",sans-serif; font-size:15px; line-height:1.6; display:grid; grid-template-columns:296px minmax(0,1fr) 264px; grid-template-rows:56px 1fr auto; min-height:100vh; }}
header.topbar {{ grid-column:1 / -1; display:flex; align-items:center; gap:14px; padding:0 22px; background:linear-gradient(180deg,#121826,#0e131d); border-bottom:1px solid var(--borde); position:sticky; top:0; z-index:20; }}
.marca {{ display:flex; align-items:center; gap:9px; font-weight:600; letter-spacing:.2px; color:var(--texto); text-decoration:none; }}
.marca .punto {{ width:9px; height:9px; border-radius:50%; background:var(--acento); box-shadow:0 0 12px var(--acento); }}
.marca .ver {{ font-size:11px; font-weight:500; color:var(--suave); border:1px solid var(--borde); border-radius:999px; padding:1px 8px; }}
header.topbar .sep {{ flex:1; }}
header.topbar a.enlace {{ font-size:13px; color:var(--suave); text-decoration:none; padding:6px 10px; border-radius:8px; }}
header.topbar a.enlace:hover {{ color:var(--texto); background:#1a2130; }}
#menu-movil {{ display:none; }}
nav {{ grid-column:1; grid-row:2 / span 2; background:var(--panel); border-right:1px solid var(--borde); padding:18px 14px 28px; position:sticky; top:56px; align-self:start; height:calc(100vh - 56px); overflow-y:auto; }}
nav .rotulo {{ font-size:11px; text-transform:uppercase; letter-spacing:.1em; color:var(--suave); margin:0 0 10px 4px; }}
#buscador {{ width:100%; padding:9px 11px; border-radius:9px; border:1px solid var(--borde); background:var(--codigo); color:var(--texto); margin-bottom:14px; }}
#buscador:focus {{ outline:none; border-color:var(--acento); box-shadow:0 0 0 3px rgba(106,168,255,.15); }}
.nav-grupo {{ margin-bottom:16px; }}
.nav-titulo {{ font-size:11px; text-transform:uppercase; letter-spacing:.08em; color:var(--suave); margin:8px 0 6px 4px; }}
.nav-item {{ display:block; padding:7px 10px; border-radius:8px; color:#c3cbd9; text-decoration:none; font-size:13.5px; border-left:2px solid transparent; }}
.nav-item:hover {{ background:var(--panel2); color:var(--texto); }}
.nav-item.activo {{ background:#1b2740; color:var(--acento); border-left-color:var(--acento); }}
main {{ grid-column:2; padding:34px 46px 64px; max-width:900px; }}
.pagina {{ display:none; }}
.pagina.activa {{ display:block; animation:aparece .18s ease-out; }}
@keyframes aparece {{ from {{ opacity:0; transform:translateY(4px); }} to {{ opacity:1; transform:none; }} }}
h1.titulo-pagina {{ font-size:28px; line-height:1.25; margin:0 0 22px; padding-bottom:14px; border-bottom:1px solid var(--borde); }}
h2 {{ font-size:20px; margin:32px 0 12px; scroll-margin-top:72px; }}
h3 {{ font-size:16.5px; margin:24px 0 10px; color:#e8edf7; scroll-margin-top:72px; }}
p, li {{ font-size:14.5px; color:#cfd6e4; }}
blockquote {{ border-left:3px solid var(--acento); background:#0f1622; margin:16px 0; padding:10px 16px; border-radius:0 8px 8px 0; color:#b9c2d2; }}
pre.code {{ background:var(--codigo); border:1px solid var(--borde); border-left:3px solid #2f57a0; border-radius:10px; padding:14px 16px; overflow-x:auto; font-size:13px; line-height:1.55; }}
code {{ font-family:"Cascadia Code",Consolas,"SF Mono",monospace; background:#161d2b; border:1px solid #222c3d; border-radius:5px; padding:1px 5px; font-size:12.8px; }}
pre code {{ background:none; border:none; padding:0; }}
table {{ border-collapse:separate; border-spacing:0; margin:16px 0; width:100%; font-size:13.5px; border:1px solid var(--borde); border-radius:10px; overflow:hidden; }}
th, td {{ padding:9px 12px; text-align:left; border-bottom:1px solid var(--borde); vertical-align:top; }}
th {{ background:#161d2b; color:#e8edf7; font-weight:600; }}
tr:last-child td {{ border-bottom:none; }}
table tr:nth-child(even) td {{ background:#0e141f; }}
a {{ color:var(--acento); text-decoration:none; }}
a:hover {{ text-decoration:underline; }}
hr {{ border:none; border-top:1px solid var(--borde); margin:26px 0; }}
aside.toc {{ grid-column:3; grid-row:2; position:sticky; top:56px; align-self:start; height:calc(100vh - 56px); overflow-y:auto; padding:26px 18px 40px; border-left:1px solid var(--borde); background:rgba(17,21,31,.55); }}
.toc-titulo {{ font-size:11px; text-transform:uppercase; letter-spacing:.1em; color:var(--suave); margin:0 0 10px; }}
#toc a {{ display:block; padding:5px 0 5px 12px; color:#a9b3c4; font-size:13px; border-left:2px solid var(--borde); }}
#toc a:hover {{ color:var(--texto); border-left-color:var(--acento); text-decoration:none; }}
#toc a.toc-3 {{ padding-left:24px; font-size:12.5px; color:#8d97a8; }}
.toc-vacio {{ font-size:13px; color:var(--suave); }}
.toc-bloque {{ margin-top:26px; padding-top:18px; border-top:1px solid var(--borde); }}
.toc-bloque a {{ display:block; font-size:13px; color:#a9b3c4; padding:4px 0; }}
footer {{ grid-column:1 / -1; display:flex; gap:18px; flex-wrap:wrap; align-items:center; padding:18px 22px; border-top:1px solid var(--borde); background:var(--panel); color:var(--suave); font-size:12.5px; }}
footer a {{ color:var(--suave); }}
footer code {{ font-size:12px; }}
@media (max-width:1120px) {{ body {{ grid-template-columns:1fr; }} nav, aside.toc {{ display:none; }} main {{ grid-column:1; padding:24px 20px 48px; }} header.topbar {{ padding:0 14px; }} #menu-movil {{ display:block; flex:1; max-width:320px; padding:7px 10px; border-radius:8px; border:1px solid var(--borde); background:var(--codigo); color:var(--texto); }} header.topbar a.enlace {{ display:none; }} }}
@media print {{ nav, aside.toc, header.topbar, footer {{ display:none; }} main {{ max-width:none; }} }}
</style>
</head>
<body>
<header class="topbar">
  <a class="marca" href="#{primero}"><span class="punto"></span>cripsy_binary_API <span class="ver">v{version}</span></a>
  <select id="menu-movil" aria-label="Ir a una pagina"></select>
  <span class="sep"></span>
  <a class="enlace" href="{repo}">Repositorio</a>
  <a class="enlace" href="{repo}/tree/master/docs">Markdown</a>
</header>
<nav>
  <div class="rotulo">Documentación</div>
  <input id="buscador" type="search" placeholder="Buscar…" aria-label="Buscar en la documentacion">
  <div id="nav">{nav_html}</div>
</nav>
<main id="contenido">{contenido_html}</main>
<aside class="toc">
  <div class="toc-titulo">En esta página</div>
  <div id="toc"></div>
  <div class="toc-bloque">
    <div class="toc-titulo">Enlaces</div>
    <a href="{repo}">Repositorio en GitHub ↗</a>
    <a href="{repo}/tree/master/installation">Instalación</a>
    <a href="{repo}/tree/master/scripts">Scripts de verificación</a>
  </div>
</aside>
<footer>
  <span>cripsy_binary_API · v{version} · MIT</span>
  <a href="{repo}">github.com/ionixcorp/cripsy_binary_API</a>
  <span>Generado desde Markdown en <code>docs/</code></span>
</footer>
<script>
var DOCS = [{busqueda_json}];
var TOCS = {{{tocs_json}}};
function mostrar(id) {{
  var seccion = document.getElementById(id);
  if (!seccion) {{ return; }}
  document.querySelectorAll('.pagina').forEach(function(s) {{ s.classList.remove('activa'); }});
  document.querySelectorAll('.nav-item').forEach(function(a) {{ a.classList.remove('activo'); }});
  seccion.classList.add('activa');
  var enlace = document.querySelector('.nav-item[data-id="' + id + '"]');
  if (enlace) {{ enlace.classList.add('activo'); enlace.scrollIntoView({{ block:'nearest' }}); }}
  var caja = document.getElementById('toc');
  caja.innerHTML = TOCS[id] || '<div class="toc-vacio">Esta página no tiene secciones.</div>';
  var selector = document.getElementById('menu-movil');
  if (selector) {{ selector.value = id; }}
  window.scrollTo(0, 0);
}}
document.querySelectorAll('.nav-item').forEach(function(a) {{
  a.addEventListener('click', function(e) {{
    e.preventDefault();
    var id = a.getAttribute('data-id');
    location.hash = id;
    mostrar(id);
  }});
}});
document.getElementById('buscador').addEventListener('input', function(e) {{
  var q = e.target.value.toLowerCase().trim();
  document.querySelectorAll('.nav-item').forEach(function(a) {{
    var id = a.getAttribute('data-id');
    var doc = DOCS.find(function(d) {{ return d.id === id; }});
    var visible = !q || (doc && (doc.t.toLowerCase().includes(q) || doc.g.toLowerCase().includes(q)));
    a.style.display = visible ? 'block' : 'none';
  }});
}});
document.getElementById('toc').addEventListener('click', function(e) {{
  var a = e.target.closest('a');
  if (!a) {{ return; }}
  e.preventDefault();
  var destino = document.getElementById(a.getAttribute('href').slice(1));
  if (destino) {{ destino.scrollIntoView({{ behavior:'smooth', block:'start' }}); }}
}});
(function () {{
  var selector = document.getElementById('menu-movil');
  if (!selector) {{ return; }}
  DOCS.forEach(function(d) {{
    var opcion = document.createElement('option');
    opcion.value = d.id;
    opcion.textContent = d.t;
    selector.appendChild(opcion);
  }});
  selector.addEventListener('change', function () {{
    location.hash = selector.value;
    mostrar(selector.value);
  }});
}})();
window.addEventListener('hashchange', function () {{
  mostrar(location.hash.replace('#','') || '{primero}');
}});
var primero = '{primero}';
if (location.hash) {{ mostrar(location.hash.replace('#','')); }} else {{ mostrar(primero); }}
</script>
</body>
</html>
"##,
        lang = lang,
        version = VERSION,
        repo = REPO,
        nav_html = nav_html,
        contenido_html = contenido_html,
        busqueda_json = busqueda_json,
        tocs_json = tocs_json,
        primero = paginas
            .first()
            .map(|p| p.id.clone())
            .unwrap_or_else(|| "inicio".into()),
    );

    let destino = Path::new(&out_dir).join(&lang);
    if let Err(e) = fs::create_dir_all(&destino) {
        eprintln!("no se pudo crear {}: {e}", destino.display());
        std::process::exit(1);
    }
    let ruta_html = destino.join("index.html");
    if let Err(e) = fs::write(&ruta_html, &html) {
        eprintln!("no se pudo escribir {}: {e}", ruta_html.display());
        std::process::exit(1);
    }
    println!(
        "documentación generada: {} ({} páginas)",
        ruta_html.display(),
        paginas.len()
    );
}

fn recopilar(dir: &Path, sufijo: &str, salida: &mut Vec<PathBuf>) {
    if let Ok(entradas) = fs::read_dir(dir) {
        for e in entradas.flatten() {
            let ruta = e.path();
            if ruta.is_dir() {
                recopilar(&ruta, sufijo, salida);
            } else if let Some(nombre) = ruta.file_name().and_then(|n| n.to_str()) {
                if nombre.ends_with(sufijo) {
                    salida.push(ruta);
                }
            }
        }
    }
}

fn nombre_grupo(g: &str) -> String {
    match g {
        "general" => "General".to_string(),
        "iqoption" => "IQ Option".to_string(),
        "deriv" => "Deriv".to_string(),
        "python" => "Python".to_string(),
        "herramientas" => "Herramientas".to_string(),
        "releases" => "Notas de versión".to_string(),
        otro => otro.to_string(),
    }
}

fn orden_grupo(g: &str) -> usize {
    match g {
        "general" => 0,
        "iqoption" => 1,
        "deriv" => 2,
        "python" => 3,
        "herramientas" => 4,
        "releases" => 5,
        _ => 99,
    }
}

fn serde_json_string(s: &str) -> String {
    let mut out = String::from("\"");
    for ch in s.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            c => out.push(c),
        }
    }
    out.push('"');
    out
}