Source syntax highlighting for habratopiks

    Good afternoon.

    First of all, I would like to thank w999d for the invite :) In response, I wanted to do something nice for Habrachan :)

    When I wrote my first post on Habré in the sandbox, I noticed that Habr doesn’t support me for some mysterious reason syntax highlighting tag for posts. When you write an article on programming, you must format the source code, otherwise no one will read it. Then I somehow managed with some third-party formatter, whose work for PHP code did not suit me very much. But the thought of making a normal formatter for habrachan fell into my head. However, even if it turns out that I invented a bicycle, the time that I spent sorting out the source of the formatter was definitely not wasted.


    So, I present the most alpha version of the HabraSyntax formatter: http://habrasyntax.fractalizer.ru

    After formatting, it turns out something like the following:


    /*======================================================================*\
    || #################################################################### ||
    || # vBulletin 3.7.3
    || # ---------------------------------------------------------------- # ||
    || # Copyright ©2000-2008 Jelsoft Enterprises Ltd. All Rights Reserved. ||
    || # This file may not be redistributed in whole or significant part. # ||
    || # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
    || # www.vbulletin.com | www.vbulletin.com/license.html # ||
    || #################################################################### ||
    \*======================================================================
    */

    // ####################### SET PHP ENVIRONMENT ###########################
    error_reporting(E_ALL & ~E_NOTICE);

    // #################### DEFINE IMPORTANT CONSTANTS #######################
    define('THIS_SCRIPT', 'ajax');
    define('CSRF_PROTECTION', true);
    define('LOCATION_BYPASS', 1);
    define('NOPMPOPUP', 1);

    // ################### PRE-CACHE TEMPLATES AND DATA ######################
    // get special phrase groups
    $phrasegroups = array('posting');
    switch ($_POST['do'])
    {
        case 'fetchuserfield':
        case 'saveuserfield':
            $phrasegroups[] = 'cprofilefield';
            $phrasegroups[] = 'user';
    }

    // get special data templates from the datastore
    $specialtemplates = array('bbcodecache');

    // pre-cache templates used by all actions
    $globaltemplates = array();

    // pre-cache templates used by specific actions
    $actiontemplates = array(
        'fetchuserfield' => array(
            'memberinfo_customfield_edit',
            'userfield_checkbox_option',
            'userfield_optional_input',
            'userfield_radio',
            'userfield_radio_option',
            'userfield_select',
            'userfield_select_option',
            'userfield_select_multiple',
            'userfield_textarea',
            'userfield_textbox',
        )
    );

    $_POST['ajax'] = 1;

    // ######################### REQUIRE BACK-END ############################
    require_once('./global.php');
    require_once(DIR . '/includes/class_xml.php');

    // #######################################################################
    // ######################## START MAIN SCRIPT ############################
    // #######################################################################

    ($hook = vBulletinHook::fetch_hook('ajax_start')) ? eval($hook) : false;

    // #############################################################################
    // user name search

    if ($_POST['do'] == 'usersearch')
    {
        $vbulletin->input->clean_array_gpc('p', array('fragment' => TYPE_STR));

        $vbulletin->GPC['fragment'] = convert_urlencoded_unicode($vbulletin->GPC['fragment']);

        if ($vbulletin->GPC['fragment'] != '' AND strlen($vbulletin->GPC['fragment']) >= 3)
        {
            $fragment = htmlspecialchars_uni($vbulletin->GPC['fragment']);
        }
        else
        {
            $fragment = '';
        }

        $xml = new vB_AJAX_XML_Builder($vbulletin, 'text/xml');
        $xml->add_group('users');

        if ($fragment != '')
        {
            $users = $db->query_read_slave("
                SELECT userid, username FROM " . TABLE_PREFIX . "user
                WHERE username LIKE('" . $db->escape_string_like($fragment) . "%')
                ORDER BY username
                LIMIT 15
            ");
            while ($user = $db->fetch_array($users))
            {
                $xml->add_tag('user', $user['username'], array('userid' => $user['userid']));
            }
        }

        $xml->close_group();
        $xml->print_xml();
    }
    Этот исходный код отформатирован с помощью FractalizeR's HabraSyntax Source Code Highlighter.


    Since the code is only written, comments and comments are welcome.

    A sufficient number of programming languages ​​are supported, so ... :) Enjoy your work.

    Also popular now: