A helper class to convert HTML to Markdown.
Author: Colin O'Dell (colinodell@gmail.com)
Author: Nick Cernis (nick@cern.is)
Exemplo n.º 1
0
 /**
  *  Parses uploaded Markdown files and displays them (does NOT insert)
  *
  *  @param   Request  $request
  *
  *  @return  mixed  Depending on validation: Redirect or Response
  */
 public function postImport(Request $request)
 {
     // Check if files have been uploaded
     $store = Storage::disk('local');
     $files = $store->files('import');
     if (count($files) <= 0) {
         return redirect('/import')->withErrors(['content' => 'Please input something to import!']);
     }
     // Now loop through all found files and extract the notes
     $notes = new Collection();
     $converter = new HtmlConverter(array('strip_tags' => true));
     foreach ($files as $file) {
         $fcontents = $store->get($file);
         if (in_array(pathinfo($file, PATHINFO_EXTENSION), $this->html_extensions)) {
             // We need to convert it to markdown first.
             $fcontents = $converter->convert($fcontents);
         }
         $notes = $notes->merge($this->retrieveNotes($fcontents, $request->suggestTags, $request->headingType));
     }
     // Now clear the directory
     $store->delete($files);
     // TODO: Check for custom elements (h2, h3, ps, etc.)
     // Now in $notes all h4s with trailing stuff should reside
     return view('imports.confirm', compact('notes'));
 }
Exemplo n.º 2
0
 /**
  * Show the form for editing the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function edit($id)
 {
     $course = Course::findOrFail($id);
     $converter = new HtmlConverter();
     $description = $converter->convert($course->description);
     return view('admin.course.edit', compact('course', 'description'));
 }
Exemplo n.º 3
0
 /**
  * Show the form for editing the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function edit($id)
 {
     $article = Article::find($id);
     $converter = new HtmlConverter();
     $html = $article->content;
     $markdown = $converter->convert($html);
     return view('article.edit', compact('article', 'markdown'));
 }
Exemplo n.º 4
0
 /**
  * Show the form for editing the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function edit($id)
 {
     $lesson = Lesson::findOrFail($id);
     // Go from HTML to Markdown
     $converter = new HtmlConverter();
     $description = $converter->convert($lesson->description);
     $courses = Course::all();
     return view('admin.lesson.edit', compact('lesson', 'courses', 'description'));
 }
Exemplo n.º 5
0
 public function getEditPost($postId)
 {
     $converter = new HtmlConverter();
     $post = Post::findOrFail($postId);
     $postContent = $converter->convert($post->content);
     if (Auth::user()->id == $post->user_id) {
         return view('forum.post.edit', compact('post', 'postContent'));
     }
     return redirect('/');
 }
Exemplo n.º 6
0
 /**
  * @param array $payload
  * @return array
  */
 protected function getTaskDescription(array $payload)
 {
     $description = '';
     if (!empty($payload['HtmlBody'])) {
         $htmlConverter = new HtmlConverter(array('strip_tags' => true));
         $description = $htmlConverter->convert($payload['HtmlBody']);
     } elseif (!empty($payload['TextBody'])) {
         $description = $payload['TextBody'];
     }
     return $description;
 }
 /**
  * Converts HTML to Markdown and explode.
  *
  * @param string $doc
  * @param bool   $indent
  *
  * @return array
  */
 private function formatDoc($doc, $indent = false)
 {
     $doc = explode("\n", $this->htmlToMarkdown->convert($doc));
     if ($indent) {
         $count = count($doc);
         for ($i = 1; $i < $count; ++$i) {
             $doc[$i] = self::INDENT . $doc[$i];
         }
     }
     return $doc;
 }
