This file is indexed.

/usr/lib/python2.7/dist-packages/sagenb/notebook/docHTMLProcessor.py is in python-sagenb 1.0.1+ds1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
# -*- coding: utf-8 -*
r"""
Live Documentation in the Notebook

Conversion of HTML (output by Sphinx or docutils) to Sage worksheet txt
file.

This takes an HTML document, i.e., Sage documentation, and returns it in
the editable format (notebook worksheet format with evaluable examples). It
also returns a string representing the CSS link for the document.  The SGML
parser is setup to return only the body of the HTML documentation page and
to re-format Sage examples and type-setting.

This module contains three classes:

- :class:`sagenb.notebook.docHTMLProcessor.genericHTMLProcessor`:
  gathers all the common methods of the other two classes.

- :class:`sagenb.notebook.docHTMLProcessor.SphinxHTMLProcessor`:
  translates HTML file generated by Sphinx into a worksheet text file

- :class:`sagenb.notebook.docHTMLProcessor.docutilsHTMLProcessor`:
  translates HTML file generated by docutils ``rst2html`` command into a
  worksheet text file

.. NOTE:: 

    This extension of sgmllib.SGMLParser was partly inspired by Mark
    Pilgrim's 'Dive Into Python' examples.

AUTHORS:

- Dorian Raymer (2006): first version

- William Stein (2007-06-10): rewrite to work with twisted Sage notebook

- Mike Hansen (2008-09-27): Rewrite to work with Sphinx HTML documentation

- Sebastien Labbe (2011-01-15): Added a new class named
  docutilsHTMLProcessor used for translating the html output of the
  rst2html docutils command run on a rst file into worksheet text file.
  Also added a new class named genericHTMLProcessor which gathers the
  common methods of both docutilsHTMLProcessor and SphinxHTMLProcessor
  classes. Added lots of doctests to make its coverage 100% doctested.

EXAMPLES:

Process the output of docutils ``rst2html`` command::

    sage: rst = ""
    sage: rst += "Additions in Sage\n"
    sage: rst += "-----------------\n"
    sage: rst += "\n"
    sage: rst += "Let's do easy computations with Sage::\n"
    sage: rst += "\n"
    sage: rst += "    s" + "age: 4 + 3\n"
    sage: rst += "    7\n"
    sage: rst += "    s" + "age: 1 - 2\n"
    sage: rst += "    -1\n"
    sage: rst += "\n"
    sage: rst += "Let's do `x^2`::\n"
    sage: rst += "\n"
    sage: rst += "    s" + "age: x^2\n"
    sage: rst += "    x^2\n"
    sage: from docutils.core import publish_string
    sage: html = publish_string(rst, writer_name='html')
    sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
    sage: p = docutilsHTMLProcessor()
    sage: txt = p.process_doc_html(html)
    sage: len(txt)
    191
    sage: print(txt)
    <h1 class="title">Additions in Sage</h1>
    <BLANKLINE>
    <BLANKLINE>
    <BLANKLINE>
    <p>Let's do easy computations with Sage:</p>
    <BLANKLINE>
    {{{id=0|
    4 + 3
    ///
    7
    }}}
    <BLANKLINE>
    {{{id=1|
    1 - 2
    ///
    -1
    }}}
    <BLANKLINE>
    <p>Let's do $x^2$:</p>
    <BLANKLINE>
    {{{id=2|
    x^2
    ///
    x^2
    }}}
    <BLANKLINE>
    <BLANKLINE>

WARNING:
    
    Input strings must be unicode.
"""
#############################################################################
#       Copyright (C) 2007 William Stein <wstein@gmail.com> and Dorian Raimer
#       Copyright (C) 2011 Sebastien Labbe <slabqc at gmail.com> 
#  Distributed under the terms of the GNU General Public License (GPL)
#  The full text of the GPL is available at:
#                  http://www.gnu.org/licenses/
#############################################################################
from __future__ import unicode_literals

from sgmllib import SGMLParser
from htmlentitydefs import entitydefs

from flask import Markup
from sagenb.misc.misc import unicode_str


