Example #1
0
    def run(self, edit, meta_type=None):
        if meta_type is None:
            meta_type = PelicanPluginTools.detect_article_type(self.view)

        article_metadata_template_keys = []
        article_metadata_template_lines = PelicanPluginTools.load_article_metadata_template_lines(self.view, meta_type)
        article_metadata_template_lines = PelicanPluginTools.normalize_article_metadata_case(article_metadata_template_lines)
        if article_metadata_template_lines:
            for article_metadata_template_line in article_metadata_template_lines:
                regex = re.compile(":?(\w+):")
                find_all = regex.findall(article_metadata_template_line)
                if len(find_all) > 0:
                    metadata_key = find_all[0]
                    if not metadata_key in article_metadata_template_keys:
                        article_metadata_template_keys.append(metadata_key)

        metadata = {}
        for article_metadata_template_key in article_metadata_template_keys:
            metadata[article_metadata_template_key] = ""

        metadata_regions = PelicanPluginTools.get_metadata_regions(self.view, "multiple")
        if len(metadata_regions) > 0:
            for region in metadata_regions:
                metadata_str = self.view.substr(region)
                metadata_str = PelicanPluginTools.normalize_article_metadata_case(metadata_str)[0]
                regex = re.compile(":?(\w+):(.*)")
                find_all = regex.findall(metadata_str)
                if len(find_all) > 0:
                    for (field_name, field_value) in find_all:
                        field_data = (field_name.strip(), field_value.strip())
                        if not field_name in metadata:
                            new_meta = "%s: %s" % field_data
                            if meta_type is "rst":
                                new_meta = ":" + new_meta
                            article_metadata_template_lines.append(new_meta)
                        metadata[field_name] = field_value.strip()

            old_metadata_begin = metadata_regions[0].begin()
            old_metadata_end = metadata_regions[len(metadata_regions) - 1].end()
            old_metadata_region = sublime.Region(old_metadata_begin, old_metadata_end)

        # initialize date field if it's empty
        metadata_key_date = "Date"
        for key in metadata.keys():
            if key.lower() == "date":
                metadata_key_date = key
        if metadata[metadata_key_date] is "":
            metadata[metadata_key_date] = PelicanPluginTools.strDateNow()

        e = self.view.begin_edit()
        article_metadata_template = PelicanPluginTools.normalize_line_endings(
            self.view, "\n".join(article_metadata_template_lines))
        article_metadata_str = article_metadata_template % metadata
        if len(metadata_regions) > 0:
            self.view.replace(e, old_metadata_region, article_metadata_str)
        else:
            self.view.insert(e, 0, article_metadata_str)
        self.view.end_edit(e)

        # initialize slug field if it's empty
        metadata_key_slug = "Slug"
        for key in metadata.keys():
            if key.lower() == "slug":
                metadata_key_slug = key
        force_slug_regeneration = PelicanPluginTools.load_setting(self.view, "force_slug_regeneration", False)
        if force_slug_regeneration or len(metadata[metadata_key_slug]) is 0:
            self.view.run_command('pelican_generate_slug')

        # scroll to top
        self.view.show(0)
Example #2
0
 def run(self, edit, mode="single"):
     self.view.sel().clear()
     metadata_regions = PelicanPluginTools.get_metadata_regions(self.view, mode)
     for region in metadata_regions:
         self.view.sel().add(region)
     self.view.show(self.view.sel())