Exemplo n.º 8
0
 /**
  * Parse incoming email
  *
  * @access public
  * @param  array   $payload   Incoming email
  * @return boolean
  */
 public function receiveEmail(array $payload)
 {
     if (empty($payload['sender']) || empty($payload['subject']) || empty($payload['recipient'])) {
         return false;
     }
     // The user must exists in Kanboard
     $user = $this->user->getByEmail($payload['sender']);
     if (empty($user)) {
         $this->logger->debug('Mailgun: ignored => user not found');
         return false;
     }
     // The project must have a short name
     $project = $this->project->getByIdentifier(Tool::getMailboxHash($payload['recipient']));
     if (empty($project)) {
         $this->logger->debug('Mailgun: ignored => project not found');
         return false;
     }
     // The user must be member of the project
     if (!$this->projectPermission->isMember($project['id'], $user['id'])) {
         $this->logger->debug('Mailgun: ignored => user is not member of the project');
         return false;
     }
     // Get the Markdown contents
     if (!empty($payload['stripped-html'])) {
         $htmlConverter = new HtmlConverter(array('strip_tags' => true));
         $description = $htmlConverter->convert($payload['stripped-html']);
     } else {
         if (!empty($payload['stripped-text'])) {
             $description = $payload['stripped-text'];
         } else {
             $description = '';
         }
     }
     // Finally, we create the task
     return (bool) $this->taskCreation->create(array('project_id' => $project['id'], 'title' => $payload['subject'], 'description' => $description, 'creator_id' => $user['id']));
 }
Exemplo n.º 9
0
 public function getMessageAsMarkdown(array $config = [])
 {
     $converter = new HtmlConverter($config);
     return $converter->convert($this->message);
 }
Exemplo n.º 10
0
 */
$title = get_input('title', false);
$content = get_input('content', false);
$guid = get_input('guid', false);
$container_guid = get_input('container_guid', elgg_get_logged_in_user_guid());
if (!$content || !$title) {
    register_error('staticsites:error:nocontent');
    forward(REFERER);
}
// Check and see if we're editing an existing page
$page = get_entity($guid);
if (elgg_instanceof($page, 'object', 'static_page')) {
    // Existing page
    $page->description = $content;
    $page->title = $title;
} else {
    // New page
    $page = new ElggObject();
    $page->subtype = 'static_page';
    $page->description = $content;
    $page->title = $title;
    $page->container_guid = $container_guid;
}
$page->save();
use League\HTMLToMarkdown\HtmlConverter;
$converter = new HtmlConverter();
$markdown = $converter->convert($content);
system_message(elgg_echo('staticsites:success:pagesaved'));
// Output page info
echo json_encode(array('guid' => $page->guid, 'title' => $page->title, 'content' => $page->description, 'container_guid' => $page->container_guid));
forward(REFERER);
Exemplo n.º 11
0
 /**
  * Get Markdown content for the task
  *
  * @access public
  * @param  array $payload
  * @return string
  */
 public function getDescription(array $payload)
 {
     if (!empty($payload['stripped-html'])) {
         $htmlConverter = new HtmlConverter(array('strip_tags' => true));
         return $htmlConverter->convert($payload['stripped-html']);
     } elseif (!empty($payload['stripped-text'])) {
         return $payload['stripped-text'];
     }
     return '';
 }
Exemplo n.º 12
0
 /**
  * @param string $html
  * @return string
  */
 public function convert($html)
 {
     $markdown = $this->converter->convert($html);
     return $this->processMarkdown($markdown);
 }
Exemplo n.º 13
0
 /**
  * Post to diaspora* when saving a post.
  *
  * @since 1.5.0
  *
  * @todo Maybe somebody wants to share a password protected post to a closed aspect.
  *
  * @param integer $post_id ID of the post being saved.
  * @param WP_Post $post    Post object being saved.
  * @return boolean If the post was posted successfully.
  */
 public function post($post_id, $post)
 {
     $this->_assign_wp_post($post);
     $options = WP2D_Options::instance();
     // Is this post type enabled for posting?
     if (!in_array($post->post_type, $options->get_option('enabled_post_types'))) {
         return false;
     }
     // Make sure we're posting to diaspora* and the post isn't password protected.
     if (!($this->post_to_diaspora && 'publish' === $post->post_status && '' === $post->post_password)) {
         return false;
     }
     $status_message = $this->_get_title_link();
     // Post the full post text or just the excerpt?
     if ('full' === $this->display) {
         $status_message .= $this->_get_full_content();
     } else {
         $status_message .= $this->_get_excerpt_content();
     }
     // Add the tags assigned to the post.
     $status_message .= $this->_get_tags_to_add();
     // Add the original entry link to the post?
     $status_message .= $this->_get_posted_at_link();
     $status_converter = new HtmlConverter(array('strip_tags' => true));
     $status_message = $status_converter->convert($status_message);
     // Set up the connection to diaspora*.
     $conn = WP2D_Helpers::api_quick_connect();
     if (!empty($status_message)) {
         if ($conn->last_error) {
             // Save the post error as post meta data, so we can display it to the user.
             update_post_meta($post_id, '_wp_to_diaspora_post_error', $conn->last_error);
             return false;
         }
         // Add services to share to via diaspora*.
         $extra_data = array('services' => $this->services);
         // Try to post to diaspora*.
         if ($response = $conn->post($status_message, $this->aspects, $extra_data)) {
             // Save certain diaspora* post data as meta data for future reference.
             $this->_save_to_history((object) $response);
             // If there is still a previous post error around, remove it.
             delete_post_meta($post_id, '_wp_to_diaspora_post_error');
         }
     } else {
         return false;
     }
 }