class genericHTMLProcessor(SGMLParser):
    r"""
    This class gathers the methods that are common to both classes
    :class:`sagenb.notebook.SphinxHTMLProcessor` and
    :class:`sagenb.notebook.docutilsHTMLProcessor` .
    """
    def process_doc_html(self, doc_in):
        r"""
        Returns processed HTML input as HTML output.  This is the only
        method that needs to be called externally.

        INPUT:

        - ``doc_in`` - a string containing properly formed HTML

        OUTPUT:

        - a string; the processed HTML

        EXAMPLES::

            sage: rst = ""
            sage: rst += "Title\n"
            sage: rst += "-----\n"
            sage: rst += "n"
            sage: rst += "Some text\n"
            sage: from docutils.core import publish_string
            sage: html = publish_string(rst, writer_name='html')
            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: txt = p.process_doc_html(html)
            sage: len(txt)
            51
            sage: txt
            u'<h1 class="title">Title</h1>\n\n<p>nSome text</p>\n\n\n\n'

        """        
        # self.feed() is a SGMLParser method and starts everything
        # off; Most of the functions here are extensions to
        # SGMLParser, and may never actually be visibly called here.

        # This module works with unicode literals. In case that input data is
        # ascii, exceptions may occur. So, input data must be converted to
        # unicode if it were not.
        doc_in = unicode_str(doc_in)  
        self.feed(doc_in) #SGMLParser call
        self.close()     #SGMLParser call
        self.hand_off_temp_pieces('to_doc_pieces')
        return self.all_pieces.replace('\\(', '').replace('\\)', '').replace('\\[', '').replace('\\]', '')


    def hand_off_temp_pieces(self, piece_type):
        r"""
        To separate the documentation's content from the Sage
        examples, everything is split into one of two cell types.
        This method puts the current ``self.temp_pieces`` into
        ``self.all_pieces``.

        INPUT:

        - ``piece_type`` - a string; indicates the type of and how to
          process the current ``self.temp_pieces``. It can be one of the
          following:

          - ``"to_doc_pieces"`` - put temp_pieces in all_pieces
          - ``"ignore"`` - delete temp_pieces
          - ``"to_cell_pieces"`` - translate temp_pieces into cells and put
            it in all_pieces

        EXAMPLES:

        Move temporary pieces to all pieces::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.hand_off_temp_pieces('to_doc_pieces')
            sage: p.all_pieces
            u'a lot of stuff done bunch of tmp strings'
            sage: p.temp_pieces
            []

        Ignore temporary pieces::

            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.hand_off_temp_pieces('ignore')
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            []

        Translate temporary pieces (starting with sage prompt) into cells::

            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['sage'+': 4+4\n', '8\n', 'sage'+': 9-4\n', '5\n']
            sage: p.hand_off_temp_pieces('to_cell_pieces')
            sage: print(p.all_pieces)
            a lot of stuff done
            {{{id=0|
            4+4
            ///
            8
            }}}
            <BLANKLINE>
            {{{id=1|
            9-4
            ///
            5
            }}}
            sage: p.temp_pieces
            []

        Translate temporary pieces (not starting with sage prompt) into cells::

            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.hand_off_temp_pieces('to_cell_pieces')
            sage: print(p.all_pieces)
            a lot of stuff done <pre class="literal-block">
            bunch of tmp strings
            </pre>
            sage: p.temp_pieces
            []

        """
        pieces = "".join(self.temp_pieces)
        pieces = pieces.lstrip()
        if piece_type == 'to_doc_pieces':
            self.all_pieces += pieces
            self.temp_pieces = []
        elif piece_type == 'ignore':
            self.temp_pieces = []
        elif piece_type == 'to_cell_pieces':
            pieces = self.process_cell_input_output(pieces)
            self.all_pieces += pieces
            self.temp_pieces = []
        else:
            raise ValueError('unknown piece_type(=%s)' % piece_type)

    def get_cellcount(self):
        r"""
        Return the current cell count and increment it by one.

        OUTPUT:

        - an int

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: d = docutilsHTMLProcessor()
            sage: d.get_cellcount()
            0
            sage: d.get_cellcount()
            1

        ::

            sage: from sagenb.notebook.docHTMLProcessor import SphinxHTMLProcessor
            sage: d = SphinxHTMLProcessor()
            sage: d.get_cellcount()
            0
            sage: d.get_cellcount()
            1
        """
        self.cellcount += 1
        return self.cellcount - 1

    def process_cell_input_output(self, cell_piece):
        r"""
        Process and return a ``cell_piece``.

        All divs with CSS class="highlight" (if generated with Sphinx)  or
        class="literal-block" (if generated with docutils) contain code
        examples.  They include

        - Models of how the function works.  These begin with, e.g.,
          'INPUT:' and are re-styled as divs with
          class="usage_model".

        - Actual Sage input and output.  These begin with 'sage:'.
          The input and output are separated according to the
          Notebook edit format.

        INPUT:

        - ``cell_piece`` - a string; a cell piece

        OUTPUT:

        - a string; the processed cell piece

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: s = "s" + "age: 4 + 4\n8"    # avoid the doctest script to parse "sage:"
            sage: p.process_cell_input_output(s)
            u'\n{{{id=0|\n4 + 4\n///\n8\n}}}\n\n'
            sage: print(p.process_cell_input_output(s))
            {{{id=1|
            4 + 4
            ///
            8
            }}}

        ::

            sage: s = "age: 4 + 4\n8"
            sage: print(p.process_cell_input_output(s))
            <pre class="literal-block">
            age: 4 + 4
            8
            </pre>

        ::

            sage: s = '&gt;'*3 + " 4 + 4\n8"
            sage: print(p.process_cell_input_output(s))
            {{{id=2|
            4 + 4
            ///
            8
            }}}

        ::

            sage: s = "s" + "age: 4 + 4\n8\ns" + "age: 2 + 2\n4"
            sage: print(p.process_cell_input_output(s))
            {{{id=3|
            4 + 4
            ///
            8
            }}}
            <BLANKLINE>
            {{{id=4|
            2 + 2
            ///
            4
            }}}
        """
        if cell_piece[:5] != 'sage:' and cell_piece[:12] != '&gt;'*3:
            piece = self.false_positive_input_output_cell(cell_piece)
        else:
            # group and format inputs and outputs
            pieces = cell_piece.split('\n')
            output_flag = False
            piece = '\n{{{id=%s|\n'%self.get_cellcount()
            for p in pieces:

                if p[:6] == 'sage: ' and not output_flag:
                    piece += p[6:] + '\n'
                elif p[:6] == 'sage: ' and output_flag:
                    piece += '\n}}}\n\n{{{id=%s|\n'%self.get_cellcount() + p[6:] + '\n'
                    output_flag = False
                elif p[:6] == '....: ':
                    piece += p[6:] + '\n'
                elif p[:13] == '&gt;'*3+' ' and not output_flag:
                    piece += p[13:] + '\n'
                elif p[:13] == '&gt;'*3+' ' and output_flag:
                    piece += '\n}}}\n\n{{{id=%s|\n'%self.get_cellcount() + p[13:] + '\n'
                    output_flag = False
                elif p[:4] == '... ':
                    piece += p[4:] + '\n'
                else:
                    # first occurrence of an output string
                    # write /// denoting output
                    if output_flag == False:
                        piece += '///'
                        if p:
                            piece += '\n' + p
                        output_flag = True
                    # multiple output lines exist, don't need /// repeated
                    else:
                        piece += p
            piece += '\n}}}\n\n'
        return Markup(piece).unescape()
                
    ##############################################
    ## General tag handlers
    ## These just append their HTML to self.temp_pieces.
    def unknown_starttag(self, tag, attrs):
        r"""
        INPUT:

        - ``tag`` - string
        - ``attrs`` - list of tuples

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: tag = 'style'
            sage: attrs = [('type', 'text/css')]
            sage: p.unknown_starttag(tag, attrs)
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', u'<style type="text/css">']
        """
        if self.keep_data:
            strattrs = "".join([' %s="%s"' % (key, value) for key, value in attrs])
            self.temp_pieces.append("<%(tag)s%(strattrs)s>" % locals())

    def unknown_endtag(self, tag):
        r"""
        INPUT:

        - ``tag`` - string

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.unknown_endtag('head')
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', u'</head>']
        """
        if self.keep_data:
            self.temp_pieces.append("</%(tag)s>" % locals())

    def handle_data(self, data):
        r"""
        INPUT:

        - ``data`` - string

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.handle_data('some important data')
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', 'some important data']
        """
        if self.keep_data:
            self.temp_pieces.append(data)
    def handle_charref(self, ref):
        r"""
        INPUT:

        - ``ref`` - string

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.handle_charref('160')
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', u'&#160;']
        """
        if self.keep_data:
            self.temp_pieces.append("&#%(ref)s;" % locals())

    def handle_entityref(self, ref):
        r"""
        INPUT:

        - ``ref`` - string

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.handle_entityref('160')
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', u'&160']
        """
        if self.keep_data:
            self.temp_pieces.append("&%(ref)s" % locals())
            if ref in entitydefs:
                self.temp_pieces.append(';')

    def handle_comment(self, data):             
        r"""
        INPUT:

        - ``data`` - string

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.handle_comment('important comment')
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', u'<!--important comment-->']
        """
        if self.keep_data:
            self.temp_pieces.append("<!--%(data)s-->" % locals())
    def handle_pi(self, text):
        r"""
        Handle processing instructions

        INPUT:

        - ``text`` - string

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.handle_pi('instructions')
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', u'<?instructions>']
        """
        if self.keep_data:
            self.temp_pieces.append("<?%(text)s>" % locals())

    def handle_decl(self, text):
        r"""
        INPUT:

        - ``data`` - string

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.handle_decl('declaration')
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', u'<!declaration>']
        """
        if self.keep_data:
            self.temp_pieces.append("<!%(text)s>" % locals())
        
    ##############################################
    ## Specific tag handlers
    def start_body(self, attrs):
        r"""
        Set ``self.keep_data`` to True upon finding the opening body tag.

        INPUT:

        - ``attrs`` - a string:string dictionary containing the
          element's attributes

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import SphinxHTMLProcessor
            sage: d = SphinxHTMLProcessor()
            sage: d.keep_data
            False
            sage: d.start_body(None)
            sage: d.keep_data
            True

        ::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: d = docutilsHTMLProcessor()
            sage: d.keep_data
            False
            sage: d.start_body(None)
            sage: d.keep_data
            True
        """
        self.keep_data = True

    def end_body(self):
        r"""
        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.end_body()
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings'] 
        """
        pass
    def end_html(self):
        r"""
        INPUT:

        - ``data`` - string

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.end_html()
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings'] 
        """
        pass

