. * *****************************************************************************/ /** * The hook_menu() * @return unknown_type */ function manpages_menu(){ $items['man'] = array( 'title' => "Explore man pages", 'page callback' => 'drupal_get_form', 'access callback' => TRUE, 'page arguments' => array('manpages_goto_form'), ); $items['man/%/%'] = array( 'page callback' => 'manpages_show', 'access callback' => TRUE, 'page arguments' => array(1,2), 'type' => MENU_CALLBACK, ); return $items; } /** * Clean a page name * @param $text * @return unknown_type */ function _manpage_clean_pagename($text){ return preg_replace('/[^A-Za-z0-9_\-]/', '', $text); } /** * Clean a section number * @param $text * @return unknown_type */ function _manpage_clean_section($text){ if (!in_array($text, array_keys(_manpages_sections()))) { return NULL; } else { return floor($text); } } /** * Clean a language string (unused) * @param $text * @return unknown_type */ function _manpage_clean_language($text){ return preg_replace('/[^A-Za-z0-9_\-]/', '', $text); } /** * HTML-ize shell colors * @param $manpage * @return unknown_type */ function _manpage_man2html($manpage){ $manpage = preg_replace("/\033\[1m([^\033]*)\033\[(0|4|22)m/", "\\1", $manpage); $manpage = preg_replace("/\033\[(4|22)m([^\033]*)\033\[(24|0)m/", "\\2", $manpage); $manpage = preg_replace("/\033\[[0-9;]*m/", "", $manpage); return $manpage; } /** * Remove shell colors * @param $manpage * @return unknown_type */ function _manpage_man2txt($manpage){ $manpage = preg_replace("/\033\[[0-9;]*m/", "", $manpage); return $manpage; } /** * Get a manual page * @param $page * @param $section * @param $language * @return unknown_type */ function _manpages_get($page, $section=NULL, $language=NULL){ $page = _manpage_clean_pagename($page); $section = _manpage_clean_section($section); $language = _manpage_clean_language($language); $mantext = array(); # drupal_set_message("man $section $page"); exec("man $section $page", $mantext); $text = implode("\n", $mantext); return $text; #return "man $section $page ($language)"; } /** * Goto Form * @param $form_state * @return unknown_type */ function manpages_goto_form($form_state){ $form = array('#submit'=>array('manpages_goto_form_submit')); $form['pagename'] = array( '#type' => 'textfield', '#title' => t("Page Name"), ); $sections = array(0=>t("All")); $sc = _manpages_sections(NULL); foreach ($sc as $sn=>$sd) { $sections[$sn] = "$sn - $sd"; } $form['section'] = array( '#type' => 'select', '#title' => t("Section"), '#options' => $sections, ); $form['format'] = array( '#type' => 'select', '#title' => t("Format"), '#options' => array( 'html' => t("Standard HTML"), 'text' => t("Plain text"), ), ); $form['submit'] = array( '#type' => 'submit', '#value' => t("Go"), '#submit'=>array('manpages_goto_form_submit'), ); return $form; } /** * Go to a man page * @param $form * @param $form_state * @return unknown_type */ function manpages_goto_form_submit($form, &$form_state){ $values = $form_state['values']; $page = $values['pagename']; $section = $values['section']; $language = $values['language']; $format = ($values['format']=='text') ? 'text' : 'html'; $page = _manpage_clean_pagename($page); $section = _manpage_clean_section($section); $language = _manpage_clean_language($language); $url = "man/$format/$page/$section/$language"; drupal_goto($url); } /** * Return available sections * @param $section * @return unknown_type */ function _manpages_sections($section=NULL){ $sections = array( 1 => t("User Commands"), 2 => t("System Calls"), 3 => t("C Library Functions"), 4 => t("Devices and Special Files"), 5 => t("File Formats and Conventions"), 6 => t("Games et. Al."), 7 => t("Miscellanea"), 8 => t("System Administration tools and Deamons"), ); if (is_null($section)) { return $sections; } else { return $sections[$section]; } } /** * Callback to show man pages * @param $format * @param $page * @param $section * @param $language * @return unknown_type */ function manpages_show($format='html', $page=NULL, $section=NULL, $language=NULL){ $page = _manpage_clean_pagename($page); $section = _manpage_clean_section($section); $language = _manpage_clean_language($language); $manpage = _manpages_get($page, $section, $language); if ($format == 'html') { $links = array(); $links[] = l(t("Text Plain"), "man/text/$page" . (($section)?("/$section" . $language?"/$language":''):'')); $links = implode(" ", $links); $mantext = _manpage_man2html($manpage); return "
$mantext"; } elseif($format == 'text') { drupal_set_header('Content-type: text/plain'); $mantext = _manpage_man2txt($manpage); echo $mantext; exit(); } else { drupal_set_message(t("Unknown format!"), 'warning'); return t("Unable to display page. unkown format specified."); } }