Exemplo n.º 14
0
 public function importSelf()
 {
     $converter = new HtmlConverter();
     $query = mssql_query("SELECT p.*, d.Counter FROM Posts p\n\t\t\t\t\t\t\tLEFT JOIN (SELECT Second_Id, count(*) Counter FROM Documents WHERE Type = 'post' GROUP BY Second_Id) d ON d.Second_Id = p.Id \n\t\t\t\t\t\t\tWHERE (p.Post IS NOT NULL AND p.Post NOT LIKE '')\n\t\t\t\t\t\t\tOR d.Counter > 0");
     while ($row = mssql_fetch_array($query, MSSQL_ASSOC)) {
         $table[] = $row;
     }
     mssql_free_result($query);
     if ($this->truncate()) {
         foreach ($table as $p) {
             $p = sanitize($p);
             $p['Date_Creation'] = $p['Date_Creation'] . " " . $p['Time'];
             $p['Date_Creation'] = str_replace(".0000000", "", $p['Date_Creation']);
             $p['Post_Public'] = $p['Post_Public'] == '0' ? '2' : '3';
             $p['Post'] = Purifier::clean($p['Post']);
             $p['Post'] = $p['Post'] == '' ? $p['Counter'] > 1 ? '<p>See attachments: </p>' : '<p>See attachment: </p>' : $p['Post'];
             if ($p['Id_Customer_User'] != '') {
                 $subquery1 = mssql_query("SELECT * FROM Customer_User_Login WHERE Customer_Id = '" . $p['Id_Customer_User'] . "'");
                 $result1 = mssql_fetch_array($subquery1, MSSQL_ASSOC);
                 $subquery2 = mssql_query("SELECT * FROM Tickets WHERE Id = '" . $p['Id_Ticket'] . "'");
                 $result2 = mssql_fetch_array($subquery2, MSSQL_ASSOC);
                 $subquery3 = "SELECT * FROM company_person WHERE email = '" . trim($result1['email_customer_user']) . "' AND company_id = '" . $result2['Id_Customer'] . "'";
                 $result = mysqli_query($this->manager->conn, $subquery3);
                 $record = mysqli_fetch_array($result);
                 $author_id = $record['id'];
             }
             if (!isset($author_id)) {
                 $author_id = findCompanyPersonId($p['Author'], $this->manager->conn);
             }
             if (strpos($p['Post'], "<p>Waiting for feedback") !== false) {
                 $p['Post'] = $p['Post'] == "<p>Waiting for feedback:</p>" ? "" : str_replace("Waiting for feedback: ", "", $p['Post']);
                 $p['Ticket_Status_Id'] = TICKET_WFF_STATUS_ID;
             } else {
                 $p['Ticket_Status_Id'] = TICKET_IN_PROGRESS_STATUS_ID;
             }
             //convert from html to markdown
             $p['Post'] = fixMarkdown($converter->convert($p['Post']));
             $p = nullIt($p);
             $query = "INSERT INTO posts (id,ticket_id,post,author_id,status_id,ticket_status_id,created_at,updated_at) \n\t\t\t\t\t\t  VALUES (" . $p['Id'] . "," . $p['Id_Ticket'] . "," . $p['Post'] . "," . $author_id . "," . $p['Post_Public'] . "," . $p['Ticket_Status_Id'] . "," . $p['Date_Creation'] . "," . $p['Date_Creation'] . ")";
             if (mysqli_query($this->manager->conn, $query) === TRUE) {
                 $this->successes++;
             } else {
                 $this->errors++;
                 if ($this->debug) {
                     logMessage("DEBUG: " . mysqli_error($this->manager->conn) . " [Post ID = " . $p['Id'] . "]");
                     echo $p['Post'];
                     if (!isset($ids)) {
                         $ids = '';
                     }
                     $ids .= $p['Id'] . ",";
                 }
             }
             $author_id = null;
         }
         $table = [];
         $query = mssql_query("SELECT * FROM Tickets\n\t\t\t\t\t\t\t\t  WHERE Status IN (6,7) \n\t\t\t\t\t\t\t\t  ORDER BY Id");
         while ($row = mssql_fetch_array($query, MSSQL_ASSOC)) {
             $table[] = $row;
         }
         foreach ($table as $p) {
             $p = sanitize($p);
             $p['Post'] = trim($p['Comment']) == "" ? "Ticket Closed" : $p['Comment'];
             $p['Post'] = Purifier::clean($p['Post']);
             $assignee_id = findCompanyPersonId($p['Id_Assignee'], $this->manager->conn);
             $p['Post'] = fixMarkdown($converter->convert($p['Post']));
             $p = nullIt($p);
             $query = "INSERT INTO posts (ticket_id,post,author_id,status_id,ticket_status_id,created_at,updated_at) \n\t\t\t\t\t\t  VALUES (" . $p['Id'] . "," . $p['Post'] . "," . $assignee_id . ",3," . TICKET_SOLVED_STATUS_ID . "," . $p['Date_Update'] . "," . $p['Date_Update'] . ")";
             if (mysqli_query($this->manager->conn, $query) === TRUE) {
                 $this->successes++;
             } else {
                 $this->errors++;
                 if ($this->debug) {
                     logMessage("DEBUG: " . mysqli_error($this->manager->conn) . " [Post ID = " . $p['Id'] . "]");
                     if (!isset($ids)) {
                         $ids = '';
                     }
                     $ids .= $p['Id'] . ",";
                 }
             }
             $author_id = null;
         }
         if (isset($ids)) {
             logMessage("Error Query: SELECT * FROM Posts WHERE Id IN (" . $ids . ")");
         }
         logMessage("Successes: " . $this->successes, 'successes');
         logMessage("Errors: " . $this->errors, 'errors');
     }
 }
 public function processContent($content)
 {
     $content = apply_filters('the_content', $content);
     // make sure all shortcodes are resolved
     $content = do_shortcode($content);
     $content = do_shortcode($content);
     $content = do_shortcode($content);
     // issue: e.g. a "</p>" tag breaks the converter
     // try to remove these invalid content
     $converter = new HtmlConverter(array('strip_tags' => true, 'header_style' => 'atx'));
     try {
         $content_converted = $converter->convert($content);
     } catch (\InvalidArgumentException $e) {
         if (WP2GRAV_EXPORT_HTMLPURIFIER) {
             if (!isset($this->purifier)) {
                 // http://htmlpurifier.org/
                 $config = \HTMLPurifier_Config::createDefault();
                 $this->purifier = new \HTMLPurifier($config);
             }
             $clean_content = $this->purifier->purify($content);
             $content_converted = $converter->convert($clean_content);
             echo "<hr>catched html convert exception " . $e->getMessage() . " and cleaned content<hr>";
         } else {
             $e->getMessage();
             exit;
         }
     }
     //        $converter->setOption('italic_style', '_');
     //        $converter->setOption('bold_style', '__');
     return $content_converted;
 }