class SphinxHTMLProcessor(genericHTMLProcessor):
    def reset(self):
        r"""
        Initialize necessary variables.  Called by
        :meth:`SGMLParser.__init__`.

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import SphinxHTMLProcessor
            sage: d = SphinxHTMLProcessor()    #indirect doctest
            sage: d.keep_data
            False
            sage: d.in_highlight_div
            False
            sage: d.temp_pieces
            []
            sage: d.all_pieces
            u''
            sage: d.cellcount
            0
        """
        # flags
        self.keep_data = False #don't keep anything before the <body> tag
        self.in_highlight_div = False

        # lists of what the parser keeps
        self.temp_pieces = []
        self.all_pieces = ''

        # counters
        self.cellcount = 0
                
        SGMLParser.reset(self)

    def false_positive_input_output_cell(self, cell_piece):
        r"""
        Return the untouched html string of a false positive input output
        cell.

        A false positive input-output cell come from a block of code which
        doesn't start with the sage prompt string 'sage:' or the Python
        prompt '>>>'.

        INPUT:

        - ``cell_piece`` - string, a cell piece

        OUPUT:

            string

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import SphinxHTMLProcessor
            sage: p = SphinxHTMLProcessor()
            sage: s = "sage -rst2html -h"
            sage: print(p.false_positive_input_output_cell(s))
            <div class="highlight"><pre>
            sage -rst2html -h
            </pre></div>

        """
        piece = '<div class="highlight"><pre>\n'
        piece += cell_piece
        piece = piece.replace('{','{&nbsp;')
        piece = piece.replace('}','}&nbsp;')
        piece += '\n</pre></div>'
        return piece

    #############################################
    ## Specific tag handlers
    ##
    def start_div(self, attrs):
        r"""
        Find out if we are starting a highlighted div.

        Once we hit the <div> tag in a highlighted block,
        hand of all of the pieces we've encountered so far
        and ignore the tag.
        
        INPUT:

        - ``attrs`` - list of tuple

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import SphinxHTMLProcessor
            sage: p = SphinxHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: attrs = [('class', 'highlight')]
            sage: p.start_div(attrs)
            sage: p.all_pieces
            u'a lot of stuff done bunch of tmp strings'
            sage: p.temp_pieces
            []
            sage: p.in_highlight_div
            True

        ::

            sage: p = SphinxHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: attrs = [('class', 'something-else')]
            sage: p.start_div(attrs)
            sage: p.all_pieces
            'a lot of stuff done ' 
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', u'<div class="something-else">']
            sage: p.in_highlight_div
            False
        """
        for name, value in attrs:
            if name.lower()=='class' and value.lower()=='highlight':
                self.in_highlight_div = True
                self.hand_off_temp_pieces('to_doc_pieces')
                return
        self.unknown_starttag('div', attrs)

    def end_div(self):
        r"""
        Once we end the highlighted div, convert all of the pieces
        to cells.

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import SphinxHTMLProcessor
            sage: p = SphinxHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings\n']
            sage: p.keep_data = True
            sage: attrs = [('class', 'highlight')]
            sage: p.start_div(attrs)
            sage: p.start_pre([])
            sage: sprompt = 'sa' + 'ge' + ': '    # to avoid problems with doctest script
            sage: p.handle_data('%s4+4\n8\n%sx^2\nx^2\n' % (sprompt, sprompt))
            sage: p.end_pre()
            sage: p.end_div()
            sage: print(p.all_pieces)
            a lot of stuff done bunch of tmp strings
            {{{id=0|
            4+4
            ///
            8
            }}}
            <BLANKLINE>
            {{{id=1|
            x^2
            ///
            x^2
            }}}
            sage: p.temp_pieces
            [] 
            sage: p.in_highlight_div
            False

        ::

            sage: p = SphinxHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: attrs = [('class', 'something-else')]
            sage: p.start_div(attrs)
            sage: p.handle_data('some data')
            sage: p.end_div()
            sage: print(p.all_pieces)
            a lot of stuff done
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', u'<div class="something-else">', 'some data', u'</div>']
            sage: p.in_highlight_div
            False

        """
        if self.in_highlight_div:
            self.in_highlight_div = False
            self.hand_off_temp_pieces('to_cell_pieces')
            return
        self.temp_pieces.append("</div>")
    
    def start_pre(self, attrs):
        r"""
        Ignore tag <pre> when inside highligh div.

        INPUT:

        - ``attrs`` - list of tuple

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import SphinxHTMLProcessor
            sage: p = SphinxHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.in_highlight_div = True
            sage: attrs = []
            sage: p.start_pre(attrs)
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings'] 

        ::

            sage: from sagenb.notebook.docHTMLProcessor import SphinxHTMLProcessor
            sage: p = SphinxHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.in_highlight_div = False
            sage: attrs = []
            sage: p.start_pre(attrs)
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', u'<pre>']
        """
        if self.in_highlight_div:
            return
        self.unknown_starttag('pre',attrs)

    def end_pre(self):
        r"""
        Ignore tag </pre> when inside highligh div.

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import SphinxHTMLProcessor
            sage: p = SphinxHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.in_highlight_div = True
            sage: p.end_pre()
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings'] 

        ::

            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.in_highlight_div = False
            sage: p.end_pre()
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', u'</pre>']
        """
        if self.in_highlight_div:
            return
        self.unknown_endtag('pre')

    #Ignore forms
    def start_form(self, attrs):
        r"""
        Hand of everything we've accumulated so far.

        Forms are ignored.

        INPUT:

        - ``attrs`` - list of tuple

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import SphinxHTMLProcessor
            sage: p = SphinxHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: attrs = []
            sage: p.start_form(attrs)
            sage: p.all_pieces
            u'a lot of stuff done bunch of tmp strings'
            sage: p.temp_pieces
            [] 
        """
        self.hand_off_temp_pieces('to_doc_pieces')
        return

    def end_form(self):
        r"""
        Ignore all of the pieces since we started
        the form.

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import SphinxHTMLProcessor
            sage: p = SphinxHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.end_form()
            sage: p.all_pieces
            'a lot of stuff done ' 
            sage: p.temp_pieces
            [] 
        """
        self.hand_off_temp_pieces('ignore')
        return

    def start_span(self, attrs):
        r"""
        Ignore all spans that occur within highlighted blocks

        INPUT:

        - ``attrs`` - list of tuple

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import SphinxHTMLProcessor
            sage: p = SphinxHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.in_highlight_div = True
            sage: attrs = []
            sage: p.start_span(attrs)
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings'] 

        ::

            sage: from sagenb.notebook.docHTMLProcessor import SphinxHTMLProcessor
            sage: p = SphinxHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.in_highlight_div = False
            sage: attrs = []
            sage: p.start_span(attrs)
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', u'<span>']
        """
        if self.in_highlight_div:
            return
        self.unknown_starttag('span', attrs)
    def end_span(self):
        r"""
        Ignore all spans that occur within highlighted blocks

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import SphinxHTMLProcessor
            sage: p = SphinxHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.in_highlight_div = True
            sage: p.end_span()
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings'] 

        ::

            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.in_highlight_div = False
            sage: p.end_span()
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', u'</span>']
        """
        if self.in_highlight_div:
            return        
        self.unknown_endtag('span')

class docutilsHTMLProcessor(genericHTMLProcessor):
    r"""
    Translates output of the docutils parser rst2html into notebook text.

    EXAMPLES::

        sage: rst = ""
        sage: rst += "Additions in Sage\n"
        sage: rst += "-----------------\n"
        sage: rst += "\n"
        sage: rst += "Let's do easy computations with Sage::\n"
        sage: rst += "\n"
        sage: rst += "    s" + "age: 4 + 3\n"
        sage: rst += "    7\n"
        sage: rst += "    s" + "age: 1 - 2\n"
        sage: rst += "    -1\n"
        sage: rst += "\n"
        sage: rst += "Let's do `x^2`::\n"
        sage: rst += "\n"
        sage: rst += "    s" + "age: x^2\n"
        sage: rst += "    x^2\n"
        sage: from docutils.core import publish_string
        sage: html = publish_string(rst, writer_name='html')
        sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
        sage: p = docutilsHTMLProcessor()
        sage: txt = p.process_doc_html(html)
        sage: len(txt)
        191
        sage: print(txt)
        <h1 class="title">Additions in Sage</h1>
        <BLANKLINE>
        <BLANKLINE>
        <BLANKLINE>
        <p>Let's do easy computations with Sage:</p>
        <BLANKLINE>
        {{{id=0|
        4 + 3
        ///
        7
        }}}
        <BLANKLINE>
        {{{id=1|
        1 - 2
        ///
        -1
        }}}
        <BLANKLINE>
        <p>Let's do $x^2$:</p>
        <BLANKLINE>
        {{{id=2|
        x^2
        ///
        x^2
        }}}
        <BLANKLINE>
        <BLANKLINE>

    """
    def reset(self):
        r"""
        Initialize necessary variables.  Called by
        :meth:`SGMLParser.__init__`.

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: d = docutilsHTMLProcessor()    #indirect doctest
            sage: d.keep_data
            False
            sage: d.in_pre_litteral_block
            False
            sage: d.in_div_footer_block
            False
            sage: d.temp_pieces
            []
            sage: d.all_pieces
            u''
            sage: d.cellcount
            0
        """
        # flags
        self.keep_data = False #don't keep anything before the <body> tag
        self.in_pre_litteral_block = False
        self.in_div_footer_block = False

        # lists of what the parser keeps
        self.temp_pieces = []
        self.all_pieces = ''

        # counters
        self.cellcount = 0
                
        SGMLParser.reset(self)

    def false_positive_input_output_cell(self, cell_piece):
        r"""
        Return the untouched html string of a false positive input output
        cell.

        A false positive input-output cell come from a block of code which
        doesn't start with the sage prompt string 'sage:' or the Python
        prompt '>>>'.

        INPUT:

        - ``cell_piece`` - string, a cell piece

        OUPUT:

            string

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: s = "sage -rst2html -h"
            sage: print(p.false_positive_input_output_cell(s))
            <pre class="literal-block">
            sage -rst2html -h
            </pre>

        """
        piece = '<pre class="literal-block">\n'
        piece += cell_piece
        piece = piece.replace('{','{&nbsp;')
        piece = piece.replace('}','}&nbsp;')
        piece += '\n</pre>'
        return piece
    
    #############################################
    ## Specific tag handlers
    ##
    # sage blocks
    def start_pre(self, attrs):
        r"""
        INPUT:

        - ``attrs`` - list of tuple

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: attrs = [('class', 'literal-block')]
            sage: p.start_pre(attrs)
            sage: p.all_pieces
            u'a lot of stuff done bunch of tmp strings'
            sage: p.temp_pieces
            []
            sage: p.in_pre_litteral_block
            True

        ::

            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: attrs = [('class', 'something-else')]
            sage: p.start_pre(attrs)
            sage: p.all_pieces
            'a lot of stuff done ' 
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', u'<pre class="something-else">']
            sage: p.in_pre_litteral_block
            False
        """
        #Find out if we are starting a pre litteral-block
        for name, value in attrs:
            if name.lower()=='class' and value.lower()=='literal-block':
                self.in_pre_litteral_block = True
                self.hand_off_temp_pieces('to_doc_pieces')
                return
        self.unknown_starttag('pre',attrs)

    def end_pre(self):
        r"""
        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: attrs = [('class', 'literal-block')]
            sage: p.start_pre(attrs)
            sage: sprompt = 'sa' + 'ge' + ': '    # to avoid problems with doctest script
            sage: p.handle_data('%s4+4\n8\n%sx^2\nx^2\n' % (sprompt, sprompt))
            sage: p.end_pre()
            sage: print(p.all_pieces)
            a lot of stuff done bunch of tmp strings
            {{{id=0|
            4+4
            ///
            8
            }}}
            <BLANKLINE>
            {{{id=1|
            x^2
            ///
            x^2
            }}}
            sage: p.temp_pieces
            [] 
            sage: p.in_pre_litteral_block 
            False

        ::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: attrs = [('class', 'something-else')]
            sage: p.start_pre(attrs)
            sage: p.handle_data('some data')
            sage: p.end_pre()
            sage: print(p.all_pieces)
            a lot of stuff done
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings', u'<pre class="something-else">', 'some data', u'</pre>']
            sage: p.in_pre_litteral_block
            False
        """
        if self.in_pre_litteral_block:
            self.in_pre_litteral_block = False
            self.hand_off_temp_pieces('to_cell_pieces')
            return
        self.unknown_endtag('pre')

    # Ignore div
    def start_div(self, attrs):
        r"""
        INPUT:

        - ``attrs`` - list of tuple

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: attrs = [('class', 'document'), ('id', 'title')]
            sage: p.start_div(attrs)
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings'] 
        """
        #Find out if we are starting a div footer block
        for name, value in attrs:
            if name.lower()=='class' and value.lower()=='footer':
                self.hand_off_temp_pieces('to_doc_pieces')
                self.keep_data = False
                self.in_div_footer_block = True
                return
        return

    def end_div(self):
        r"""
        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.end_div()
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings'] 
        """
        if self.in_div_footer_block:
            self.in_div_footer_block = False
            self.keep_data = True
        return

    # latex role
    def start_cite(self, attrs):
        r"""
        INPUT:

        - ``attrs`` - list of tuple

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: attrs = []
            sage: p.start_cite(attrs)
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings',  u'$']
        """
        self.temp_pieces.append("$")
        return

    def end_cite(self):
        r"""
        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.keep_data = True
            sage: p.start_cite([])
            sage: p.handle_data('x^2')
            sage: p.end_cite()
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings',  u'$', 'x^2',  u'$']
        """
        self.temp_pieces.append("$")
        return

    # script (for example for mathjax)
    def start_script(self, attrs):
        r"""
        INPUT:

        - ``attrs`` - list of tuple

        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: attrs = [('type', 'text/x-mathjax-config')]
            sage: p.start_script(attrs)
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings'] 
            sage: p.keep_data
            False
        """
        self.keep_data = False
        return

    def end_script(self):
        r"""
        EXAMPLES::

            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: p.all_pieces = 'a lot of stuff done '
            sage: p.temp_pieces = ['bunch ', 'of ', 'tmp ', 'strings']
            sage: p.end_script()
            sage: p.all_pieces
            'a lot of stuff done '
            sage: p.temp_pieces
            ['bunch ', 'of ', 'tmp ', 'strings'] 
            sage: p.keep_data 
            True
        """
        self.keep_data = True
        return